You can end any macro variable reference with a delimiter, but the delimiter is necessary
only if the characters that follow can be part of a SAS name. For example, both of these
TITLE statements are correct:
title "&name.--a report";
title "&name--a report";
They produce the following:
TITLE "sales--a report";
Displaying Macro Variable Values
The simplest way to display macro variable values is to use the %PUT statement, which
writes text to the SAS log. For example, the following statements write the following
%let a=first;
%let b=macro variable;
%put &a ***&b***;
Here is the result:
first ***macro variable***
You can also use a “%PUT Statement” on page 325 to view available macro variables.
%PUT provides several options that enable you to view individual categories of macro
The system option SYMBOLGEN displays the resolution of macro variables. For this
example, assume that macro variables PROC and DSET have the values GPLOT and
Sasuser.Houses, respectively.
options symbolgen;
title "%upcase(&proc) of %upcase(&dset)";
The SYMBOLGEN option prints to the log:
SYMBOLGEN: Macro variable PROC resolves to gplot
SYMBOLGEN: Macro variable DSET resolves to sasuser.houses
For more information about debugging macro programs, see Chapter 10, “Macro Facility
Error Messages and Debugging,” on page 119.
Referencing Macro Variables Indirectly
Using an Expression to Generate a Reference
The macro variable references shown so far have been direct macro references that begin
with one ampersand: &name. However, it is also useful to be able to indirectly reference
macro variables that belong to a series so that the name is determined when the macro
variable reference resolves. The macro facility provides indirect macro variable
referencing, which enables you to use an expression (for example, CITY&N) to generate
a reference to one of a series of macro variables. For example, you could use the value of
macro variable N to reference a variable in the series of macro variables named CITY1
Referencing Macro Variables Indirectly 33
to CITY20. If N has the value 8, the reference would be to CITY8. If the value of N is 3,
the reference would be to CITY3.
Although for this example the type of reference that you want is CITY&N, the following
example will not produce the value of &N appended to CITY:
%put &city&n; /* incorrect */
This code produces a warning message saying that there is no macro variable CITY
because the macro facility has tried to resolve &CITY and then &N and concatenate
those values.
When you use an indirect macro variable reference, you must force the macro processor
to scan the macro variable reference more than once. This process will resolve the
desired reference on the second, or later, scan. To force the macro processor to rescan a
macro variable reference, you use more than one ampersand in the macro variable
reference. When the macro processor encounters multiple ampersands, its basic action is
to resolve two ampersands to one ampersand. For example, for you to append the value
of &N to CITY and then reference the appropriate variable name, do the following:
%put &&city&n; /* correct */
If &N contains 6, when the macro processor receives this statement, it performs the
following steps:
1. resolves && to &
2. passes CITY as text
3. resolves &N into 6
4. returns to the beginning of the macro variable reference, &CITY6, starts resolving
from the beginning again, and prints the value of CITY6
Generating a Series of Macro Variable References with a Single
Macro Call
Using indirect macro variable references, you can generate a series of references with a
single macro call by using an iterative %DO loop. The following example assumes that
the macro variables CITY1 through CITY10 contain the respective values Cary, New
York, Chicago, Los Angeles, Austin, Boston, Orlando, Dallas, Knoxville, and Asheville:
%macro listthem;
%do n=1 %to 10; &&city&n
%mend listthem;
%put %listthem;
This program writes the following to the SAS log:
Cary New York Chicago Los Angeles Austin Boston
Orlando Dallas Knoxville Asheville
Using More Than Two Ampersands
You can use any number of ampersands in an indirect macro variable reference, although
using more than three is rare. Regardless of how many ampersands are used in this type
of reference, the macro processor performs the following steps to resolve the reference.
34 Chapter 3 Macro Variables

Get SAS 9.4 Macro Language: Reference, Fourth Edition, 4th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.