SAS 宏取消引用

SAS Macro Dereferencing

提问人:ruhi 提问时间:11/11/2023 最后编辑:Richardruhi 更新时间:11/12/2023 访问量:42

问:

声明了以下宏变量:

%let x=2; 
%let y=3;
%let x3=4; 
%let y2=5; 
%let x2=6; 
%let y3=7; 
%let m=x; 
%let n=y; 

取消引用后将评估什么:

i.&&&m&y - &&&n&y 
ii.&&&m&x&&&n&y
SAS

评论

0赞 Tom 11/12/2023
当你运行它时,你会得到什么?
0赞 ruhi 11/12/2023
1 选项 NONOTES NOSTIMER NOSOURCE NOSYNTAXCHECK;68 69 %让 x=2;70 %让 y=3;71 %让 x3=4;72 %让 y2=5;73 %让 x2=6;74 %让 y3=7;75 %让 m=x;76 %让 n=y;77 78 %put &&&&m&y - &&&&n&y ;4 - 7 79 80 选项 NONOTES NOSTIMER NOSOURCE NOSYNTAXCHECK;90
0赞 Tom 11/12/2023
那么问题是什么呢?
0赞 ruhi 11/12/2023
但不确定是否正确
0赞 ruhi 11/12/2023
取消引用后将评估 i. &&&&m&y - &&&&n&y ii.&&&&m&x&&&n&y

答:

4赞 Tom 11/12/2023 #1

你可以运行它来找到答案。

1    %let x=2;
2    %let y=3;
3    %let x3=4;
4    %let y2=5;
5    %let x2=6;
6    %let y3=7;
7    %let m=x;
8    %let n=y;
9
10   %put i. &&&m&y - &&&n&y ;
i. 4 - 7
11   %put ii. &&&m&x&&&n&y ;
ii. 67

当宏处理器看到&&时,它会将其转换为&,并提醒自己需要再次传递结果以进一步解析宏变量引用。因此,在 &&&&m&y 的第一次传递中,它将 && 转换为 &,将 &m 转换为 x,将 &y 转换为 3。然后,当它返回时,它会处理结果,它将 &x3 转换为 4。

您可以打开 SYMBOLGEN 并查看其运行情况。

12
13   options symbolgen;
14   %put i. &&&m&y - &&&n&y ;
SYMBOLGEN:  && resolves to &.
SYMBOLGEN:  Macro variable M resolves to x
SYMBOLGEN:  Macro variable Y resolves to 3
SYMBOLGEN:  Macro variable X3 resolves to 4
SYMBOLGEN:  && resolves to &.
SYMBOLGEN:  Macro variable N resolves to y
SYMBOLGEN:  Macro variable Y resolves to 3
SYMBOLGEN:  Macro variable Y3 resolves to 7
i. 4 - 7
15   %put ii. &&&m&x&&&n&y ;
SYMBOLGEN:  && resolves to &.
SYMBOLGEN:  Macro variable M resolves to x
SYMBOLGEN:  Macro variable X resolves to 2
SYMBOLGEN:  && resolves to &.
SYMBOLGEN:  Macro variable N resolves to y
SYMBOLGEN:  Macro variable Y resolves to 3
SYMBOLGEN:  Macro variable X2 resolves to 6
SYMBOLGEN:  Macro variable Y3 resolves to 7
ii. 67