BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Ilgar
Calcite | Level 5

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;

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

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);

View solution in original post

5 REPLIES 5
Reeza
Super User

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);

Ilgar
Calcite | Level 5

Thank you so much, guys!

Reeza, your code worked fine.

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.

Quentin
Super User

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.

BASUG is hosting free webinars Next up: Jane Eslinger presenting PROC REPORT and the ODS EXCEL destination on Mar 27 at noon ET. Register now at the Boston Area SAS Users Group event page: https://www.basug.org/events.
Ilgar
Calcite | Level 5

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

art297
Opal | Level 21

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)

sas-innovate-2024.png

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.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 5 replies
  • 791 views
  • 6 likes
  • 4 in conversation