Help using Base SAS procedures

Executing macro for each observation in dataset

Reply
N/A
Posts: 0

Executing macro for each observation in dataset

Hi All,

I have a macro(MyMacro) which I need to execute for a variable in each observation of the dataset.

So I have a data set block like below...

data files;

...
...

call symputx('folderPath',optemp1);

run;

%MyMacro(&folderPath);
run;


The Macro seems to be executing for only one observation(I have 4 observations in the files data set). This may be because of the run statement I have after symputx statement. But without that run statement, symputx does not execute and hence folderPath does not get resolved.

Any suggestions how to handle this.

Thanks,
Neel
Super Contributor
Posts: 359

Re: Executing macro for each observation in dataset

The run puts the macro outside the datastep. Just pass the variable name string to the macro.

data files;

...
...
%MyMacro(optemp1);
run;
N/A
Posts: 0

Re: Executing macro for each observation in dataset

Thanks.

But if I invoke macro directly with variable name like

%MyMacro(optemp1);

the macro resolves its argument to optemp1 instead the value in optemp1 & hence fails.
Super Contributor
Super Contributor
Posts: 3,174

Re: Executing macro for each observation in dataset

You say your program "fails" but it's not been clearly stated as to what fails.

It would be best served if you shared the SAS log information where you get the erorr and include expanded SAS code for subscribers / visitors to see -- also explain what the DATA step is attempting to accomplish in your words.

And, add to your program for the most expanded info possible (maybe this will also help with self-debugging?):

OPTIONS SOURCE SOURCE2 MACROGEN SYMBOLGEN;

It is possible to invoke a macro multiple times so that it is expanded / resolved within a DATA step (that is between a DATA statement and a RUN statement).

Scott Barry
SBBWorks, Inc.
Contributor kdp
Contributor
Posts: 31

Re: Executing macro for each observation in dataset

I have code where I am doing the same thing you are trying to do. Try this and see if it works:

data files;
set olddataset;
....
call execute('%MyMacro('||folderPath||');');
run;

This way you don't have to create a macro variable and then pass it on to the macro, it will just take the variable in your old data set and pass that on to the macro.

Also, instead of creating a new data set called "files", you can just call it "_null_" which won't create a new data set but still run the macro 4 times.

Hope it helps!
N/A
Posts: 0

Re: Executing macro for each observation in dataset

Using call execute statement may cause different bugs
use this:

/*prepare*/
proc sql noprint;
select '%MyMacro(' || folderPath || ')' into : mvRun separated by ';'
from lib.dataset;
quit;

/*execute*/
&mvRun


Very simple =) Just remember, macro variable length is limited by 65k symbols
Valued Guide
Posts: 2,175

Re: Executing macro for each observation in dataset

Neel

have a look at the function RESOLVE

good luck

PeterC
Ask a Question
Discussion stats
  • 6 replies
  • 373 views
  • 0 likes
  • 5 in conversation