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.
Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.
Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.
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.