Syntax, %LET statement:
%LETmacro-variable=<value>;
|
%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. |
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.
|
%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;
%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;