SAS Macro Dereferencing

Following macro variables are declared:

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

What will following dereferencing evaluate to :

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

  • What do you get when you run it?

    – 




  • 1 OPTIONS NONOTES NOSTIMER NOSOURCE NOSYNTAXCHECK; 68 69 %let x=2; 70 %let y=3; 71 %let x3=4; 72 %let y2=5; 73 %let x2=6; 74 %let y3=7; 75 %let m=x; 76 %let n=y; 77 78 %put &&&m&y – &&&n&y ; 4 – 7 79 80 OPTIONS NONOTES NOSTIMER NOSOURCE NOSYNTAXCHECK; 90

    – 

  • So what is the question?

    – 

  • but not sure whether correct or not

    – 

  • What will following dereferencing evaluate to i. &&&m&y – &&&n&y ii. &&&m&x&&&n&y

    – 

You can just run it to find the answer.

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

When the macro processor sees && it translates it to & and leaves a reminder to itself that it will need to make another pass over the result to further resolve macro variable references. So on the fist pass of &&&m&y it converts && to & and &m to x and &y to 3. Then when it goes back a processes the result it converts &x3 to 4.

You can turn on SYMBOLGEN and see it in action.

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

Leave a Comment