BookmarkSubscribeRSS Feed
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   )





is this possible?   thx a lot.

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  

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

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.

Fluorite | Level 6

Actually, this question originates from this:




Really desparately crasy...

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



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.


Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.


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
  • 4 in conversation