SAS macro variable change

谁说胖子不能爱 提交于 2019-12-10 04:57:56

问题


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... ?


回答1:


Change it to

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




回答2:


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.



来源:https://stackoverflow.com/questions/5430299/sas-macro-variable-change

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!