Macro Basic
Macro Basic
Macro Basics
Chapter 1 Chapter 2 Chapter 3 Chapter 4
Introduction Defining and Using Macro Variables Defining and Using Macros Macro Parameters
Chapter 1
Introduction
1.1 Macro Facility Overview
1.2 1.3 1.4 1.5
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.
pass information between SAS steps dynamically create code at execution time conditionally execute DATA or PROC steps create generalizable and flexible code.
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
much of the syntax and content of the macro language is familiar. The following terms will be used throughout this book. text a collection of characters and symbols that can contain variable names, data set names, SAS statement fragments, complete SAS statements, or even complete DATA and PROC steps. macro variable the names of macro variables are almost always preceded by an ampersand (&) in SAS code. Macro variables often are used to store text. macro stored text that contains SAS statements and macro language statements. macro program statement controls what actions take place during the macro execution. They are always preceded by a percent sign (%) and are often syntactically similar to statements used in the DATA step. macro expression one or more macro variable names, text, and/or macro functions combined together by one or more operators and/or parentheses. Macro expressions are very analogous to the expressions used in standard SAS programming. macro function predefined routines for processing text in macros and macro variables. Many macro functions are similar to functions used in the DATA step. operators symbols that are used for comparisons, logical operation, or arithmetic calculations. The operators are the same ones used in the DATA step. automatic macro variable special-purpose macro variables. These are automatically defined and provided by the SAS System. These variable names should be considered as reserved. open code SAS program statements that exist outside of any macro definition. resolving macro references during the resolution process, elements of the macro language (or references) are replaced with text. You can find additional terminology in the glossary.
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:
s s s
Global statements are executed. 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
s s s
macro variables are resolved called macros are executed (resolved) macro statements are executed.
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.
Outside of all macros Global values DSN ---> clinics Inside a macro Local values DSN ----> clinics COLOR ---> blue DSN ---> clinics COLOR ---> undefined
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).