Let’s continue to take a look at SAS Macro Facility. This post will focus on macro programs. You can read one of my previous posts to see the details about SAS macro varlables.

Components of SAS macro program

Here is the syntax for sas programs. It starts with %MACRO program-name and ends with %MEND statement. Arguments enclosed in angle brackets are optional. Therefore, parameter-list and option are not reuired when building sas macros. Here please pay attention to the fact that program-name is not required in %MEND statement but required for %MACRO statement. Though parameter-list is optional, it is very important if you want to pass values to macro program. It is macro parameters which can enable you to do different (though similar) work such to run macro program on different data without changing macro program. Fundamentally, macro parameters will be treated as macro variable in macro program.

How to develop macro programs?

As we all know that, the most important feature of macro program is to do repeatitive work. Before starting to think how to develop macro, we have to create code which can at least do one of those repeatitve work. Suppose that we want to get summary statistics of HEIGHT and WEIGHT for different age group within each gender population from SASHELP.CLASS. First of all, we can write code to compute summary statistics of HEIGHT. Code in below figure can get statistics such mean, std, min, max and median for female students at each of ages 11, 12, 13, 14 and 15.

Now let’s add %MACRO program (parameter-list) statement and %MEND statement.
Suppose that we give this macro a name – dist and give this macro two parameters: sex and var. Macro references &sex and &var should be used to replace sex and height, respectively. So far, a macro program named dist have been built successfully.

How to execute a macro program?

We can execute a macro program by sumitting a reference to the macro program. The syntax is %program-name. You only need to place a percent sign (%) before macro program name. If the macro have parameters, we still have to include parameter list when executing macro program. Let’s use our above marco program as example. In below figure, we call macro dist four times. If you submit below code, you will find that you can get 4 different datasets having the same name (dist). Each of them contains summary statistics for students meeting criteria listed in parameter list. For example, if you submit the first one %dist(sex = F, var = height);, you will get the same dataset as that shown in second part (How to develop macro programs). In another word, you will get summary statistics of height for female students. %dist(sex = F, var = weight); will enable SAS to produce statistics of weight for female students.

Two types of macro parameters: positional and keyword

For positional macro parameters, there is a one-to-one correspondence between the list of parameters on %MACRO statement and the values of parameters on the macro program call. The left part of below figure shows you how to build macro using positional parameters and how to call the macro. Here I have to remind you that if you don’t follow the rule of the one-to-one correspondence, SAS cannot compile your program successfully. For example, if you call the program using %dist(height, F);, SAS macro will replace macro reference &sex with height and that’s why your program will fail. This approach is error-prone, right?
As for the keyword parameters, there must be an equal sign (=) following each parameter name and there is not necessary to have a one-to-one relationship between paramters on %MACRO statement and values of parameters on macro call statement. The right panel in below figure shows you how to create macro using keyword parameter. You can see that the position of parameters in %MACRO statement and macro call statement can be different.
We can also use a mix of positonal parameter and keyword parameter to develop macro programs. Since positional parameter is error-prone and I strongly recommend you to use keyword paramter only in daily programming life. And thus how to create macro using a mix of positional parameter and keyword parameter will not be described here.

option MCOMPILENOTE to display compile status in log

mcompilenote can enable SAS to display notes about macro program compilation in SAS log.
There are three values available for mcompilenote option: NONE, NOAUTOCALL and ALL. NONE can suppress display of all macro program compilation notes. NOAUTOCALL can suppress compilation notes for autocall macro programs. ALL can enable SAS to display compilation notes for all macro programs. When you set mcompilenote to be all and place the statement before you macro program, you will get the compile note (successful or not) in the log. If there are errors in your macro program, SAS will print ERROR message in sas log so that you can correct your mistakes. For example, in the right part of below figure, you can see that to should be replaced with %to according to SAS message in the log.

options SYMBOLGEN, MPRINT, MLOGIC to display message about macro program processing in log

symbolgen can enable SAS to write results of resolving macro variables to the SAS log. For example, macro reference &sex was resolved to F while macro reference &var was resolved to height in below figure. If you want to see the sas code that macro processor constructs and submits, you have to enable mprint option. Those are not enclosed in squares are the code constructed and summitted by macro processor. mlogic option can trace the execution of macro programs. With the help of these options, you can debug your macro programs easily. And you can disable these options using nosymbolgen, nomprint, nomlogic.

Global macro variables and Local macro variables

The macro variables that can be referenced are stored in macro symbol tables. There are two types of macro symbol tables: global and local. Macro variables stored in global symbol table are global macro variables while those stored in local symbol table are local macro variables. You can reference global macro variables throughout your SAS session from both open code and within macro programs. However, local macro variables can only be resolved from within the macro program that define them.

Global macro variables

Here shows you how to add macro variables to the global macro symbol table. One way is to create macro variables in open code. Another one is to apply CALL SYMPUTX or CALL SYMPUT in data step. If your macro program creates macro variables and does not add this macro variable to global macro symbol table using %global statement, SAS will treat these macro variables as local macro variables. In another word, %global statement is the third approach that can be used to create global macro variables. For example, in the right panel of below figure, macro variable reference &age can not be resolved in PROC MEAN step if we don’t place %global age; before subset macro program. Of course, you can also place %global age; within subset macro and before %let age = 11;.

Local macro variable

%local statement can be used to create local macro variables. As I described previously, macro variables defined within a macro program will be local macro variables. Here is an example which can make good demonstration of differences between local macro variable and global macro variable. In data step, &age was resolved to be 11 which is value of local macro variable. While &age in PROC PRINT step was resolved to be 12 which is value of global macro variable. You can also apply %put _global_ and %put _local_ to check whether the macro variable is in local macro symbol table or global symbol table. From below figure, you can see that there are two macro variables named age. one is stored in global symbol table and anther is stored in local symbol table.

%PUT statement

The %PUT statement can enable SAS to write text and macro variable values to the SAS log. %PUT statement is maro language statement and cannot be part of DATA step nor PROC step unless the DATA step and PROC step is within a macro program. Here shows you the syntax for %PUT statement and detailed explanation for commonly used options.

Like the post? Welcome to share or you can subscribe to get latest post. How to subscribe? Go to the top-right corner of this web page to submit your name and email address. If you cannot receive emails from us, please check your spam/junk folder.