Sie sind auf Seite 1von 128

Chapter 20: Macro Variables

20.1 Introduction to Macro Variables

20.2 Macro Variable References

20.3 User-Defined Macro Variables

20.4 Delimiting Macro Variable References

20.5 Macro Functions

1
Chapter 20: Macro Variables

20.1 Introduction to Macro Variables

20.2 Macro Variable References

20.3 User-Defined Macro Variables

20.4 Delimiting Macro Variable References

20.5 Macro Functions

2
Objectives
 Describe what values macro variables store and
where they reside.
 Identify the two types of macro variables.

3
Macro Variables
Macro variables store text, including complete or partial
 steps

 statements

Macro variables are called symbolic variables because


they can be referenced as symbols for SAS code.

4
Global Symbol Table
Macro variables are stored in a memory area called the
global symbol table. When SAS is invoked, the global
symbol table is created and initialized with automatic
macro variables.

Global Symbol Table


SYSDATE 09NOV07
Automatic SYSDATE9 09NOV2007
Variables SYSDAY Friday
SYSTIME 10:47
SYSUSERID joeuser
. .
. .

5
Global Symbol Table
User-defined macro variables can be added to the
global symbol table.

Global Symbol Table


. .
. .
Automatic SYSTIME 10:47
Variables SYSUSERID joeuser
. .
. .
User-Defined OFFICE Sydney
Variables DATE1
25may2008
UNITS 4
6
Macro Variables
Macro variables in the global symbol table
 are global in scope (available throughout the
SAS session)
 have a minimum length of 0 characters (null value)

 have a maximum length of 64K characters

 store numeric tokens as text.

7
8
Chapter 20: Macro Variables

20.1 Introduction to Macro Variables

20.2 Macro Variable References

20.3 User-Defined Macro Variables

20.4 Delimiting Macro Variable References

20.5 Macro Functions

9
Objectives
 Explain how macro variable references are handled
by the word scanner and macro processor.

10
Macro Variable References
Macro variable (symbolic) references:
 begin with an ampersand (&), followed by a macro
variable name
 can appear anywhere in your program

 are not case-sensitive

 represent macro triggers that are passed to the macro


processor

11
Macro Variable References
When the macro processor receives a macro variable
reference, it:
searches the symbol table for the macro variable

resolves the macro variable by substituting its value

issues a warning to the SAS log if the macro variable


is not found in the symbol table

12
Macro Variable References
Example: Write the day of the week to the SAS log.

Partial SAS Log


12 %put Today is &sysday;
Today is Tuesday

13
Substitution within a Macro Statement

Compiler
Macro Processor
Word
Scanner

%put Symbol Table


Input %put Today
Today is
is &sysday;
&sysday;
Stack SYSDAY Tuesday
SYSLAST _NULL_

14
Substitution within a Macro Statement
When a macro trigger is encountered, it is passed to the
macro processor for evaluation.

Compiler
Macro Processor
Word %put
%put
Scanner

Today Symbol Table


Input Today is
is &sysday;
&sysday;
Stack SYSDAY Tuesday
SYSLAST _NULL_

15
Substitution within a Macro Statement
The macro processor requests tokens until a semicolon
is encountered.

Compiler
Macro Processor
Word %put
%put Today
Today is
is &sysday;
&sysday;
Scanner

Symbol Table
Input
Stack SYSDAY Tuesday
SYSLAST _NULL_

16
Substitution within a Macro Statement
The macro variable reference triggers the macro processor
to search the symbol table for the reference.

Compiler
Macro Processor
Word %put
%put Today
Today is
is &sysday;
&sysday;
Scanner

Symbol Table
Input
Stack SYSDAY Tuesday
SYSLAST _NULL_

17
Substitution within a Macro Statement
The macro processor resolves the macro variable reference,
substituting its value.

Compiler
Macro Processor
Word %put
%put Today
Today is
is Tuesday;
Tuesday;
Scanner

Symbol Table
Input
Stack SYSDAY Tuesday
SYSLAST _NULL_

18
Substitution within a Macro Statement
The macro processor executes the %PUT statement,
writing the resolved text to the SAS log.

Compiler
Macro Processor
Word %put
%put Today
Today is
is Tuesday;
Tuesday;
Scanner

Symbol Table
Input
Stack SYSDAY Tuesday
SYSLAST _NULL_

19
Substitution within a SAS Literal
Example: Substitute system information in footnotes.
proc freq data=orion.Customer;
table Country / nocum;
footnote1 'Created &systime &sysday, &sysdate9';
footnote2 'By user &sysuserid on system &sysscpl';
run;

