SAS macros use parameters to define and call them. Default values can be set for some parameters. But what if you want a macro that can handle a variable number of arguments? ParmBuff is the solution for Handling Variable Arguments in SAS Macros.
Have you ever heard of ParmBuff? It’s a pretty cool system option for SAS macro programming that not many people know about. With ParmBuff, you get a lot of flexibility that can help you do some pretty amazing things with macros. In this post, we’ll go over some real-world examples to help you understand ParmBuff better. Trust us, you’re going to be impressed!
How to use Parambuff option for Handling Variable Arguments in SAS Macros
The ParmBuff
option is a SAS Macro Language feature that captures all macro parameters as a single text string. This enables Handling Variable Arguments in SAS Macros
Basic Syntax
The basic syntax for using ParmBuff
is as follows:
%macro my_macro (parm1, parm2, / parmbuff);
Key Components
parm1, parm2
: Standard macro parameters/
: Separator that indicates the start ofParmBuff
parmbuff
: The option itself
The Parameter Buffer and SYSBUFF variable
Parameter buffer, which is a temporary storage location in memory. When a macro is defined with the PARMBUFF option, SYSPBUFF resolves to the text provided as parameter values during invocation.
Note: This text includes the parentheses and commas.
Using the PARMBUFF option and SYSPBUFF, you can define a macro that accepts a varying number of parameters at each invocation.
To make things clearer, let’s check out the code below. It’s a simple macro %print with the Parmbuff Option used in the %Macro Statement.
%macro print/parmbuff;
%put Syspbuff contains: &syspbuff;
%mend print;
%print(sashelp.class,sashelp.class)
Later, we print the content in the parameter buffer in the log. When you see the log, you’ll notice that SAS prints the value within parentheses(sashelp.class,sashelp.class)
. Just remember, this is an important point to keep in mind.
Syspbuff contains: (sashelp.class,sashelp.class)
Here is an example of how to print SAS datasets passed in a macro variable.
%macro printz/parmbuff;
%put Syspbuff contains: &syspbuff;
/* Remove parentheses and spaces */
%let syspbuff = %sysfunc(compress(&syspbuff, %str( () )));
%put &=syspbuff;
%let num_values = %sysfunc(countw(%quote(&syspbuff), %str(,) )) ;
%put &=num_values;
/*Loop through each values*/
%do i=1 %to &num_values;
%let next_value = %scan(%quote(&syspbuff), &i, %str(,));
%put &next_value;
%end;
%mend printz;
%printz(sashelp.cars, sashelp.class, sashelp.heart)
Here is the log:
Syspbuff contains: (sashelp.cars,sashelp.class,sashelp.heart) SYSPBUFF=sashelp.cars,sashelp.class,sashelp.heart NUM_VALUES=3 sashelp.cars sashelp.class sashelp.heart
In this code, I have created a SAS macro named print
s that takes a variable number of arguments. Here’s a step-by-step explanation of what this code does:
%macro print/parmbuff;
: This line defines the start of theprint
macro and specifies that it accepts parameters.%put Syspbuff contains: &syspbuff;
: This line uses the%put
macro to display a message in the SAS log. It shows the initial value of thesyspbuff
macro variable, which contains the arguments passed to the macro.%let syspbuff = %sysfunc(compress(&syspbuff, %str( () )));
: This code uses the%compress
function within%sysfunc
to remove spaces and parentheses from thesyspbuff
variable. This step cleans up the input.%put &=syspbuff;
: After cleaning, this line uses%put
to display the cleanedsyspbuff
variable in the SAS log.%let num_values = %sysfunc(countw(%quote(&syspbuff), %str(,) ));
: This code counts the number of values in the cleanedsyspbuff
variable using%countw
. It calculates the number of arguments passed to the macro.%put &=num_values;
: The number of values is displayed in the SAS log.%do i = 1 %to &num_values;
: This is the start of a loop that iterates through the values insyspbuff
.%let next_value = %scan(%quote(&syspbuff), &i, %str(,));
: In each iteration, the%scan
function is used to extract the next value fromsyspbuff
, and%quote
– a macro quoting function ensures the commas are masked.%put &next_value;
: Thenext_value
is displayed in the SAS log during each iteration, effectively printing each dataset name one by one.%end;
: This marks the end of the loop.
In summary, this code defines a macro (print
) that takes a variable list of dataset names as input using the Parambuff feature, cleans the input, counts the number of values, and then iterates through and prints each dataset name individually. It’s a useful example of how to work with a variable number of macro parameters in SAS.
Our article on passing commas in SAS macros is available for you to read.
Conclusion
The ParmBuff option in SAS Macro Language is a powerful tool for handling variable arguments in SAS macros. So, the next time you find yourself constrained by the rigidity of standard parameter passing, remember that ParmBuff
is your gateway to a more dynamic and efficient coding experience.