DATA Step, Macro, Functions and more

reference macro from loop

Reply
Contributor
Posts: 60

reference macro from loop

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?

Frequent Contributor
Posts: 95

Re: reference macro from loop

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

Super Contributor
Posts: 1,636

Re: reference macro from loop

%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

Super User
Super User
Posts: 6,502

Re: reference macro from loop

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.

Ask a Question
Discussion stats
  • 3 replies
  • 285 views
  • 0 likes
  • 4 in conversation