m102d01a
20
Substitution within a SAS Literal
SAS Output
Customer Country

Country Frequency Percent


ƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒ
AU 8 10.39
CA 15 19.48
DE 10 12.99
IL 5 6.49
TR 7 9.09
US 28 36.36
ZA 4 5.19

Created &systime &sysday, &sysdate9


By user &sysuserid on system
&sysscpl

The word scanner does not tokenize literals enclosed in


single quotation marks, so macro variables do not resolve.
21
Substitution within a SAS Literal
Example: Substitute system information in footnotes.

proc freq data=orion.Customer;


table
To reference Country
macro variables / the
within a literal, enclose nocum;
literal in double quotation marks.

footnote1 "Created &systime &sysday, &sysdate9";


footnote2 "By user &sysuserid on system &sysscpl";
run;

m102d01b
22
Substitution within a SAS Literal
PROC FREQ Output
Customer Country

Country Frequency Percent


ƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒ
AU 8 10.39
CA 15 19.48
DE 10 12.99
IL 5 6.49
TR 7 9.09
US 28 36.36
ZA 4 5.19

Created 10:47 Friday, 09NOV2007


By user joeuser on system XP_PRO

23
Substitution within a SAS Literal

Compiler
Macro Processor
Word
Scanner

Symbol Table
proc
proc print
print data=a;
Input title
data=a;
title "Today is
"Today is &sysday";
&sysday"; SYSDAY Tuesday
Stack run;
run;
SYSLAST WORK.ALL

24
Substitution within a SAS Literal
SAS statements are passed to the compiler.

proc
proc print
print data=a;
data=a;
Compiler title
title
Macro Processor
Word ""
Today
Today
Scanner is
is

Symbol Table
Input
&sysday";
&sysday";
Stack run; SYSDAY Tuesday
run; SYSLAST WORK.ALL

25
Substitution within a SAS Literal
The macro trigger is passed to the macro processor.

proc
proc print
print data=a;
data=a;
Compiler title
title
Macro Processor
Word "" &sysday
Today
Today
Scanner is
is

Symbol Table
Input
";
";
Stack run; SYSDAY Tuesday
run; SYSLAST WORK.ALL

26
Substitution within a SAS Literal
The macro processor searches the symbol table.

proc
proc print
print data=a;
data=a;
Compiler title
title
Macro Processor
Word "" &sysday
Today
Today
Scanner is
is

Symbol Table
Input
";
";
Stack run; SYSDAY Tuesday
run; SYSLAST WORK.ALL

27
Substitution within a SAS Literal
The resolved reference is passed back to the input stack.

proc
proc print
print data=a;
data=a;
Compiler title
title
Macro Processor
Word ""
Today
Today
Scanner is
is

Symbol Table
Input
Tuesday";
Tuesday";
Stack run; SYSDAY Tuesday
run; SYSLAST WORK.ALL

28
Substitution within a SAS Literal
Word scanning continues.

proc
proc print
print data=a;
data=a;
Compiler title
title
Macro Processor
Word ""
Today
Today
Scanner is
is
Tuesday
Tuesday
""
Symbol Table
Input
;;
Stack run; SYSDAY Tuesday
run; SYSLAST WORK.ALL

29
Substitution within a SAS Literal
The double-quoted literal is passed to the compiler as a
unit.

proc
proc print
print data=a;
data=a;
Compiler title
title "Today is
"Today is Tuesday”
Tuesday”
Macro Processor
Word
Scanner

Symbol Table
Input
;;
Stack run; SYSDAY Tuesday
run; SYSLAST WORK.ALL

30
Substitution within a SAS Literal
When a step boundary is encountered, compilation ends
and execution begins.

proc
proc print
print data=a;
data=a;
Compiler title
title "Today is
"Today is Tuesday";
Tuesday";
Macro Processor
Word
Scanner
run
run
;;
Symbol Table
Input
Stack SYSDAY Tuesday
SYSLAST WORK.ALL

31
32
20.01 Multiple Choice Poll
Macro variable references are resolved by the:
a. SAS compiler
b. Macro processor
c. Word scanner

33
20.01 Multiple Choice Poll – Correct Answer
Macro variable references are resolved by the:
a. SAS compiler
b. Macro processor
c. Word scanner

34
Substitution within SAS Code
Example: Generalize PROC PRINT to print the most
recently created data set, using the automatic
macro variable SYSLAST.

Compiler

