Macro Variables Defined by Users

Overview for Defining Macro Variables

You can create your own macro variables, change their values, and define their scope. You can define a macro variable within a macro, and you can also specifically define it as a global variable, by defining it with the %GLOBAL statement. Macro variable names must start with a letter or an underscore and can be followed by letters or digits. You can assign any name to a macro variable as long as the name is not a reserved word. The prefixes AF, DMS, SQL, and SYS are not recommended because they are frequently used in SAS software when creating macro variables. Thus, using one of these prefixes can cause a name conflict with macro variables created by SAS software. For a complete list of reserved words in the macro language, see Reserved Words in the Macro Facility. If you assign a macro variable name that is not valid, an error message is printed in the SAS log.
You can use %PUT _ALL_ to view all user-created macro variables.

Creating Macro Variables and Assigning Values

The simplest way to create and assign a value to a macro variable is to use the macro program statement %LET:
%let dsname=Newdata;
DSNAME is the name of the macro variable. Newdata is the value of the macro variable DSNAME. The value of a macro variable is simply a string of characters. The characters can include any letters, numbers, or printable symbols found on your keyboard, and blanks between characters. The case of letters is preserved in a macro variable value. Some characters, such as unmatched quotation marks, require special treatment, which is described later.
If a macro variable already exists, a value assigned to it replaces its current value. If a macro variable or its value contains macro triggers (% or &), the trigger is evaluated before the value is assigned. In the following example, &name is resolved to Cary and then it is assigned as the value of city in the following statements:
%let name=Cary;
%let city=&name;
Generally, the macro processor treats alphabetic characters, digits, and symbols (except & and %) as characters. It can also treat & and % as characters using a special treatment, which is described later. It does not make a distinction between character and numeric values as the rest of SAS does. (However, the %EVAL Function and %SYSEVALF Function can evaluate macro variables as integers or floating point numbers.)
Macro variable values can represent text to be generated by the macro processor or text to be used by the macro processor. Values can range in length from 0 to 65,534 characters. If you omit the value argument, the value is null (0 characters). By default, leading and trailing blanks are not stored with the value.
In addition to the %LET statement, the following list contains other features of the macro language that create macro variables:
  • iterative %DO statement
  • %GLOBAL statement
  • %INPUT statement
  • INTO clause of the SELECT statement in SQL
  • %LOCAL statement
  • %MACRO statement
  • SYMPUT and SYMPUTX routine and SYMPUTN routine in SCL
  • %WINDOW statement
The following table describes how to assign a variety of types of values to macro variables.
Types of Assignments for Macro Variable Values
Assign
Values
Constant text
A character string. The following statements show several ways that the value maple can be assigned to macro variable STREET. In each case, the macro processor stores the five-character value maple as the value of STREET. The leading and trailing blanks are not stored.
%let street=maple;

%let street=            maple;
%let street=maple            ;
Note: Quotation marks are not required. If quotation marks are used, they become part of the value.
Digits
The appropriate digits. This example creates the macro variables NUM and TOTALSTR:
%let num=123;
%let totalstr=100+200;
The macro processor does not treat 123 as a number or evaluate the expression 100+200. Instead, the macro processor treats all the digits as characters.
Arithmetic expressions
The %EVAL function, for example,
%let num=%eval(100+200); / * produces 300 * /
use the %SYSEVALF function, for example,
%let num=%sysevalf(100+1.597); / * produces 101.597 * /
For more information, see Macro Evaluation Functions.
A null value
No assignment for the value argument. For example,
%let country=;
A macro variable reference
A macro variable reference, &macro-variable. For example,
%let street=Maple;
%let num=123;
%let address=&num &street Avenue;
This example shows multiple macro references that are part of a text expression. The macro processor attempts to resolve text expressions before it makes the assignment. Thus, the macro processor stores the value of macro variable ADDRESS as 123 Maple Avenue.
You can treat ampersands and percent signs as literals by using the %NRSTR function to mask the character so that the macro processor treats it as text instead of trying to interpret it as a macro call. See Macro Language Elements and Macro Quoting for information.
A macro invocation
A macro call, %macro-name. For example,
%let status=%wait;
When the %LET statement executes, the macro processor also invokes the macro WAIT. The macro processor stores the text produced by the macro WAIT as the value of STATUS.
To prevent the macro from being invoked when the %LET statement executes, use the %NRSTR function to mask the percent sign:
%let status=%nrstr(%wait);
The macro processor stores %wait as the value of STATUS.
Blanks and special characters
Macro quoting function %STR or %NRSTR around the value. This action masks the blanks or special characters so that the macro processor interprets them as text. See Macro Quoting Functions. For example,
%let state=%str( North Carolina);
%let town=%str(Taylor%'s Pond);
%let store=%nrstr(Smith&Jones);
%let plotit=%str(
       proc plot;
         plot income*age;
       run;);
The definition of macro variable TOWN demonstrates using %STR to mask a value containing an unmatched quotation mark. Macro Quoting Functions discuss macro quoting functions that require unmatched quotation marks and other symbols to be marked.
The definition of macro variable PLOTIT demonstrates using %STR to mask blanks and special characters (semicolons) in macro variable values. When a macro variable contains complete SAS statements, the statements are easier to read if you enter them on separate lines with indentions for statements within a DATA or PROC step. Using a macro quoting function retains the significant blanks in the macro variable value.
Value from a DATA step
The SYMPUT routine. This example puts the number of observations in a data set into a FOOTNOTE statement where AGE is greater than 20:
data _null_;
   set in.permdata end=final;
   if age>20 then n+1;
   if final then call symput('number',trim(left(n)));
run;
footnote "&number Observations have AGE>20";
During the last iteration of the DATA step, the SYMPUT routine creates a macro variable named NUMBER whose value is the value of N. (SAS also issues a numeric-to-character conversion message.) The TRIM and the LEFT functions remove the extra space characters from the DATA step variable N before its value is assigned to the macro variable NUMBER.
For a discussion of SYMPUT, including information about preventing the numeric-character message, see CALL SYMPUT Routine.