SAS间接宏处理

时间:2014-05-21 15:31:17

标签: sas sas-macro

我在考试试卷上有一个问题,询问SAS日志中的内容

%let test=one;
%let one=two;
%let two=three;
%let three=last;

%put what displays is &&&&&test;

我很惊讶地发现答案是:两个,因为我认为此引用将完全解析为最后。 SAS也同意答案是两个

任何人都可以解释一下SAS如何得出答案 2 ,因为我读过的所有理论说明都表明宏处理器应该执行以下操作

  • scan1&&&&& test - > &&&& test(即&&&&&&并告诉处理器继续从右向左扫描)
  • scan2&&&& test - > &&& one
  • scan3&&& one - > && two
  • scan4&& two - > &安培;三
  • scan5& three - >最后

1 个答案:

答案 0 :(得分:5)

使用symbolgen选项可以帮助查看日志中发生的事情:

1    options symbolgen;

2    %let test=one;
3    %let one=two;
4    %let two=three;
5    %let three=last;
6
7    %put what displays is &&&&&test;
SYMBOLGEN:  && resolves to &.
SYMBOLGEN:  && resolves to &.
SYMBOLGEN:  Macro variable TEST resolves to one
SYMBOLGEN:  && resolves to &.
SYMBOLGEN:  Macro variable ONE resolves to two
what displays is two

从左到右,使用括号显示代币:

&&&&&test
(&&)(&&)(&test)
(&) (&) (one)
&&one
(&&)(one)
(&)(one)
&one
two