Macro Processor
Word
Scanner

Symbol Table
proc
proc print
print data=&syslast;
Input title
data=&syslast;
title "Listing of
"Listing of &syslast";
&syslast";
Stack run;
run; SYSDAY Tuesday
SYSLAST ORION.ALL

35
Substitution within SAS Code
SAS statements are passed to the compiler. When a macro
trigger is encountered, it is passed to the macro processor
for evaluation.
proc
proc print
print data=
Compiler data=

Macro Processor
Word
Scanner &syslast
&syslast

Symbol Table
;;
Input title
title "Listing
"Listing of
of &syslast";
&syslast";
Stack run;
run; SYSDAY Tuesday
SYSLAST ORION.ALL

36
Substitution within SAS Code
The macro variable reference triggers the macro processor
to search the symbol table for the reference.

proc
proc print
print data=
Compiler data=

Macro Processor
Word
&syslast
Scanner

Symbol Table
;;
Input title
title "Listing
"Listing of
of &syslast";
&syslast";
Stack run;
run; SYSDAY Tuesday
SYSLAST ORION.ALL

37
Substitution within SAS Code
The macro processor resolves the macro variable reference,
passing its resolved value back to the input stack.

proc
proc print
print data=
Compiler data=

Macro Processor
Word
Scanner

Symbol Table
ORION.ALL;
Input ORION.ALL;
Stack SYSDAY Tuesday
title SYSLAST ORION.ALL
title "Listing
"Listing
of &syslast";
of &syslast";
run;
run;
38
Substitution within SAS Code
Word scanning continues.

Compiler proc
proc print
print data=ORION.ALL;
data=ORION.ALL;

Macro Processor
Word
Scanner

Symbol Table
Input
Stack SYSDAY Tuesday
title SYSLAST ORION.ALL
title "Listing
"Listing of
of
&syslast";
&syslast";
run;
run;
39
Substitution within SAS Code
A step boundary is encountered. Compilation ends, and
execution begins.

proc
proc print
print data=ORION.ALL;
data=ORION.ALL;
Compiler title
title "Listing of
"Listing of ORION.ALL";
ORION.ALL";

Macro Processor
Word run
run
Scanner ;;

Symbol Table
Input
Stack SYSDAY Tuesday
SYSLAST ORION.ALL

40
Unresolved Reference
Example: Reference a non-existent macro variable within a
SAS literal.

Compiler

Macro Processor
Word
Scanner

Symbol Table
Input proc
proc print
print data=orion.exp;
data=orion.exp;
title
title "Expenses for
"Expenses for R&D";
R&D";
Stack SYSDAY Tuesday
run;
run; SYSLAST _NULL_

41
Unresolved Reference
The macro trigger is passed to the macro processor for
evaluation.

proc
proc print
print data=orion.exp;
data=orion.exp;
Compiler title
title

Macro Processor
Word "" &D
Expenses
Expenses
Scanner for
for
RR

Symbol Table
Input ";
";
run;
run;
Stack SYSDAY Tuesday
SYSLAST _NULL_

42
Unresolved Reference
The macro processor writes a WARNING message to the
SAS log when it cannot resolve a reference.

proc WARNING: Apparent symbolic


proc print
print data=orion.exp;
data=orion.exp; reference D not resolved.
Compiler title
title

Macro Processor
Word "" &D
Expenses
Expenses
Scanner for
for
RR

Symbol Table
Input ";
";
run;
run;
Stack SYSDAY Tuesday
SYSLAST _NULL_

43
Unresolved Reference
If the macro processor cannot resolve a reference, the
tokens are passed back to the word scanner. The word
scanner passes them to the compiler.
proc
proc print
print data=orion.exp;
data=orion.exp;
Compiler title
title "Expenses for
"Expenses for R&D"
R&D"

Macro Processor
Word ;;
run
run
Scanner ;;

Symbol Table
Input
Stack SYSDAY Tuesday
SYSLAST _NULL_

44
45
20.02 Quiz
How could you avoid a warning about an unresolved
symbolic reference inside a SAS literal?
title "Expenses for R&D";

46
20.02 Quiz – Correct Answer
How could you avoid a warning about an unresolved
symbolic reference inside a SAS literal?
title "Expenses for R&D";

Place the literal inside single quotes:


title 'Expenses for R&D';

47
Unresolved Reference
Example: Reference a non-existent macro variable within
SAS code.
proc print data=&sydlast;
title "Listing of &syslast";
run;

