Handling Variable Arguments In SAS Macros

Handling Variable Arguments in SAS Macros

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 of ParmBuff
  • 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 prints that takes a variable number of arguments. Here’s a step-by-step explanation of what this code does:

  1. %macro print/parmbuff;: This line defines the start of the print macro and specifies that it accepts parameters.
  2. %put Syspbuff contains: &syspbuff;: This line uses the %put macro to display a message in the SAS log. It shows the initial value of the syspbuff macro variable, which contains the arguments passed to the macro.
  3. %let syspbuff = %sysfunc(compress(&syspbuff, %str( () )));: This code uses the %compress function within %sysfunc to remove spaces and parentheses from the syspbuff variable. This step cleans up the input.
  4. %put &=syspbuff;: After cleaning, this line uses %put to display the cleaned syspbuff variable in the SAS log.
  5. %let num_values = %sysfunc(countw(%quote(&syspbuff), %str(,) ));: This code counts the number of values in the cleaned syspbuff variable using %countw. It calculates the number of arguments passed to the macro.
  6. %put &=num_values;: The number of values is displayed in the SAS log.
  7. %do i = 1 %to &num_values;: This is the start of a loop that iterates through the values in syspbuff.
  8. %let next_value = %scan(%quote(&syspbuff), &i, %str(,));: In each iteration, the %scan function is used to extract the next value from syspbuff, and %quote– a macro quoting function ensures the commas are masked.
  9. %put &next_value;: The next_value is displayed in the SAS log during each iteration, effectively printing each dataset name one by one.
  10. %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.

Every week we'll send you SAS tips and in-depth tutorials

JOIN OUR COMMUNITY OF SAS Programmers!

Subhro

Subhro provides valuable and informative content on SAS, offering a comprehensive understanding of SAS concepts. We have been creating SAS tutorials since 2019, and 9to5sas has become one of the leading free SAS resources available on the internet.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.