我在宏中定义一个宏变量。然后,我把它输入到第二个宏中。在macro2计数器内将值更改为200。但是,当我检查宏2运行后放入的宏变量中的内容时,它仍然是0。我想把价值200的东西存起来?这个是可能的吗?
%macro macro1();
%let variable1= 0;
macro2(counter=&variable1)
%put &variable1;
%mend macro1;
%macro1;
发布于 2015-06-15 20:49:36
你这里有几个问题。首先,您在调用%
之前遗漏了macro2
,但我怀疑这只是一个错误。主要问题是,您正在尝试执行在其他语言中称为按引用呼叫的操作。您可以在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;
实际上,我有另一篇关于StackOverflow的文章,它解释了&&&
语法;您可以使用看看这里。请注意,%EVAL
调用与逐引用调用无关,它只是为了进行添加。
https://stackoverflow.com/questions/30855047
复制相似问题