48
Unresolved Reference
SAS Log
73 proc print data=&sydlast;
-
22
------
202
WARNING: Apparent symbolic reference SYDLAST not resolved.
ERROR: File WORK.SYDLAST.DATA does not exist.
ERROR 22-322: Expecting a name.
ERROR 202-322: The option or parameter is not recognized and will be ignored.
74 title "Listing of &syslast";
75 run;

NOTE: The SAS System stopped processing this step because of errors.
NOTE: PROCEDURE PRINT used (Total process time):
real time 0.00 seconds
cpu time 0.00 seconds

49
50
Exercise

This exercise reinforces the concepts discussed


previously.

51
Chapter 20: Macro Variables

20.1 Introduction to Macro Variables

20.2 Macro Variable References

20.3 User-Defined Macro Variables

20.4 Delimiting Macro Variable References

20.5 Macro Functions

52
Objectives
 Create user-defined macro variables.
 Display values of user-defined macro variables
in the SAS log.

53
The %LET Statement
The %LET statement creates a macro variable and
assigns it a value.

General form of the %LET statement:

%LET
%LETvariable=value;
variable=value;

 variable follows SAS naming conventions.


 If variable already exists, its value is overwritten.
 If variable or value contain macro triggers, the triggers
are evaluated before the %LET statement is executed.

54
The %LET Statement
Value can be any string:
 The maximum length is 64K characters.

 The minimum length is 0 characters (null value).

 Numeric tokens are stored as character strings.

 Mathematical expressions are not evaluated.

 The case of value is preserved.

 Quotation marks are stored as part of value.

 Leading and trailing blanks are removed from value


before the assignment is made.

55
%LET Statement Examples
Determine the value assigned to each macro variable by
these %LET statements.
Global Symbol Table

%let name= Ed Norton ;


%let name2=' Ed Norton ';
%let title="Joan's Report";
%let start=;
%let sum=3+4;
%let total=0;
%let total=&total+∑
%let x=varlist;
%let &x=name age height;

56
%LET Statement Examples
The %LET statement truncates leading and trailing blanks,
but preserves case.
Global Symbol Table

%let name= Ed Norton ;


%let name2=' Ed Norton '; name Ed Norton
%let title="Joan's Report";
%let start=;
%let sum=3+4;
%let total=0;
%let total=&total+∑
%let x=varlist;
%let &x=name age height;

57
%LET Statement Examples
Quotation marks are stored as part of the value.

Global Symbol Table

%let name= Ed Norton ;


%let name2=' Ed Norton '; name Ed Norton
%let title="Joan's Report"; name2 ' Ed Norton '
%let start=;
%let sum=3+4;
%let total=0;
%let total=&total+∑
%let x=varlist;
%let &x=name age height;

58
%LET Statement Examples
Quotation marks are stored as part of the value.

Global Symbol Table

%let name= Ed Norton ;


%let name2=' Ed Norton '; name Ed Norton
%let title="Joan's Report"; name2 ' Ed Norton '
%let start=; title "Joan's Report"
%let sum=3+4;
%let total=0;
%let total=&total+∑
%let x=varlist;
%let &x=name age height;

59
%LET Statement Examples
A null value is stored.

Global Symbol Table

%let name= Ed Norton ;


%let name2=' Ed Norton '; name Ed Norton
%let title="Joan's Report"; name2 ' Ed Norton '
%let start=; title "Joan’s Report"
%let sum=3+4; start
%let total=0;
%let total=&total+∑
%let x=varlist;
%let &x=name age height;

60
%LET Statement Examples
Mathematical expressions are not evaluated.

Global Symbol Table

%let name= Ed Norton ;


%let name2=' Ed Norton '; name Ed Norton
%let title="Joan's Report"; name2 ' Ed Norton '
%let start=; title "Joan’s Report"
%let sum=3+4; start
%let total=0; sum 3+4
%let total=&total+∑
%let x=varlist;
%let &x=name age height;

61
%LET Statement Examples
Numeric tokens are stored as character strings.

Global Symbol Table

%let name= Ed Norton ;


%let name2=' Ed Norton '; name Ed Norton
%let title="Joan's Report"; name2 ' Ed Norton '
%let start=; title "Joan’s Report"
%let sum=3+4; start
%let total=0; sum 3+4
%let total=&total+∑ total 0
%let x=varlist;
%let &x=name age height;

62
%LET Statement Examples
The macro trigger is evaluated before the %LET statement
executes. The previous value of TOTAL is replaced.
Global Symbol Table

%let name= Ed Norton ;


%let name2=' Ed Norton '; name Ed Norton
%let title="Joan's Report"; name2 ' Ed Norton '
%let start=; title "Joan’s Report"
%let sum=3+4; start
%let total=0; sum 3+4
%let total=&total+∑ total 0+3+4
%let x=varlist;
%let &x=name age height;

63
%LET Statement Examples

Global Symbol Table

%let name= Ed Norton ;


%let name2=' Ed Norton '; name Ed Norton
%let title="Joan's Report"; name2 ' Ed Norton '
%let start=; title "Joan’s Report"
%let sum=3+4; start
%let total=0; sum 3+4
%let total=&total+∑ total 0+3+4
%let x=varlist; x varlist
%let &x=name age height;

64
%LET Statement Examples
The macro variable's name resolves to varlist.

Global Symbol Table

%let name= Ed Norton ;


%let name2=' Ed Norton '; name Ed Norton
%let title="Joan's Report"; name2 ' Ed Norton '
%let start=; title "Joan’s Report"
%let sum=3+4; start
%let total=0; sum 3+4
%let total=&total+∑ total 0+3+4
%let x=varlist; x varlist
%let &x=name age height; varlist name age height

65
%LET Statement Examples
Example: Use a macro variable to select a numeric value.
%let units=4;
proc print data=orion.Order_Fact;
where Quantity > &units;
var Order_Date Product_ID Quantity;
title "Orders exceeding &units units";
run;
Macro variables store numbers as text, not as numeric
values.

m102d02
66
%LET Statement Examples
SAS Log
1 %let units=4;
2 proc print data=orion.Order_Fact;
3 where Quantity > &units;
4 var Order_Date Product_ID Quantity;
5 title "Orders exceeding &units units";
6 run;

NOTE: There were 6 observations read from the data set ORION.ORDER_FACT.
WHERE Quantity>4;
NOTE: PROCEDURE PRINT used (Total process time):
real time 0.70 seconds
cpu time 0.15 seconds

67
%LET Statement Examples
Example: Use a macro variable to select a character
value.
%let office=Sydney;
proc print data=orion.Employee_Addresses;
where City="&office";
var Employee_Name;
title "&office Employees";
run;

m102d03
68
%LET Statement Examples
SAS Log
55 %let office=Sydney;
56 proc print data=orion.Employee_Addresses;
57 where City="&office";
58 var Employee_Name;
59 title "&office Employees";
60 run;

NOTE: There were 67 observations read from the data set ORION.EMPLOYEE_ADDRESSES.
WHERE City='Sydney';
NOTE: PROCEDURE PRINT used (Total process time):
real time 0.00 seconds
cpu time 0.00 seconds

69
SAS Programming Review
Character constants are enclosed in quotation marks.

where City="&office";  right


resolves to ....
where City="Sydney";

where City='&office';  wrong


resolves to ....
where City='&office';

where City=&office;  wrong


resolves to ....
where City=Sydney;
70
%LET Statement Examples
Example: Use macro variables to select dates.
%let date1=25may2007;
%let date2=15jun2007;
proc print data=orion.Order_Fact;
where Order_Date between "&date1"d and "&date2"d;
var Order_Date Product_ID Quantity;
title "Orders between &date1 and &date2";
run;

pf20d04
71
%LET Statement Examples
SAS Log
61 %let date1=25may2007;
62 %let date2=15jun2007;
63 proc print data=orion.Order_Fact;
64 where Order_Date between "&date1"d and "&date2"d;
65 var Order_Date Product_ID Quantity;
66 title "Orders between &date1 and &date2";
67 run;
NOTE: There were 11 observations read from the data set ORION.ORDER_FACT.
WHERE (Order_Date>='25MAY2007'D and Order_Date<='15JUN2007'D);
NOTE: PROCEDURE PRINT used (Total process time):
real time 0.00 seconds
cpu time 0.00 seconds

72
Displaying Macro Variables
Example: Display the names and values of all
user-defined macro variables in the SAS log.
%put _user_;

Partial SAS Log


175 %put _user_;
GLOBAL OFFICE Sydney
GLOBAL DATE1 25may2008
GLOBAL DATE2 15jun2008
GLOBAL UNITS 4

73
Displaying Macro Variables
The SYMBOLGEN system option writes macro variable
values to the SAS log as they are resolved.

General form of the SYMBOLGEN system option:

OPTIONS
OPTIONS SYMBOLGEN;
SYMBOLGEN;

 The default value is NOSYMBOLGEN.

