Introduction to the Macro Facility |
Macro variables are an efficient way of replacing text strings in SAS code. The simplest way to define a macro variable is to use the %LET statement to assign the macro variable a name (subject to standard SAS naming conventions), and a value.
%let city=New Orleans;
Now you can use the macro variable CITY in SAS statements where you would like the text New Orleans to appear. You refer to the variable by preceding the variable name with an ampersand (&), as in the following TITLE statement:
title "Data for &city";
The macro processor resolves the reference to the macro variable CITY:
title "Data for New Orleans";
A macro variable can be defined within a macro definition or within a statement that is outside a macro definition (called open code).
Note: The title is enclosed in double quotation marks. In quoted strings in open code, the macro processor resolves macro variable references within double quotation marks but not within single quotation marks.
A %LET statement in open code (outside a macro definition) creates a global macro variable that is available for use anywhere (except in DATALINES or CARDS statements) in your SAS code during the SAS session in which the variable was created. There are also local macro variables, which are available for use only inside the macro definition where they are created. See Scopes of Macro Variables for more information about global and local macro variables.
Macro variables are not subject to the same length limits as SAS data set variables. However, if the value you want to assign to a macro variable contains certain special characters (for example, semicolons, quotation marks, ampersands, and percent signs) or mnemonics (for example, AND, OR, or LT), you must use a macro quoting function to mask the special characters. Otherwise, the special character or mnemonic might be misinterpreted by the macro processor. See Macro Quoting for more information about macro quoting.
While macro variables are useful for simple text substitution, they cannot perform conditional operations, DO loops, and other more complex tasks. For this type of work, you must define a macro.
Copyright © 2009 by SAS Institute Inc., Cary, NC, USA. All rights reserved.