BookmarkSubscribeRSS Feed
deleted_user
Not applicable
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
6 REPLIES 6
Flip
Fluorite | Level 6
The run puts the macro outside the datastep. Just pass the variable name string to the macro.

data files;

...
...
%MyMacro(optemp1);
run;
deleted_user
Not applicable
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.
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
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.
kdp
Calcite | Level 5 kdp
Calcite | Level 5
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!
deleted_user
Not applicable
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
Peter_C
Rhodochrosite | Level 12
Neel

have a look at the function RESOLVE

good luck

PeterC

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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