SAS宏中此代码的逻辑是什么?

ved*_*ll_ 0 sas sas-macro

%let x = 15;


  %macro test;
        %let x = 10;
        %put x inside macro test = &x;
    %mend test;

    %test;

    %put x outside the macro test =&x;
    %put x inside the macro test =&x;
Run Code Online (Sandbox Code Playgroud)

我需要知道宏定义之外的测试值是多少?

Chr*_*s J 6

如果在全局范围中定义了宏变量,即您的%LET X = 15;,则宏中宏变量的任何更改都会影响全局值,除非您在宏中使用显式覆盖它%LOCAL X;.

%let x = 15; /* Global X = 15 */

%macro test;
   %let x = 10; /* Global X = 10 */
   %put x inside macro test = &x; /* 10 */
%mend test;
%test;

%put x outside the macro test =&x; /* 10 */

但随着 %LOCAL

%let x = 15; /* Global X = 15 */

%macro test;
   %local x;
   %let x = 10; /* Local X = 10 */
   %put x inside macro test = &x; /* 10 */
%mend test;
%test;

%put x outside the macro test =&x; /* 15 */

  • 换句话说,在内部宏中,始终确保将宏变量定义为"local"或"global"以避免错误. (2认同)