74
Displaying Macro Variables
Partial SAS Log
176 options symbolgen;
177 %let office=Sydney;
178 proc print data=orion.employee_addresses;
179 where city="&office";
SYMBOLGEN: Macro variable OFFICE resolves to Sydney
180 var employee_name;
SYMBOLGEN: Macro variable OFFICE resolves to Sydney
181 title "&office Employees";
182 run;

75
Deleting User-Defined Macro Variables
The %SYMDEL statement deletes one or more user-defined macro variables from the global symbol
table.

To save memory, delete macro variables from the global symbol table when they are no longer
needed.

General form of the %SYMDEL statement:

Example: Delete the macro variables OFFICE and UNITS.

%SYMDEL
%SYMDELmacro-variables;
macro-variables;

%symdel office units;

76
77
Exercise

This exercise reinforces the concepts discussed


previously.

78
Chapter 20: Macro Variables

20.1 Introduction to Macro Variables

20.2 Macro Variable References

20.3 User-Defined Macro Variables

20.4 Delimiting Macro Variable References

20.5 Macro Functions

79
Objectives
Place a macro variable reference adjacent to:
 text

 another macro variable reference

80
Referencing Macro Variables
Macro variable references adjacent to leading and/or
trailing text:
text&variable
&variabletext
text&variabletext
Adjacent macro variable references:
&variable&variable

81
Combining Macro Variables with Text
You can place text immediately before a macro variable
reference.
Example: Data sets are stored in a SAS data library with
a naming convention of Yyyyymon.
yyyy can be 2000, 2001, 2002, and so on.
mon can be JAN, FEB, MAR, and so on.
Write an application that uses macro variables
to build SAS data set names and other
tokens.

82
Combining Macro Variables with Text
%let month=jan;
proc chart data=orion.y2000&month;
hbar week / sumvar=sale;
run;
proc plot data=orion.y2000&month;
plot sale*day;
run;

generates
proc chart data=orion.y2000jan;
hbar week / sumvar=sale;
run;
proc plot data=orion.y2000jan;
plot sale*day;
run;

83
Combining Macro Variables with Text
This example illustrates adjacent macro variable
references.
Example: Modify the previous program to allow both
month and year to be substituted.

%let year=2000;
%let month=jan;
proc chart data=orion.y&year&month;
hbar week / sumvar=sale;
run;
proc plot data=orion.y&year&month;
plot sale*day;
run;

84
Combining Macro Variables with Text
The generated program is identical to the program
in the previous example.
proc chart data=orion.y2000jan;
hbar week / sumvar=sale;
run;
proc plot data=orion.y2000jan;
plot sale*day;
run;

85
Combining Macro Variables with Text
You can place text immediately after a macro variable
reference if it does not change the reference.
Example: Modify the previous program to substitute
the name of an analysis variable.

%let year=2000;
%let month=jan;
%let var=sale;
proc chart data=orion.y&year&month;
hbar week / sumvar=&var;
run;
proc plot data=orion.y&year&month;
plot &var*day;
run;

86
Combining Macro Variables with Text
The generated program is identical to the program in the
previous example.

proc chart data=orion.y2000jan;


hbar week / sumvar=sale;
run;
proc plot data=orion.y2000jan;
plot sale*day;
run;

87
88
20.03 Quiz
Modify the previous program to allow the user to choose
either a Base SAS or SAS/GRAPH procedure.
/* GRAPHICS should be null or G */
%let graphics=g;
%let year=2000;
%let month=jan;
%let var=sale;
proc &graphicschart data=orion.y&year&month;
hbar week / sumvar=&var;
run;
proc &graphicsplot data=orion.y&year&month;
plot &var*day;
run;

What is the problem with this program?


89
20.03 Quiz – Correct Answer
SAS interprets the macro variable’s name as
GRAPHICSCHART because no delimiter separates
the macro variable reference from the trailing text.
Partial SAS Log
1 %let graphics=g;
2 %let year=2000;
3 %let month=jan;
4 %let var=sale;
5 proc &graphicschart data=orion.y&year&month;
-
10
WARNING: Apparent symbolic reference GRAPHICSCHART not resolved.

ERROR 10-205: Expecting the name of the procedure to be executed.

90
Macro Variable Name Delimiter
The word scanner recognizes the end of a macro variable
reference when it encounters a character that cannot be
part of the reference.
A period (.) is a special delimiter that ends a macro
variable reference. The period does not appear as text
when the macro variable is resolved.

91
Macro Variable Name Delimiter
Example: Correct the problem from the previous example.

