Chapter Contents


SAS Macro Language: Reference

Replacing Text Strings Using Macro Variables

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. Here is a simple example:

%let city=New Orleans;

Now you can use the macro variable CITY in SAS statements where you'd 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, and the statement becomes

title "Data for New Orleans";

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.  [cautionend]

A %LET statement in open code (outside a macro definition) creates a { it global} macro variable that is available for use anywhere 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 Chapter 5, "Scope of Macro Variables," for more information on 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 Chapter 7, "Macro Quoting," for more information on 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 kind of work, you must define a macro, which is described in the next section.

Chapter Contents



Top of Page

Copyright 1999 by SAS Institute Inc., Cary, NC, USA. All rights reserved.