DATA Step, Macro, Functions and more

Can I call/execute macro within a DO loop?

Reply
Occasional Contributor
Posts: 12

Can I call/execute macro within a DO loop?

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.

Super User
Posts: 17,808

Re: Can I call/execute macro within a DO loop?

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
Posts: 6,936

Re: Can I call/execute macro within a DO loop?

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

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Super User
Super User
Posts: 7,401

Re: Can I call/execute macro within a DO loop?

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.

Occasional Contributor
Posts: 12

Re: Can I call/execute macro within a DO loop?

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

Super User
Super User
Posts: 7,401

Re: Can I call/execute macro within a DO loop?

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.

Ask a Question
Discussion stats
  • 5 replies
  • 481 views
  • 0 likes
  • 4 in conversation