We often have to do repetitive programming work especially when developing table for summary of demographics. The only difference between one measurement and the next is the variable. In this case, we can use a SAS programming assistant – the SAS macro facility – to help us out. We will give you an overall summary of SAS macro facility in several posts including this post.

SAS Macro Facility and its components

The SAS macro facility is a tool for text subsititution. When macro processor encounters a macro reference, it will replace the reference with text associated with this macro reference. The SAS macro facility is consists of two main components: SAS macro variables and SAS macro programs.

Component 1: SAS Macro Variables

Suppose that we associate text F with a macro reference sex, macro processor will replace &sex with text F. By submiting code in below figure, you will get a output like that in the right panel. Only data about females will be printed. In this example, a macro variable sex is defined and used to select a subset of sashelp.class.

Component 2: SAS Macro Programs

Now let’s look at another example which shows you how to use Macro program to excute the same PROC PRINT on multiple subset datasets. In this example, we associate macro reference age with 12 and 13 respecitively. When following program executes, it submits PROC PRINT step two times. It replaces &age with each of the ages 12 and 13. From the right part of figure, you can see that two subset datasets were printed. Here &age is a reference for macro variable age while %print is an example reference to a macro program.
.

Advantages of SAS macro facility

By incorporating macro facility in your programs, you can accomplish repetitive tasks quickly and efficiently. The macro program can be reused many times without having to change the code within macro. What you only need to do is to change values of parameters. It can also provide a more modular structure to your programs. By applying reference to the macro program in your programs, your code will become shorter and much easier to read.

Two stypes of Macro variable

Generally speaking, there are two kinds of macro varaibles: automatic variables and user-defined macro variables. Each time you start a SAS session, automatic macro variables will be defined by SAS. These automatic variables remain available through out the whole SAS session. User-defined macro variables are defined by you for your own purpose and will be described in details in next section.

Automatic macro variables are used to store information about SAS session such as date on which the SAS session is started and version of SAS. Here lists a few automatic variables.

Type Automatic Macro Variable Description
Values that remain fixed SYSDATE Date the SAS session started in DATE7. format
SYSDATE9 Date the SAS session started in DATE9. format
SYSDAY Day of the week the SAS session started
SYSVER Release number of SAS that is executing
SYSTIME Time the SAS session started
Values that can be

changed by SAS

SYSERR Return code set at end of each DATA step and most PROC steps
SYSERRORTEXT Last error message generated within the SAS log
SYSFILRC Return code from most recent FILENAME statement
SYSLIBRC Return code from most recent LIBNAME statement
SYSMACRONAME Returns the name of the currently executing macro program
SYSRC Returns a value corresponding to an error condition
SYSWARNINGTEXT Text of the last warning message generated within the SAS log
Values that can be

changed by you or

SAS

SYSDSN Name of the most recently created dataset in two fields: WORK TEMP
SYSLAST Name of the most recently created dataset in one field: WORK.TEMP

Only those in bold are used most often in clinical trial industry. Here presents you how to see values of automatic macro variables using %put statement. The values will be printed in log after submiting the code.

Three common methods to define user-defined macro variable

There are three ways to define user-defined macro variable. The simpler approach is to use %let statement as shown in the first figure of this post. Another one to use CALL SYMPUT or CALL SYMPUTX statement in data step. The last one is to use PROC SQL. Below figure shows you how to create the macro variable via these three methods. You can see that CALL SYMPUTX can strip both leading and trailing blanks while CALL SYMPUT does not trim leading and trailing blanks. And there are leading blanks in PROC SQL-created macro variables. We can use separated by ‘’ or trimmed to remove blanks.

Macro variable values are text values

All values including numbers assigned to macro variables are treated as text values. We cannot do calculations with macro variables without telling the macro processor to consider text values as numbers using macro fuctions such as %EVAL and %SYSEVALF. The %EVAL function does integer arithmetic while %SYSEVALF can deal with float number arithmetic. If you look at top part of figure in above section (Three common methods to define user-defined macro variable), you will find that macro reference &z is resolved to be 12 + 13 instead of 25.

Macro variable reference enclosed within single quotation marks is not resolved

If we want a a macro variable’s value to be included as part of a literal sting, we must enclose the string with doulbe quotation marks. Following program code create a macro variable total to store the total number of female students. And you can see that macro variable total reference is resolved when enclosed with doulbe quotation marks. But if it is enclosed in single quotation marks, it will not be resolved.

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.