I have data step with DO i=1 to 5 loop.
I need to reference macro variable & Ti (&T1 for i=1, &T2 for i=2 etc.)
How to do that?
You may use SYMGET to resolve the macro variable name at run time.
CATS() function will construct the name of the macro variable name as 'T1', 'T2', etc at run time.
SYMGET() function will get the value the macro variable produced by CATS() function (e.g., SYMGET('T1') when i=1).
Zafer
%let T1=A;
%let T2=B;
%let T3=C;
%let T4=D;
%let T5=E;
data _null_;
do i=1 to 5;
T = SYMGET(CATS('T',i));
put i= T=;
end;
run;
| 15 | %let T1=A; | ||
| 16 | %let T2=B; | ||
| 17 | %let T3=C; | ||
| 18 | %let T4=D; | ||
| 19 | %let T5=E; | ||
| 20 | |||
| 21 | data _null_; | ||
| 22 | do i=1 to 5; | ||
| 23 | T = SYMGET(CATS('T',i)); | ||
| 24 | put i= T=; | ||
| 25 | end; | ||
| 26 | run; |
i=1 T=A
i=2 T=B
i=3 T=C
i=4 T=D
i=5 T=E
%let t1=a1;
%let t2=a2;
%let t3=a3;
%let t4=a4;
%let t5=a5;
data want;
do i=1 to 5;
id=symget(cats('t',i));
output;
end;
run;
proc print;run;
Linlin
I am curious about why you would want a data step DO loop to reference a series of macro variables?
Wouldn't it be easier to restructure the code so that the macro variables are instead actual variables? Then you could use an array reference.
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
Still thinking about your presentation idea? The submission deadline has been extended to Friday, Nov. 14, at 11:59 p.m. ET.
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.