BookmarkSubscribeRSS Feed
Wafflecakes
Calcite | Level 5

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?

14 REPLIES 14
Reeza
Super User

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

Kurt_Bremser
Super User

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&param.;
output out=testing&param.;
run;

proc print data=testing&param.;
var x&param.;
run;

%mend;

%test(1)
%test(2)
%test(3)
Wafflecakes
Calcite | Level 5

Kurt - what if more thna 1 parameter needs to be made flexible?

Wafflecakes
Calcite | Level 5

%macro test(par, param);
proc reg data=testing;
model y=x;
where test=&par
output out=&param.;
run;

proc print data=&param.;
run;

%mend;

%test(1,1)
%test(2,2)
%test(3,3)

 

Is this correct?

 

Alternatively....

%test(1, linear1)

%test(2, linear2)

%test(3, linear3)

Wafflecakes
Calcite | Level 5
Keep in mind the &par refers to the value of a variable in the dataset and &param refers to the name of an outputted dataset.
Wafflecakes
Calcite | Level 5
I got some output but the numbers are off.
Wafflecakes
Calcite | Level 5
when you put &param and call %test(1,linear1), does the name of the dataset get called linear1 where &param first shows up and gets used in the subsequent proc print step as linear1?
Kurt_Bremser
Super User

@Wafflecakes wrote:
when you put &param and call %test(1,linear1), does the name of the dataset get called linear1 where &param 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.

Wafflecakes
Calcite | Level 5
the proc reg part of the macro works but not the proc print - does it matter if the proc print step is actually a data step: data &param; set &param;
Reeza
Super User

@Wafflecakes wrote:
 does it matter if the proc print step is actually a data step: data &param; set &param;

What? That doesn't make a lot of sense. 

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!

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
  • 14 replies
  • 1351 views
  • 0 likes
  • 3 in conversation