Good day all,
Suppose I have a sas macro like so...
%macro test
proc reg data=testing;
model y=x1 x2 x3;
output out=testing2
run;
proc print data=testing2;
var x1 x2 x3
run;
%mend test;
I need to call the macro many, many times, but the 'testing2' output dataset is different each time I call it. How do I make this change?
Your code has errors. I'm going to assume it's sample/representative code.
Add a parameter to your macro definition that provides the data set name and then call the macro multiple times using CALL EXECUTE.
Or build an automatic DO loop into your macro.
There are examples of either here:
https://stats.idre.ucla.edu/sas/seminars/sas-macros-introduction/
or search Lexjansen.com
You only use a macro if you need dynamic code. There is nothing dynamic in your example code.
IF there is something in your real code that needs to be made flexible, you can use the parameters used for that to name the output datasets in some way.
Suppose this code
proc reg data=testing;
model y=x1;
output out=testing1;
run;
proc print data=testing1;
var x1;
run;
has to be made flexible, then you would do this:
%macro test(param);
proc reg data=testing;
model y=x¶m.;
output out=testing¶m.;
run;
proc print data=testing¶m.;
var x¶m.;
run;
%mend;
%test(1)
%test(2)
%test(3)
Kurt - what if more thna 1 parameter needs to be made flexible?
@Wafflecakes wrote:
Kurt - what if more thna 1 parameter needs to be made flexible?
Use more macro parameters.
%macro test(par, param);
proc reg data=testing;
model y=x;
where test=&par
output out=¶m.;
run;
proc print data=¶m.;
run;
%mend;
%test(1,1)
%test(2,2)
%test(3,3)
Is this correct?
Alternatively....
%test(1, linear1)
%test(2, linear2)
%test(3, linear3)
Your first version to call the macro won't work, as single digits are not valid as dataset names; your second version will work.
If proc reg doesn't give you the results you expect, you should post that as question in the stat proc forum.
@Wafflecakes wrote:
when you put ¶m and call %test(1,linear1), does the name of the dataset get called linear1 where ¶m first shows up and gets used in the subsequent proc print step as linear1?
If I read you right, yes. Use options symbolgen mprint mlogic; to show all macro activity in the log.
Look at the log; you'll see which dataset is created by proc reg, and which one is used in proc print.
@Wafflecakes wrote:
does it matter if the proc print step is actually a data step: data ¶m; set ¶m;
What? That doesn't make a lot of sense.
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!
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.