Fundamentally, the SAS macro facility is a tool for text substitution. You associate a macro reference with text. When the macro processor encounters that reference, it replaces the reference with the associated text. This text can be as simple as text strings or as complex as SAS language statements. The macro processor becomes your SAS programming assistant in helping you construct your SAS programs.
The SAS macro facility is a component of Base SAS. The Base SAS product is integral to SAS and must be installed if you want to write SAS programs or run SAS procedures in any of the SAS products. Therefore, if you have access to SAS, you have access to the macro facility, and you can include macro facility features in your programs. Additionally, if you use SAS Enterprise Guide, you can add macro facility features when specifying projects and tasks. Indeed, many of the SAS products that you license contain programs that make use of the macro facility.
the SAS macro facility works side-by-side with Base SAS to build and execute your programs. The macro facility has its own language distinct from the SAS language, but the language and conventions of the macro facility are similar to the style and syntax of the SAS language. If you already write DATA steps, you have a head start on understanding the language and conventions of the macro facility.
How the SAS macro facility fits into SAS
The two main components of the SAS macro facility are SAS macro variables and SAS macro programs. With SAS macro variables, you create references to larger pieces of text. A typical use of a macro variable is to repeatedly insert a piece of text throughout a SAS program. SAS macro programs use macro variables and macro programming statements to build SAS programs. Macro programs can direct conditional execution of DATA steps and PROC steps. Macro programs can do repetitive tasks such as creating or analyzing a series of data sets.
Example 1.1 shows how a macro variable can be used, and Example 1.2 shows how a macro program can be used.
Example 1.1: Using a Macro Variable to Select Observations to Process
The macro variable MONTH_SOLD defined in Example 1.1 is used to select a subset of a data set and to place information in the PROC PRINT report title. Macro language and macro variable references are in bold.
proc print data=books.ytdsales
title “Books Sold for Month &month_sold”;
var booktitle saleprice;
Example 1.2: Using a Macro Program to Execute the Same PROC Step on Multiple Data Sets
When Example 1.2 executes, it submits a PROC MEANS step three times: once for each of the years 2014, 2015, and 2016. Each time, PROC MEANS processes a different data set. The macro language and references that generate the three steps are in bold.
%do year=2014 %to 2016;
proc means data=books.sold&year;
title “Sales Information for &year”;
var listprice saleprice;
SAS first released the macro facility in SAS 82.0 in 1982. SAS macro language has relatively few statements, and these statements are very powerful.
In a world of rapidly changing software tools and techniques, the macro facility remains one of the most widely used and versatile components of SAS. What you learn now about the macro facility will serve you for many years of SAS programming.
— — — — — — —