Macro
Macro
Macro Basics
Chapter 1
Introduction
Chapter 2
Chapter 3
Chapter 4
Macro Parameters
Chapter 1
Introduction
1.1 Macro Facility Overview
1.2
Terminology
1.3
1.4
Referencing Environments
1.5
Chapter Summary
This chapter introduces you to the fundamentals of the SAS macro language, and it
includes an overview and some of the terminology of the language. Because the behavior of macros is different from that of code that is written for base SAS, sections are also
included on macro execution and how the SAS System sees and uses macros.
The tools made available through the macro facility include macro (or symbolic) variables, macro statements, and macro functions. These tools are included as part of the
SAS code, or program, where they are detected when the code is sent to the SAS Supervisor for execution.
1.2 Terminology
The statement and syntax structure that is used by the macro facility is known as the
macro language and like any language it has its own terminology. The SAS user familiar
with the programming language used in base SAS, however, will discover quickly that
Chapter 1: Introduction
does contain macro references, then the macro processor intercepts and resolves them prior to
execution. The resolved macro references then become part of the SAS code that is passed to
the DATA or PROC step processor.
When code is passed to the SAS supervisor, the following takes place for each step:
Macro definitions are compiled and stored until they are called.
A check is made to see if there are any macro statements, macro variables, or macro
calls. If there are, then
The DATA or PROC step that contains resolved macro references (if there were any) is
compiled and executed.
SEE ALSO
SAS Macro Language Reference, First Edition contains a detailed discussion of how SAS processes statements with macro activity on pp.1419 and 3341.
Local macro variables have values that are available only within the macro in which they are
defined.
Because each macro creates its own local referencing environment, macro variable values that
are defined in one macro may be undefined within another. Indeed, macro variable names need
not be unique even among nested macros. This means that the specific value associated with a
given macro variable may depend on how the macro variable is used in the program.
In the following schematic, the macro variable DSN is defined globally and is, therefore, also
known inside of the shaded macro. The macro variable COLOR, however, is only defined inside
of the shaded macro and is not known outside of the macro.
Ou t s i d e o f a l l ma c r o s
G l ob a l v a l u e s
DSN - - - > c l i n i c s
I n s i d e a ma c r o
Lo c a l v a l u e s
DSN - - - - > c l i n i c s
COLOR - - - > b l u e
DSN - - - > c l i n i c s
COLOR - - - > und e f i n e d
You can control the referencing environment for a macro variable through the use of the
%GLOBAL and %LOCAL statements, which are described in Section 5.4.2.
SEE ALSO
Extensive examples can be found in SAS Guide to Macro Processing, Version 6, Second Edition
(pp. 3754) and the newer SAS Macro Language: Reference, First Edition (pp. 5066).
SUGI presentations that specifically cover referencing environments include Bercov (1993) and
Hubbell (1990).
An example of a macro variable that takes on more than one value at the same time is given in
Carpenter (1996, p. 1637).