解决宏变量名称SAS中包含%eval的宏 [英] Resolving macro containing %eval in macro variable name, SAS

查看:60
本文介绍了解决宏变量名称SAS中包含%eval的宏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个玩具示例,以帮助解决我遇到的更大问题.本质上,它涉及在引用较大的宏变量名称时使用%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_&xNruntil &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"

这篇关于解决宏变量名称SAS中包含%eval的宏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