Search notes:

SAS: macros

Macros allow to write a program that writes another (or more) programs (sometimes referred to as meta programming)
Usually, the goal is to write macro statements that the macro processor turns into standard SAS statements.
Macros are especially useful to create variations of repetitive code.
Macro statements start with a percent sign (%). A defined macro is evaluated by also prepending its name with a percent sign.

Define macro

%macro macro_name [(parameters)]
  macro text
%mend macro_name;
This macro is then invoked like so:
%macro_name
and evalulates to macro text.

Compilation of a macro

The compilation of a macro creates two things:

Comments

Preferrably, comments in macros should be of the form % comment text etc... ; except when commenting macro parameters in which case /* .... */ should be used.

Autocall libraries

Macros should be stored in SAS autocall libraries in order for them to be in a standard location.
On Unix and Windows, an autocall library is a (filesystem-)directory that stores macros.
Macros in an autocall library are not compiled. If not protected, their source can be looked at.
In contrast, the stored compiled macro library does not necessarily have the source code with it.
See also !SASROOT/sasautos

Displaying source code of a macro

%copy: copies specified items from a macro library.
%copy macro_name /<options> out='filename.txt'

SASMACR

The SASMACR catalog which is stored in WORK (with entry type MACRO) stores all compiled SAS macros.

Tokens with special meaning

Some tokens have a special meaning to the macro processor:
In order to work with text that contains such tokens, the text needs to be quoted.

See also

macro variables
macro statements
macro functions
using macros
dictionary.macros
Use the dictionary.catalog dictionary table to select/show available macros.
&sysmacroname
Representation of booleans
SAS: Interaction between macros and data steps with execute, symget and symput
SAS stores some predefined macros in !SASROOT/sasautos.
Personal macros

Index