Search code examples
sassas-macro

what is the logic of this code in SAS Macros?


%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;

i need to know what is value of test outside the macro defination?


Solution

  • If a macro variable is defined in the global scope, i.e. your %LET X = 15;, then any changes within a macro to that macro variable affect the global value, unless you explicitly override it within the macro using %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 */
    

    But with %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 */