开发者

SAS macro variable change

In general how do we deal with th开发者_如何学运维e 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