BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
SeanZ
Obsidian | Level 7

Hi there, I have a basic question (maybe). I am a new learner for SAS macro. I try to define some optional parameters that I may use in the macro or may not. But I am not sure how to do it. Specifically,

 

%macro prog (infile, output, p1, p2, o1, o2)

 

in the above macro, I would like to input infile name, output name, parameter 1 and 2 (p1 and p2), and with two optional parameters o1 and o2. My purpose is to use them if I call them (by including them in the program), while not use them if I omit them in the program. Please let me know how to do it. thanks.

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

So given you definition (%macro prog(infile, output, p1, p2, o1, o2); ... %mend; ) you could call it with any number of those parameter given a value.

%prog ('myfile.csv', mydata, AGE, GENDER,AGE<15,GENDER='M'); 
%prog ('myfile.csv', mydata, AGE, GENDER);
%prog ('myfile.csv', mydata,,,AGE<15,GENDER='M'); 

But also note one nice thing about SAS macros is that even though you have defined the macro with position arguments you can still specify the values by name.

%prog 
(C2=GENDER='M'
,INFILE='myfile.csv'
,C1=AGE<15
,OUTPUT=mydata
,P1=AGE
,P2=GENDER
) ;

Now inside the macro definition you need to figure out if you need do something different in the code generation based on whether the user has supplied a value for a particulr parameter.  For example C1 might be used as the condition to apply in a WHERE statment.  So you might code it this way.

%if %length(&c1) %then %do;
  where &c1 ;
%end ;

 

View solution in original post

2 REPLIES 2
Tom
Super User Tom
Super User

So given you definition (%macro prog(infile, output, p1, p2, o1, o2); ... %mend; ) you could call it with any number of those parameter given a value.

%prog ('myfile.csv', mydata, AGE, GENDER,AGE<15,GENDER='M'); 
%prog ('myfile.csv', mydata, AGE, GENDER);
%prog ('myfile.csv', mydata,,,AGE<15,GENDER='M'); 

But also note one nice thing about SAS macros is that even though you have defined the macro with position arguments you can still specify the values by name.

%prog 
(C2=GENDER='M'
,INFILE='myfile.csv'
,C1=AGE<15
,OUTPUT=mydata
,P1=AGE
,P2=GENDER
) ;

Now inside the macro definition you need to figure out if you need do something different in the code generation based on whether the user has supplied a value for a particulr parameter.  For example C1 might be used as the condition to apply in a WHERE statment.  So you might code it this way.

%if %length(&c1) %then %do;
  where &c1 ;
%end ;

 

FreelanceReinh
Jade | Level 19

Hi @SeanZ,

 

As a minor addition to what Tom has written: You can also specify default values to parameters in the %MACRO statement, which will be used if the respective parameter is not specified in the macro call.

 

Example:

%macro prog(infile, output, p1, p2, o1=365, o2=C:\Temp);

In Tom's example of a WHERE condition you could write c1=1, i.e. use 1 as the default value. "where 1;" (after resolution of macro code) is a valid statement and 1 is always true, so you could safely omit the %LENGTH check.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 2 replies
  • 20690 views
  • 2 likes
  • 3 in conversation