Lesson 9, Part 6: Macro Arrays (Indirectly Referencing Macro Variables)¶
Dynamic code substitution¶
- Create six macro variables with a common prefix and a numeric suffix with an increasing number for each macro variable (in OPEN CODE- i.e., outside of the macro).
During the execution of the %DO-%END LOOP within the macro,
* a new macro variable “I” is created.
* two &s in front of the macro variable name (&&disease&i)
When the macro %REF is executed, SAS goes through 5 iterations and resolves
&&disease&i to &disease1 in the first scan &disease1 to cvd in the second scan (1st iteration).
&&disease&i to &disease2 in the first scan &disease2 to cancerin the second scan (2nd iteration).
&&disease&i to &disease3 in the first scan &disease3 to stroke in the second scan (3rd iteration).
&&disease&i to &disease4 in the first scan &disease4 to hbp in the second scan (4th iteration).
&&disease&i to &disease5 in the first scan &disease5 to diabetes in the second scan (5th iteration).
The word scanner hands the macro processor the first 2 tokens - &&
- && resolves to & which is placed back in the input stack
The word scanner hands the macro processor the next token - disease
- disease resolves to disease which is placed back in the input stack
The word scanner hands the macro processor the next 2 tokens – &i
- &i resolves to 1 which is placed back in the input stack
Now the input stack has &disease1 ** The word scanner hands the macro processor these 2 tokens which resolve to cvd
*Ex7_indirect_reference_1.sas;
options nodate nonumber nonotes nosource;
ods html close;
%macro ref;
ods html close;
options nonotes;
%LET disease1=cvd;
%LET disease2=cancer;
%LET disease3=stroke;
%LET disease4=hbp;
%LET disease5=diabetes;
%LET last_element=5;
%DO i = 1 %TO &last_element;
%put &&disease&i;
%END;
%mend ref;
%ref
The SAS System
cvd
cancer
stroke
hbp
diabetes
E3969440A681A2408885998500000005