Macro variables in SAS are used extensively for reusing codes. You can create a multi-usable program in which the analysis of variables can be controlled by updating the values of macro variables.

LET statement can be used to create a few macro variables which work extremely well. Data step with CALL SYMPUT or INTO statement in PROC SQL provides a better method to automate the creation of macro variables.

However, if you have a large number of variables in a data step that needs to be converted into macro variables, these solutions may require a great deal of typing.

SAS provides an elegant solution to overcome these problems with the CALL SET routine. You can convert data step variables into macro variables with a few lines of code using the %SYSCALL SET routine.

This routine also allows you to convert and process one observation of data step variables at a time thus saving memory usage.

Ways to create Macro Variables in SAS

SAS provides a number of different options for creating macro variables.

1. %LET statement for creating a single macro variable

The %LET statement is one of the easiest methods to create a macro variable and is the equivalent of the DATA step’s assignment statement.

%LET statement is followed by the macro variable name, an equal sign (=), and then the text value to be assigned to the macro variable.

SYNTAX:

%LET <macro-variable-name> = <macro-variable-value>

Example:

%LET Name = Smith;

Note that, quotation marks are not used while creating macro variables with the LET statement. Macro variables are neither character nor numeric, they always just stored as a plain text.

When the macro variables &name is created with the %let statement, the value of ‘Smith’, you can use this macro variable across anywhere in your program.

However, if you have more than a few macro variables to create, typing the %let statements can be tedious and error-prone.

2. CALL SYMPUT – To create many macro variables

If the variables that you need to store in macro variables are contained in a SAS dataset, then %LET statements cannot be used to assign those values into a macro variable.

In the below, example the Fname macro variable is supposed to contain the values of the Name variable from the SASHELP.CLASS dataset.

data new_class; set sashelp.class; %let FName = Name; run;

Since %LET statements are executed before the data step is even compiled, the macro variable &name will resolve to ‘Name’ as the value and not the actual value in the data set.

To overcome this problem we will need to use the data step to place information onto the macro variable symbol tables.

We can do this with SYMPUT which is a DATA step call routine, and not a macro language statement.

SYNTAX

CALL SYMPUT(macro-variable, value);

EXAMPLE

data new_class; set sashelp.class; call symput('FName',Name); run;

In the above example, the CALL SYMPUT statement creates the macro variable &Fname containing the variable name. However, only the last value of the variable name is assigned to the macro variable.

To place all of the values of the name into a macro variable, we can include a suffix as in the below example.

data new_class; set sashelp.class; suffix=put(_n_,5.); call symput(cats('FName',suffix),Name); run;

The suffix variable counts the observation(_n_) and appends count onto the macro variable Fname so that unique Macro variables are created for each name.

The resultant macro variables &Fname1 will store the first name value, the second value is stored in the macro variable &Fname2 and so on.

Now, in order to retrieve any value of the Name variable, you can use the below statement and replace the number with the observation number.

The statement below resolves to ‘Carol‘ which is the 4th Observation in the dataset.