Using User-Defined Macro Variables

The %LET Statement

You have seen that SAS provides a variety of automatic macro variables for you. You can also create your own macro variables.
The simplest way to define your own macro variables is to use a %LET statement. The %LET statement enables you to define a macro variable and to assign a value to it.
General form, %LET statement:
%LET variable=value;
Here is an explanation of the syntax:
variable
is any name that follows the SAS naming convention.
value
can be any string from 0 to 65,534 characters.
variable or value
if either contains a reference to another macro variable (such as &macvar), the reference is evaluated before the assignment is made.
Note: If variable already exists, value replaces the current value.

Example

To create a macro variable named time and assign a value of afternoon to it, you would submit the following %LET statement:
%let time=afternoon;
When you use the %LET statement to define macro variables, you should keep in mind the following rules:
  • All values are stored as character strings.
  • Mathematical expressions are not evaluated.
  • The case of the value is preserved.
  • Quotation marks that enclose literals are stored as part of the value.
  • Leading and trailing blanks are removed from the value before the assignment is made.

%LET Statement Examples

When you define a macro variable, remember that its value is always a character string. This table provides examples of macro variable assignment statements to illustrate the rules that are listed in the previous section.
%LET Statement
Variable Name
Variable Value
Length
%let name= Ed Norton ;
name
Ed Norton
9
%let name2=' Ed Norton ';
name2
' Ed Norton '
13
%let title="Joan's Report";
title
"Joan's Report"
15
%let start=;
start
 
0
%let total=0;
total
0
1
%let sum=4+3;
sum
4+3
3
%let total=&total+&sum
total
0+4+3
5
%let x=varlist;
x
varlist
7
%let &x=name age height;
varlist
name age height
15
In the following example, the value DALLAS is assigned to the macro variable site. The macro variable site is then used to control program output.
%let site=DALLAS;
title "REVENUES FOR &site TRAINING CENTER";
proc tabulate data=sasuser.all(keep=location
                         course_title fee);
   where upcase(location)="&site";
   class course_title;
   var fee;
   table course_title=' ' all='TOTALS',
         fee=' '*(n*f=3. sum*f=dollar10.)
         / rts=30 box='COURSE';
run;
Revenues for Dallas Training Center
..................Content has been hidden....................

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