Solved
New Contributor
Posts: 3

# Multiple Macro Variables

Hi,

I have a long list of macro variables that need to be updated periodically. The values always increase in increments of 7. How can I make this more efficient? So, instead of updating this list manually (I have several other lists that are much longer), can I assign the 1st value and then have a DO loop do the job? I tried writing a Macro, but failed each time.

thank you

%LET WK1 = 19392;

%LET WK2 = 19399;

%LET WK3 = 19406;

%LET WK4 = 19413;

%LET WK5 = 19420;

----------------

%LET WK27 = 19574;

Accepted Solutions
Solution
‎02-22-2013 11:21 AM
Super User
Posts: 23,771

## Re: Multiple Macro Variables

You can write a loop in a data _null_ step or change your current code to feed off the first variable.

1.

data _null_;

start=19392;

do i=1 to 27;

call symput("wk"||strip(put(i, 8.)), start+(i-1)*7);

end;

run;

2.

%let wk1=19392;

%let wk2=%eval(&wk1+7);

%let wk3=%eval(&wk2+7);

All Replies
Solution
‎02-22-2013 11:21 AM
Super User
Posts: 23,771

## Re: Multiple Macro Variables

You can write a loop in a data _null_ step or change your current code to feed off the first variable.

1.

data _null_;

start=19392;

do i=1 to 27;

call symput("wk"||strip(put(i, 8.)), start+(i-1)*7);

end;

run;

2.

%let wk1=19392;

%let wk2=%eval(&wk1+7);

%let wk3=%eval(&wk2+7);

New Contributor
Posts: 3

## Re: Multiple Macro Variables

Thank you so much, guys!

Arthur, the macro did not work.  I am getting an error message. Does it work when you test it? I would very much like to make it work thru Macro. thanks again.

PROC Star
Posts: 1,460

## Re: Multiple Macro Variables

Hi,

Art's macro works for me (log below).  What error are you getting?

One think to keep in mind is the *scope* of macro variables.

When you call the macro below, it creates local macro variables that will only exist while %buildit is executing (assuming that the macro variables do not already exist in an outer scope).

So if you want the macro variables to be available outside of the macro, you may want to define them as %global, or create them in the outer scope. To make them global, add %global wk&i; inside the %do loop, before the %LET statement. But global macro variables can become problematic/difficult to manage, as they exist for the entire session.  May be safer to take Art's do loop and move it inside of the macro you are building.

```85   %macro buildit(start);
86     %do i=1 %to 27;
87       %LET WK&i = %eval(&start.+(&i-1)*7);
88       %put WK&i=&&Wk&i;
89     %end;
90   %mend buildit;
91
92   %buildit(19392)
WK1=19392
WK2=19399
WK3=19406
WK4=19413
WK5=19420
WK6=19427
WK7=19434
WK8=19441
WK9=19448
WK10=19455
WK11=19462
WK12=19469
WK13=19476
WK14=19483
WK15=19490
WK16=19497
WK17=19504
WK18=19511
WK19=19518
WK20=19525
WK21=19532
WK22=19539
WK23=19546
WK24=19553
WK25=19560
WK26=19567
WK27=19574
```

HTH,

--Q.

New Contributor
Posts: 3

## Re: Multiple Macro Variables

Thanks, Quentin! %Gobal was the key. Now the macro works!

PROC Star
Posts: 8,165

## Re: Multiple Macro Variables

You could use Fareeza's approach but, since you mentioned that you were trying to do it with a macro, you could use something like:

%macro buildit(start);

%do i=1 %to 27;

%LET WK&i = %eval(&start.+(&i-1)*7);

%end;

%mend buildit;

%buildit(19392)

🔒 This topic is solved and locked.