Change SAS Macro Variable

In general, how do we deal with a situation where macro variables must be changed 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 deal with this situation when I want% test (3), and then arraytwo need to take the dimension 4 ...?

+6
sas sas-macro
source share
2 answers

Change it to

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

+5
source share

Using% eval is sufficient if only integer arithmetic is required. 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 using one of two methods:

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

For many of the most commonly used functions, there are exact macroequivalents, and all you have to do is add% before the function name. Functions that do not have exact macroequivalents can usually be taken to accept a macro variable by calling them inside% sysfunc (). Notably, however, they usually expect that a string enclosed in single quotes will fail when called in a piece of macro through% sysfunc () unless you remove the quotes. For example.

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

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

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

This is because, in the macro environment, SAS interprets the text uniform as a string, while at the data stage, SAS interprets unquoted text as a variable name.

+3
source share

All Articles