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... ?
Change it to
array arraytwo[%EVAL(&ARG + 1)] ;
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With