在SAS宏中改变宏变量的价值

Dan*_*Duq 7 sas sas-macro

我在宏中定义一个宏变量.然后,我将它喂入第二个宏.在macro2计数器内部将值更改为200.但是,当我检查宏2变量运行后我放入的宏变量内部时,它仍然显示0.我希望它存储值200?这可能吗?

 %macro macro1();
   %let variable1= 0;
   macro2(counter=&variable1)

   %put &variable1;
 %mend macro1;

 %macro1;
Run Code Online (Sandbox Code Playgroud)

spa*_*ead 9

你有几个问题在这里.首先,你%在打电话之前就错过了macro2,但我怀疑这只是一个错字.主要问题是您尝试使用其他语言中的引用来执行call-by-reference.您可以通过传递变量的名称而不是变量的值来在SAS宏中执行此操作,然后使用一些时髦的&语法将该名称的变量设置为新值.

以下是一些示例代码:

%macro macro2(counter_name);
    /* The following code translates to:
    "Let the variable whose name is stored in counter_name equal
    the value of the variable whose name is stored in counter_name
    plus 1." */

    %LET &counter_name = %EVAL (&&&counter_name + 1);

%mend;

%macro macro1();
   %let variable1= 0;

   /* Try it once - see a 1 */
   /* Notice how we're passing 'variable1', not '&variable1' */
   %macro2(counter_name = variable1)
   %put &variable1;

   /* Try it twice - see a 2 */
   /* Notice how we're passing 'variable1', not '&variable1' */
   %macro2(counter_name = variable1)
   %put &variable1;
 %mend macro1;

 %macro1;
Run Code Online (Sandbox Code Playgroud)

我实际上在StackOverflow上有另一篇文章,它解释了&&&语法; 你可以在这看看.请注意,该%EVAL调用与call-by-reference无关,只是在那里进行添加.