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-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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.

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
  • 2873 views
  • 0 likes
  • 4 in conversation