BookmarkSubscribeRSS Feed
smeet
Calcite | Level 5

Hello. I'd like to run a macro function over every observation of a variable in a dataset. My code (which doesn't work) looks like this:

 

DATA _NULL_;
	SET Work.HAVE;
	%MacroFunction(Var1);
RUN;

where %MacroFunction is a macro defined earlier in my code that takes one argument. This %MacroFunction creates an external .txt file for each entry under Var1.

 

Running just %MacroFunction(Observation1) in open code works perfectly, but since I'd like to run this for every observation under Var1, I'd prefer not to write %MacroFunction(Observation1), %MacroFunction(Observation2), etc., especially when the number of observations may change with each run.

4 REPLIES 4
PaigeMiller
Diamond | Level 26

It would help if you showed us the macro function eponymously named %MacroFunction

 

In general, putting a macro function inside a data step requires there to be valid, legal, working SAS data step syntax when the macro function executes and generates text.

 

So this simple example won't work:

 

%macro macrofunction;
proc means data=sashelp.class; run;
%mend;

data abc;
    set sashelp.class;
    new_variable=%macrofunction;
run;

 

Why? Because you cannot embed PROC MEANS inside a SAS data step code, this is not valid working legal SAS data step code.

 

At this link, I provide a macro that does work inside a data step, because it generates valid working legal data step code. https://communities.sas.com/t5/SAS-Programming/How-to-Calculate-Death-Rates-in-One-Statement-in-a-DA...

Why does it work? Because the macro resolves to legal valid working SAS data step code, when the value of &string is C the code becomes sum_c + (cause=:"C"); and this is valid in a data step.

 

 

--
Paige Miller
Reeza
Super User

You may want either CALL EXECUTE or PROC FCMP instead, depends on the code as stated by others.

Quentin
Super User

It's hard to know without seeing your macro, but it sounds like you are trying to use a dataset to "drive" macro calls. 


One approach to this is using DOSUBL or CALL EXECUTE.  This thread has some examples: https://communities.sas.com/t5/SAS-Programming/Iteratively-pass-each-row-of-a-data-file-to-a-macro/m...

The Boston Area SAS Users Group (BASUG) is hosting our in person SAS Blowout on Oct 18!
This full-day event in Cambridge, Mass features four presenters from SAS, presenting on a range of SAS 9 programming topics. Pre-registration by Oct 15 is required.
Full details and registration info at https://www.basug.org/events.
Tom
Super User Tom
Super User

The is not such thing as a macro "function".  

 

The closest thing would be a function style macro that generates only part of a SAS statement so that you could call it in the middle of a SAS statement.

 

And the way you are calling the macro you couldn't use such a function style macro anyway since your code would only work if the macro generates one or more complete STATEMENTS.  But only statements that could be part of a data step.  Such as assignment statements or IF THEN statement or perhaps LABEL or FORMAT statements.

 

 

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 4 replies
  • 570 views
  • 1 like
  • 5 in conversation