解析宏变量名称中包含 %eval 的宏,SAS [英] Resolving macro containing %eval in macro variable name, SAS
问题描述
这是一个玩具示例,旨在帮助我解决更大的问题.它本质上涉及在引用更大的宏变量名称时使用 %eval() 宏.
this is a toy example in order to help with a larger problem I have. It essentially involves using a %eval() macro when referencing a bigger macro variable name.
我创建了一个宏变量x_2,它使用循环的值'&it',从最终输出可以看出该变量已成功创建,但是我只能将其放入日志而不进行评估&it+1,当使用大于 1 的循环时,我需要这样做.
I have created a macro variable x_2, which uses the values of the loop, '&it', the variable is created successfully as can be seen by the final output, however I can only put it to the log without evaluating &it+1, which I will need to do when using a loop bigger than size 1.
似乎先解决 x_ 并给出警告,然后再评估 x_2 作为一个整体并给出输出.
It seems to resolve x_ first, giving a warning, before then evaluating x_2 as a whole and giving the output.
我意识到这只是一个关于如何正确引用宏的问题,但我找不到任何将评估用作宏变量名的一部分的示例.
I realise this is just a problem about how to reference macros correctly, but I cannot find any examples where it uses an evaluation as part of a macro variable name.
谢谢.
%macro testing;
%DO it = 1 %TO 1;
data dataset;
s=100;
t=99;
run;
data _null_;
set dataset;
if s = 100 then do;
call symput("x_%eval(&it+1)",t);
end;
run;
%put "&x_%eval(&it+1)";
%put &x_2;
%END;
%mend testing;
%testing;
日志输出
MLOGIC(TESTING): %PUT "&x_%eval(&it+1)"
WARNING: Apparent symbolic reference X_ not resolved.
SYMBOLGEN: Macro variable IT resolves to 1
SYMBOLGEN: Macro variable X_2 resolves to 99
" 99"
MLOGIC(TESTING): %PUT &x_2
SYMBOLGEN: Macro variable X_2 resolves to 99
99
MLOGIC(TESTING): %DO loop index variable IT is now 2; loop will not iterate again.
MLOGIC(TESTING): Ending execution.
推荐答案
确实,SAS 在调用 %eval 函数之前进行了变量替换最简单的解决方案是在前面的语句中调用 %eval
Indeed, SAS does the variable substitution before calling the %eval function The easiest solution is to call %eval in an earlier statement
%let xNr = %eval(&it+1);
%put "&&x_&xNr";
双&符号用于延迟 x_&xNr
的评估,直到 &xNr
评估为 2
并且没有警告x_
未定义.
The double ampersand serves to delay the evaluation of x_&xNr
until &xNr
is evaluated to 2
and not have the warning that x_
is undefined.
SYMBOLGEN: Macro variable IT resolves to 1
SYMBOLGEN: && resolves to &.
SYMBOLGEN: Macro variable XNR resolves to 2
SYMBOLGEN: Macro variable X_2 resolves to 99
" 99"
这篇关于解析宏变量名称中包含 %eval 的宏,SAS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!