You can use the %LET statement in SAS to create macro variables that can store values that you can then use later on in your program.
The following examples show two common ways to use the %LET statement in practice.
Example 1: Use %LET Statement to Store Numeric Value in SAS
Suppose we have the following dataset in SAS that contains information about various basketball players:
/*create dataset*/ data my_data; input team $ points; datalines; A 22 A 14 A 23 B 30 B 18 B 20 C 13 C 12 C 26 ; run; /*view dataset*/ proc print data=my_data;
We can use the %LET statement to create a macro variable called points_cutoff that has a value of 20.
We can then reference this variable later on by using an ampersand ( & ) in front of it to create a new dataset that contains a new column indicating if each player scored more than 20 points or not:
/*assign value of 20 to macro variable*/ %let points_cutoff = 20; /*use macro variable to create new column called good_player*/ data new_data; set my_data; good_player = points > &points_cutoff; run; /*view new dataset*/ proc print data=new_data;
Notice that the new column called good_player contains a value of 1 if the value in the points column is greater than 20 or a value of 0 if the value in the points column is not greater than 20.
Example 2: Use %LET Statement to Store String Value in SAS
We can also use the %LET statement in SAS to create a macro variable that stores a string value.
The following code shows how to use the %LET statement to create a variable called table_title that contains a value of “Basketball Data” which we can then reference later on to print a title for the dataset:
/*create dataset*/ data my_data; input team $ points; datalines; A 22 A 14 A 23 B 30 B 18 B 20 C 13 C 12 C 26 ; run; /*assign string to macro variable*/ %let table_title = "Basketball Data"; /*print dataset with title*/ proc print data=my_data; title &table_title; run;
The dataset contains the title that we specified in the macro variable.
Note: You can find the complete documentation for the SAS LET statement here.
The following tutorials explain how to perform other common tasks in SAS: