We use the SAS let statement to define macro variables to use in our code. The variables created are character strings or text expressions.

%let var1 = This is a macro variable defined by the SAS let statement;

The SAS Macro Language is very powerful and allows us to create dynamic SAS programs. The basis of the Macro Language is the use of macro variables.

To create macro variables, we can use the SAS let statement.

The let statement allows us to create macro variables which are character strings or text expressions. The user-defined macro variable then goes to the table of macro variables. let removes trailing and leading blanks from the input expression.

%let var1 = This is a macro variable defined by the SAS let statement;

To use the macro variable later in your code, you can access it with “&” in front of the variable name.

%let var1 = This is a macro variable defined by the SAS let statement;

%put &var1;

/* Output */
This is a macro variable defined by the SAS let statement

Using the SAS let statement to Create a List of Strings

One useful way we can use the let statement in SAS is to create a list of strings. We can create a list of strings and then use the SAS scan() function in a loop to loop through each string in our macro variable.

In the SAS code below, we define a list of strings in the macro variable “list” and then loop over it with the help of the SAS countw() function.

%let list = NY TX FL MN OH TN MI OK;

%macro scan_example;
%do i = 1 to %sysfunc(countw(&list));
    %let current_state = %scan(&list,&i);
    %if ¤t_state = TX %then %do;
        /* Do Texas stuff here */
    %end;
%end;
%mend;

Hopefully this article has been useful for helping you learn how to define macro variables in your SAS code with a let statement.

Categorized in:

SAS,

Last Update: February 26, 2024