Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SAS macro variable change

Tags:

sas

sas-macro

In general how do we deal with the situation where macro variables need to be modified inside a macro; for example, suppose I have this macro:

%macro test (arg=); 
array arrayone [&arg];   /* This is ok */
array arraytwo [&arg+1] /* This is not ok. How to make it work? */ 
... 

How do we manage these situation when I want %test(3) and then the arraytwo needs to take dimension 4... ?

like image 349
francogrex Avatar asked Mar 25 '11 08:03

francogrex


2 Answers

Change it to

array arraytwo[%EVAL(&ARG + 1)] ;

like image 163
Chris J Avatar answered Oct 06 '22 22:10

Chris J


Using %eval is sufficient as long as you only require integer arithmetic. If you need to perform floating point arithmetic with macro variables, you should use %sysevalf instead.

Most data step functions can be applied directly to macro variables via one of two methods:

 1. %function()
 2. %sysfunc(function())

For many of the most commonly used functions, there are exact macro equivalents, and all you have to do is add a % in front of the function name. Functions that don't have exact macro equivalents can usually be made to accept a macro variable by calling them inside %sysfunc(). N.B. data step functions that usually expect a string wrapped in single quotes will fail when called in a piece of macro code via %sysfunc() unless you remove the quotes. E.g.

data _null_;
  x = rand('uniform');
run;

works fine in a data step, but to give a macro variable the same value in a piece of macro code, you would need to use

%let x = %sysfunc(rand(uniform));

This is because in the macro environment, SAS interprets the text uniform as a string, whereas in a data step SAS would interpret the unquoted text as the name of a variable.

like image 43
user667489 Avatar answered Oct 06 '22 20:10

user667489