Macro variables

From sasCommunity
(Redirected from Macro variable)
Jump to: navigation, search
This is a work in progress. You are welcome to contribute to this article.

Macro variables are a part of the SAS macro language. The (text) values held in the variables are substituted into the SAS code during compilation of the SAS program. Macro variables are interpreted by the macro processor as a SAS program is being parsed at compile time. The text that is generated (resolved) by the macro functions can then be interpreted as SAS code and run by the SAS compiler.

Syntax

Macro variables are a valid SAS identifier name preceded by an ampersand ( & ) symbol and can optionally be terminated with a period ( . ) or other symbol that unambiguously marks the end of the identifier name. e.g. white space. If the next symbol is a period ( . ), a terminating period ( . ) is needed as the trailing period immediately following a macro variable name will be removed.

Ampersand ( & ) symbols that appear inside character strings that are enclosed in double quotes ( " ) will be interpreted as the start of a macro variable. However, if the character string is enclosed in single quotes ( ' ) then the ampersand is treated as just another character.

Value assignment

Macro variables can be assigned a value by using the %LET statement.

Automatic variables

See the article automatic macro variables for more information.

Some macro variables automatically exist and are assigned values by the SAS system. Although most of these macro variables are READONLY, some can be modified by the user. Most automatic macro variables start with SYS (&SYSDATE9), however some procedures, such as PROC SQL, and some products, such as SAS/AF, will also create automatic macro variables and these may start with other letters. To see a list of automatic macro variables currently on your system you can use a %PUT statement.

 %put _automatic_ ;

Reserved words

Some names cannot be used as macro variable identifiers. Avoid using any of the following reserved words as they already have specific uses in the macro language.

Reserved words
ABEND ABORT ACT ACTIVATE BQUOTE BY CLEAR
CLOSE CMS COMANDR COPY DEACT DEL DELETE
DISPLAY DMIDSPLY DMISPLIT DO EDIT ELSE END
EVAL FILE GLOBAL GO GOTO IF INC
INCLUDE INDEX INFILE INPUT KCMPRES KINDEX KLEFT
KLENGTH KSCAN KSUBSTR KTRIM KUPCASE LENGTH LET
LIST LISTM LOCAL MACRO MEND METASYM NRBQUOTE
NRQUOTE NRSTR ON OPEN PAUSE PUT QKCMPRES
QKLEFT QKSCAN QKSUBSTR QKTRIM QKUPCASE QSCAN QSUBSTR
QSYSFUNC QUOTE QUPCASE RESOLVE RETURN RUN SAVE
SCAN STOP STR SUBSTR SUPERQ SYMDEL SYMGLOBL
SYMLOCAL SYMEXIST SYSCALL SYSEVALF SYSEXEC SYSFUNC SYSGET
SYSRPUT THEN TO TSO UNQUOTE UNSTR UNTIL
UPCASE WHILE WINDOW

Misuse of these words will usually result in an error.

Also it is strongly recommended that one avoids using the following prefixes in any macro variables, to avoid naming conflicts with existing automatic variables.

  • AF ...
  • DMS ...
  • SQL ... (Conflicts with SQL procedure automatic variables.)
  • SYS ... (Conflicts with system automatic variables.)

Because macro variables starting with these prefixes can conflict with valid automatic variable names, strange or unpredictable results may occur without warnings.

Further reading

See also

References

Macro Variables 
SAS support Reference documentation by version: 9.2, 9.3, 9.4