DATA Step, Macro, Functions and more

how to define optional macro parameters?

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 122
Accepted Solution

how to define optional macro parameters?

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.

 

 


Accepted Solutions
Solution
‎04-06-2016 04:26 AM
Super User
Super User
Posts: 7,050

Re: how to define optional macro parameters?

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


All Replies
Solution
‎04-06-2016 04:26 AM
Super User
Super User
Posts: 7,050

Re: how to define optional macro parameters?

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 ;

 

Trusted Advisor
Posts: 1,117

Re: how to define optional macro parameters?

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.

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 2 replies
  • 2755 views
  • 1 like
  • 3 in conversation