Wednesday, January 9, 2019

SAS: Test scope of macro variable (created in a data step in a macro)



TEST SCOPE OF MACRO VARIABLE



SOURCE CODE FOLLOWS:



* Test scope of macro variable ;



options nomprint nomlogic nosymbolgen;



data work.x;

input a $ b $ ;

datalines;

X J

X K

Y K

Y J

Z J

Z K

;

run;





%macro inner_macro(A=);



%local macvar;



data _NULL_;

set work.x ( where = (  a = "&A" )) ;

put a= b=;

if (b = "K") then do;

     call symputx("macvar", "Y", "L");

end;

else do;

     call symputx("macvar", "N", "L");

end;

test=symget("macvar");

put test=;

output;

stop; * end after first obs passing where condition ;

run;



%put >>--> After data step &=macvar;



%mend inner_macro;





%macro outer_macro;



%inner_macro(A=X);  * expect macvar=N ;

%inner_macro(A=Y);  * expect macvar=Y ;

%inner_macro(A=Z);  * expect macvar=N ;



%mend outer_macro;



%outer_macro;






LOG FOLLOWS:



NOTE: The data set WORK.X has 6 observations and 2 variables.

NOTE: DATA statement used (Total process time):

      real time           0.01 seconds

      cpu time            0.01 seconds

     





A=X B=J

TEST=N

NOTE: There were 1 observations read from the data set WORK.X.

      WHERE a='X';

NOTE: DATA statement used (Total process time):

      real time           0.00 seconds

      cpu time            0.00 seconds

     



>>--> After data step MACVAR=N



A=Y B=K

TEST=Y

NOTE: There were 1 observations read from the data set WORK.X.

      WHERE a='Y';

NOTE: DATA statement used (Total process time):

      real time           0.00 seconds

      cpu time            0.00 seconds

     



>>--> After data step MACVAR=Y



A=Z B=J

TEST=N

NOTE: There were 1 observations read from the data set WORK.X.

      WHERE a='Z';

NOTE: DATA statement used (Total process time):

      real time           0.00 seconds

      cpu time            0.00 seconds

     



>>--> After data step MACVAR=N




No comments:

Post a Comment