%let graphics=g;
%let year=2000;
%let month=jan;
%let var=sale;
proc &graphics.chart data=orion.y&year&month;
hbar week / sumvar=&var;
run;
proc &graphics.plot data=orion.y&year&month;
plot &var*day;
run;

92
Macro Variable Name Delimiter
The generated code does not include the period.

proc gchart data=orion.y2000jan;


hbar week / sumvar=sale;
run;
proc gplot data=orion.y2000jan;
plot sale*day;
run;

93
94
20.04 Quiz
Modify the previous example to include a macro variable
that stores a libref.
%let lib=orion;
%let graphics=g;
%let year=2000;
%let month=jan;
%let var=sale;
libname &lib 'SAS-data-library';
proc &graphics.chart data=&lib.y&year&month;
hbar week / sumvar=&var;
run;
proc &graphics.plot data=&lib.y&year&month;
plot &var*day;
run;

What is the problem with this program?

95
20.04 Quiz – Correct Answer
This program
%let lib=orion;
...
libname &lib 'SAS-data-library';
proc &graphics.chart data=&lib.y&year&month;
...
generates this code:
libname orion 'sas-data-library';
proc gchart data=oriony2000jan;
hbar week / sumvar=sale;
run;
proc gplot data=oriony2000jan;
plot sale*day;
run;
The period after &LIB is interpreted as a delimiter, so
96
the period does not appear as text.
Macro Variable Name Delimiter
Use another period after the delimiter period to supply the
needed token.
%let lib=orion;
...
libname &lib 'SAS-data-library';
proc &graphics.chart data=&lib..y&year&month;
...
proc &graphics.plot data=&lib..y&year&month;

97
Macro Variable Name Delimiter
delimiter text

proc &graphics.chart data=&lib..y&year&month;

The first period is treated as a delimiter, the second


as text.

The compiler receives:


...
proc gchart data=orion.y2000jan;
...

98
99
Exercise

This exercise reinforces the concepts discussed


previously.

100
Chapter 20: Macro Variables

20.1 Introduction to Macro Variables

20.2 Macro Variable References

20.3 User-Defined Macro Variables

20.4 Delimiting Macro Variable References

20.5 Macro Functions

101
Objectives
Use macro functions to:
 manipulate text

 perform arithmetic

 mask certain characters

102
Macro Functions
Macro functions:
 have syntax similar to that of corresponding
DATA step functions
 yield similar results

 manipulate macro variables and expressions

 represent macro triggers

 are executed by the macro processor

 do not require quotation marks around character


constant arguments

103
Macro Functions
Selected character string manipulation functions:
%UPCASE translates letters from lowercase to uppercase.
%SUBSTR extracts a substring from a character string.

%SCAN extracts a word from a character string.

Other functions:
%EVAL performs arithmetic and logical operations.
%STR quotes special characters.

104
Macro Functions
Arguments to macro string manipulation functions can be
any text and/or macro triggers:
 constant text
 macro variable references
 macro functions
 macro calls

Constant text arguments do not require quotation marks.

105
Macro Functions
Example: Use macro functions to avoid hardcoding the
current year and most recently created data
set name.
data orders;
set orion.Orders;
where year(Order_Date)= 2007;
Lag=Delivery_Date - Order_Date;
run;

proc means data=orders maxdec=2 min max mean;


class Order_Type;
var Lag;
title "Report based on ORDERS data";
run;

m102d05a
106
The %SUBSTR Function
General form of the %SUBSTR function:

%SUBSTR(argument,
%SUBSTR(argument, position
position<,n>)
<,n>)

The %SUBSTR function returns the portion of argument


beginning at position :
 for a length of n characters, if n is specified.

 to the end of argument , if n is not specified.

107
The %SUBSTR Function
Example: Substring the year portion of SYSDATE9.
%put sysdate9=&sysdate9;
%let year=%substr(&sysdate9,6);
%put year=&year;

SAS Log
165 %put sysdate9=&sysdate9;
sysdate9=16JAN2008
166 %let year=%substr(&sysdate9,6);
167 %put year=&year;
year=2008

108
109
20.05 Multiple Choice Poll
What is the value of X after %LET statement execution?
%let X=%substr("ABCD",2,1);
a. A
b. B
c. C
d. D

110
20.05 Multiple Choice Poll – Correct Answer
What is the value of X after %LET statement execution?
%let X=%substr("ABCD",2,1);
a. A
b. B
c. C
d. D

111
The %SCAN Function
General form of the %SCAN function:

