DATA Step, Macro, Functions and more

Basic SAS %macro question

Reply
Contributor
Posts: 51

Basic SAS %macro question

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?

Super User
Posts: 22,874

Re: Basic SAS %macro question

Posted in reply to Wafflecakes

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

Super User
Posts: 9,611

Re: Basic SAS %macro question

Posted in reply to Wafflecakes

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)
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Contributor
Posts: 51

Re: Basic SAS %macro question

Posted in reply to KurtBremser

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

Super User
Posts: 9,611

Re: Basic SAS %macro question

Posted in reply to Wafflecakes

Wafflecakes wrote:

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


Use more macro parameters.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Contributor
Posts: 51

Re: Basic SAS %macro question

[ Edited ]
Posted in reply to KurtBremser

%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)

Contributor
Posts: 51

Re: Basic SAS %macro question

Posted in reply to Wafflecakes
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.
Super User
Posts: 9,611

Re: Basic SAS %macro question

Posted in reply to Wafflecakes

Your first version to call the macro won't work, as single digits are not valid as dataset names; your second version will work.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Contributor
Posts: 51

Re: Basic SAS %macro question

Posted in reply to KurtBremser
I got some output but the numbers are off.
Super User
Posts: 9,611

Re: Basic SAS %macro question

Posted in reply to Wafflecakes

If proc reg doesn't give you the results you expect, you should post that as question in the stat proc forum.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Contributor
Posts: 51

Re: Basic SAS %macro question

Posted in reply to KurtBremser
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?
Super User
Posts: 9,611

Re: Basic SAS %macro question

Posted in reply to Wafflecakes

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.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Contributor
Posts: 51

Re: Basic SAS %macro question

Posted in reply to KurtBremser
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;
Super User
Posts: 9,611

Re: Basic SAS %macro question

Posted in reply to Wafflecakes

Look at the log; you'll see which dataset is created by proc reg, and which one is used in proc print.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Super User
Posts: 22,874

Re: Basic SAS %macro question

Posted in reply to Wafflecakes

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. 

Ask a Question
Discussion stats
  • 14 replies
  • 237 views
  • 0 likes
  • 3 in conversation