BookmarkSubscribeRSS Feed
Laoban_James
Fluorite | Level 6

Can I call/execute macro within a DO loop?

 

something like

 

data TWO; set ONE;
  do until(last.ID)

     %macro( some variable in TWO,  ONE,   maybe another dataset   )

end;
run;

 

 

 

is this possible?   thx a lot.

5 REPLIES 5
Reeza
Super User

Sort of, you need to use a routine to call the macro - routine is CALL EXECUTE

 

Generate the text and pass it to call execute  

Kurt_Bremser
Super User

You do not "execute" the macro in the loop. Instead, during the data step compilation phase, the macro is resolved, and the resulting text is inserted as code in the loop, in place of the macro call. Once compilation has finished successfully (meaning the resulting data step code is syntactically correct), the whole code is executed; at data step runtime, no macro elements are therefore present, having been resolved beforehand.

 

If you use call execute, be aware that the data step feeds then code into the main SAS interpreter's input queue, and this code is dealt with after the data step has finished. Also be aware that macro references in a call execute will once again be resolved before the data step starts, unless you prevent that by proper quoting or other means (eg reading macro names from a dataset).

RW9
Diamond | Level 26 RW9
Diamond | Level 26

What are you trying to do, post test data, in the form of a datastep - and the output you are looking for.  The question is too general as it is, yes you could have that macro there, if it generates valid code for its place, yes you could call execute - again if the macro is valid.  But we don't know anything around the code, so can't advise.

Laoban_James
Fluorite | Level 6

Actually, this question originates from this:

 

https://communities.sas.com/t5/SAS-Procedures/How-to-run-a-rolling-regression-with-two-datasets/m-p/...

 

 

 

Really desparately crasy...

RW9
Diamond | Level 26 RW9
Diamond | Level 26

Well, I don't really know what the "regression" part is, am not a statistician, so can't help there.  Call execute is used to generate code, it won't speed anything up for you.  You will still need to select your data, then run your procedure on it.  If it is taking a long time, then consider subsetting, or aggregating your data first.  I.e.

data test_block1;

  set total (where=(id=1));

run;

 

proc reg...

 

data test_block2;

  ...

...

 

Call execute can be used to generate the code per row of your data, but all that means is there will be a vast amount of code all running quickly, rather than a small bit of code running slowly, which will probably end up being more or less the same.  SAS procedures and datasteps are optimised, so the more grouping you can do and in the fewer datasteps the better, using datastep rather than SQL might also reduced the resource usage.

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

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
  • 5 replies
  • 3486 views
  • 0 likes
  • 4 in conversation