%SCAN(argument,
%SCAN(argument, nn<,delimiters>)
<,delimiters>)

The %SCAN function:


 returns the nth word of argument, where words are
strings of characters separated by delimiters
 uses a default set of delimiters if none are specified

 returns a null string if there are fewer than n words in


argument

112
The %SCAN Function
Example: Scan SYSLAST for a one-level data set name.
%put syslast=&syslast;
%let dsn=%scan(&syslast,2,.);
%put dsn=&dsn;

SAS Log
41 %put syslast=&syslast;
syslast=WORK.ORDERS
42 %let dsn=%scan(&syslast,2,.);
43 %put dsn=&dsn;
dsn=ORDERS

113
Macro Functions
Step 1: Write and debug a program with hardcoded
constants.
data orders;
set orion.Orders;
where year(Order_Date)=2007;
Lag=Delivery_Date - Order_Date;
run;

proc means data=orders maxdec=2 min max mean;


class Order_Type;
var Lag;
title "Report based on ORDERS data";
run;

m102d05a
114
Macro Functions
Step 2: Use macro functions to manipulate automatic
macro variables for substitution within SAS code.
data orders;
set orion.Orders;
where year(Order_Date)=%substr(&sysdate9,6);
Lag=Delivery_Date - Order_Date;
run;

proc means data=orders maxdec=2 min max mean;


class Order_Type;
var Lag;
title "Report based on %scan(&syslast,2,.) data";
run;

m102d05b
115
Macro Functions
PROC MEANS Output
Report based on ORDERS data

Analysis Variable : Lag

N
Order Type Obs Minimum Maximum Mean
ƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒ
1 70 0.00 10.00 0.43

2 15 2.00 10.00 4.27

3 31 1.00 7.00 3.87

ƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒ

116
The %EVAL Function
General form of the %EVAL function:

%EVAL(expression)
%EVAL(expression)

The %EVAL function:


 performs arithmetic and logical operations

 truncates non-integer results

 returns a text result

 returns 1 (true) or 0 (false) for logical operations

 returns a null value and issues an error message


when non-integer values are used in arithmetic
operations

117
The %EVAL Function
Example: Perform simple integer arithmetic.
%let x=%eval(2+2);
%put x=&x;

SAS Log
154 %let x=%eval(2+2);
155 %put x=&x;
x=4

118
The %EVAL Function
Example: Compute the first year of a range based on the
current date.

%let thisyr=%substr(&sysdate9,6);
%let lastyr=%eval(&thisyr-1);
proc means data=orion.order_fact maxdec=2 min max mean;
class order_type;
var total_retail_price;
where year(order_date) between &lastyr and &thisyr;
title1 "Orders for &lastyr and &thisyr";
title2 "(as of &sysdate9)";
run;

m102d06
119
The %EVAL Function
PROC MEANS Output
Orders for 2007 and 2008
(as of 24JAN2008)

The MEANS Procedure

Analysis Variable : Total_Retail_Price Total Retail Price for This


Product

N
Order Type Obs Minimum Maximum Mean
ƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒ
1 89 3.20 1066.40 122.01

2 19 8.20 1937.20 301.01

3 40 9.60 702.00 168.28


ƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒ

120
121
20.06 Quiz
What is wrong with this attempt to store a SAS statement
in a macro variable?

%let statement=title 'S&P 500';

122
20.06 Quiz – Correct Answer
What is wrong with this attempt to store a SAS statement
in a macro variable?

%let statement=title 'S&P 500';

The semicolon ends the %LET statement.


The stored TITLE statement lacks a semicolon.

123
The %STR Function
The %STR function masks (removes the normal meaning of)
these special tokens:

+ - * / , < > = ; ' "


LT EQ GT LE GE NE AND OR NOT

blank
General form of the %STR function:

%STR(argument)
%STR(argument)

argument can be any combination of text and macro


triggers.

124
The %STR Function
The %STR function:
 masks tokens so the macro processor does not
interpret them as macro-level syntax
 enables macro triggers to work normally

 preserves leading and trailing blanks in its argument

 masks an unpaired quotation mark or parenthesis in


its argument when the quotation mark or parenthesis
is immediately preceded by a percent sign (%)

125
The %STR Function
Example: Store a SAS statement in a macro variable.
%let statement=%str(title 'S&P 500';);

SAS Log
3 %let statement=%str(title 'S&P 500';);

pf20d08
126
127
Exercise

This exercise reinforces the concepts discussed


previously.

128

Das könnte Ihnen auch gefallen