Using SAS Macro Variables

%LET Statement

The SAS macro language enables you to design dynamic programs that you can easily update or modify.
A macro variable can be defined to represent a string of text that appears in your program. For example, if you reference a specific variable value in multiple places in a program, you can substitute a macro variable in its place. Then, if you want to update the value of the variable, you need to update the macro variable definition only once, rather than searching through your code to find the value in multiple places. It is common to place macro variable assignments at the top of SAS programs.
Use the %LET statement to create a macro variable and assign it a value.
Syntax, %LET statement:
%LETmacro-variable=<value>;
  • macro-variable is either the name of a macro variable or a text expression that produces a macro variable name. The name can refer to a new or existing macro variable.
  • value is a character string or a text expression. Omitting value produces a null value (0 characters). Leading and trailing blanks in value are ignored.
You reference the macro variable that you created by using the name of the macro variable with an ampersand (&). An example is &macro-variable.
Note: If the macro variable has already been previously defined in the program, the value is replaced with the most current value.

Example: Using SAS Macro Variables with Numeric Values

When referencing a SAS macro variable with numeric values, use the name of the macro variable with a preceding ampersand (&) and no quotation marks.
%let Cyl_Count=5;                        /*#1*/
proc print data=sashelp.cars;
   where Cylinders=&Cyl_Count;           /*#2*/
   var Type Make Model Cylinders MSRP;
run;
proc freq data=sashelp.cars;
   where Cylinders=&Cyl_Count;
   tables Type;
run;
1 Use the %LET statement to create a macro variable named Cyl_Count that stores the value 5.
2 To reference the macro variable Cyl_Count in your code, use an ampersand (&) and then the macro variable name. Doing so enables you to reference the value of Cyl_Count without having to repeatedly write out the full value.
Seven observations are printed.
Output 9.11 PROC PRINT Results
PROC PRINT Results
Output 9.12 PROC FREQ Results
PROC FREQ Results
When you want to run the code for a different cylinder count, you change only the value of Cyl_Count at the top of the program.

Example: Using SAS Macro Variables with Character Values

When referencing a SAS macro variable with character values, enclose the ampersand (&) and the macro variable in double quotation marks.
In the following example, the macro variable is “Wagon", and the WHERE statement would be where Type = "Wagon". The macro variable is simply taking the place of Wagon, so the macro variable goes inside the quotation marks. Although constants can be enclosed in single quotation marks, macro variables with character variables must always be enclosed in double quotation marks.
%let CarType=Wagon;               /*#1*/
proc print data=sashelp.cars;
  where Type="&CarType";          /*#2*/
  var Type Make Model MSRP;
run;
proc means data=sashelp.cars;
  where Type="&CarType";
  var MSRP MPG_Highway;
run;
proc freq data=sashelp.cars;
  where Type="&CarType";
  tables Origin Make;
run;
1 Use the %LET statement to create a macro variable named CarType that stores the text Wagon.
Note: It is recommended that you do not include quotation marks when you define the macro variable value. Use quotation marks when necessary after the macro variable is resolved.
2 To reference the macro variable CarType in your code, use an ampersand (&) and then the macro variable name.
If you want to run reports later on a different type of car, such as an SUV, then update the value of the macro variable to SUV and rerun the program. If you did not use a macro variable, then you would have to replace the value throughout the code.
The following PRINT, MEANS, and FREQ output shows results where the value for Type is Wagon.
Output 9.13 PROC PRINT Results
PROC PRINT Results
Output 9.14 PROC MEANS Results
PROC MEANS Results
Output 9.15 PROC FREQ Results
PROC FREQ Results

Example: Using Macro Variables in TITLE Statements

The following example uses multiple macro variables with character and numeric values. There are two %LET statements that are used to define two different macro variables. The first is a TITLE statement that contains character values. The other %LET statement contains numeric values. Notice that when you are referencing macro variables with character values, the ampersand (&) and the macro variable name are enclosed in double quotation marks, but the macro variables with numeric values are not.
%let TitleX=PROC PRINT Of Only &Cyl_Count Cylinder Vehicles;
%let Cyl_Count=5;
Title "&TitleX";
proc print data=sashelp.cars;
   where Cylinders=&Cyl_Count;
   var Type Make Model Cylinders MSRP;
run;
Output 9.16 PROC PRINT Results: 5-Cylinder Vehicles
PROC PRINT Results: 5 Cylinder Vehicles
If you want to rerun the code to find 12–cylinder vehicles and want to run PROC MEANS instead, simply update your macro variables and rerun the code.
%let TitleX=PROC MEANS Of Only &Cyl_Count Cylinder Vehicles;
%let Cyl_Count=12;
Title "&TitleX";
proc means data=sashelp.cars;
   where Cylinders=&Cyl_Count;
   var MSRP;
run;
Output 9.17 PROC MEANS Results: 12–Cylinder Vehicles
PROC MEANS Results: 12 Cylinder Vehicles
Last updated: February 14, 2019
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset