- 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.
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.
Macro variables can be assigned a value by using the %LET statement.
- 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_ ;
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.
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.
- Tips:Using the &SYSUSERID Macro Variable
- Tips:Handling trailing and leading blanks in SAS Macro Variable
- Looping through tokens in a macro variable with Scan
- Tips:Use the RESOLVE Function to Build a List into a Macro Variable
- Tips:Two steps in one with Proc SQL – summarize and create macro variable
- Tips:Strip Blanks from PROC SQL-Created Macro Variable Values
- Tips:Making Macro Variables READONLY
- Macro Variable for next title line
- Passing Macro Variables
- Passing Real Numbers as Macro Variables
- Resolving a macro variable within single quotes, (Tip)
- Removing Macro Variables from the SAS Environment
- Use of Macro to generate data values instead of code
- Changing Data Set Variables into Macro Variables
- Deleting global macro variables
- &SYSTIME with Seconds
- Before You Get Started: A Macro Language Preview in Three Parts
- Macro language
- Macro statements
- Macro functions
- Category:Macro Language