BookmarkSubscribeRSS Feed
tom12122
Obsidian | Level 7

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?

3 REPLIES 3
Alpay
Fluorite | Level 6

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

Linlin
Lapis Lazuli | Level 10

%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

Tom
Super User Tom
Super User

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.

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 1676 views
  • 0 likes
  • 4 in conversation