Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
1
2
2
Objectives
State the purpose of the macro facility.
List programming applications where the macro facility
might be useful.
3
Purpose of the Macro Facility
The macro facility is a text processing facility for generating
SAS code. The macro facility helps minimize the amount of
SAS code you must type to perform common tasks.
4
Purpose of the Macro Facility
The macro facility enables you to:
create and resolve macro variables anywhere within
a SAS program
write and call macro programs (macro definitions or
macros) that generate custom SAS code
5
Substituting User-Defined
Values
Example: Reference the same value repeatedly within
a program.
proc freq data=orion.order_fact;
where year(order_date)=2008;
table order_type;
title "Order Types for 2008";
run;
proc means data=orion.order_fact;
where year(order_date)=2008;
class order_type;
var Total_Retail_Price;
title "Price Statistics for 2008";
run;
9
10
2
11
Objectives
Identify the tokens in a SAS program.
Describe how a SAS program is tokenized, compiled,
and executed.
12
Program Flow
A SAS program can be any combination of:
DATA and PROC steps
global statements
13
Program Flow
Input
Input Stack
Stack
Batch or
SUBMIT Stored Noninteractive
Command Process Submission
data
data bonus;
bonus; %STPBEGIN;
%STPBEGIN; //SYSIN
//SYSIN DD
DD **
set orion.staff;
set orion.staff; proc
proc print
print data=new;
data=new; options
options nodate;
nodate;
bonus=salary*.1;
bonus=salary*.1; run;
run; proc sql;
proc sql;
run;
run; proc
proc means
means data=new;
data=new; select
select **
proc
proc print;
print; run;
run; from
from orion.staff;
orion.staff;
run;
run; %STPEND;
%STPEND; quit;
quit;
14
Program Flow
When SAS code is in the input stack, a component of
SAS called the word scanner :
reads the text in the input stack, character by
character, left to right, top to bottom
breaks the text into fundamental units called tokens
data
data
Word bonus
bonus
;;
Scanner
set
set orion.staff;
orion.staff;
bonus=salary*.1;
bonus=salary*.1;
Input run;
run;
proc
proc print;
print;
Stack run;
run;
15
Program Flow
The word scanner passes the tokens, one at a time, to
the appropriate compiler, as the compiler demands.
data
data bonus;
Compiler bonus;
set
set
Word orion
orion
..
Scanner staff
staff
;;
bonus=salary*.1;
Input Stack run;
bonus=salary*.1;
run;
proc
proc print;
print;
run;
run;
16
Program Flow
The compiler:
requests tokens until it receives a semicolon
performs a syntax check on the statement
repeats this process for each statement
SAS:
suspends compilation when a step boundary is
encountered
executes the compiled code if there are no
compilation errors
repeats this process for each step
17
Tokenization
The word scanner recognizes four classes of tokens:
literals
numbers
names
special tokens
18
Literal Tokens
A literal token is a string of characters enclosed in
quotation marks.
19
Number Tokens
Number tokens can be
integers, including SAS date constants
floating point numbers, containing a decimal point
and/or exponent.
Examples: 3
3.
3.5
-3.5
'01jan2009'd
5E8
7.2E-4
20
Name Tokens
Name tokens contain one or more characters beginning
with a letter or underscore and continuing with
underscores, letters, or the digits 0-9.
Examples: infile
_n_
item3
univariate
dollar10.2
21
Special Tokens
Special tokens can be any character, or combination of
characters, other than a letter, digit, or underscore.
Examples: * / + - ** ; $ ( ) . & % @ # = ||
22
Tokenization
A token ends when the word scanner detects:
the beginning of another token
a blank
23
Example
Input Stack var
var x1-x10
x1-x10 zz ;;
Tokens 1. var
2. x1
3. -
4. x10
5. z
6. ;
24
Example
Input Stack title 'Report for May';
Tokens 1. title
2. 'Report for May'
3. ;
25
26
19.01 Multiple Choice Poll
When is SAS code executed?
a. Before the input stack
b. After the input stack and before the word scanner
c. After the word scanner and before compilation
d. At a step boundary after compilation
e. None of the above
27
19.01 Multiple Choice Poll –
Correct Answer
When is SAS code executed?
a. Before the input stack
b. After the input stack and before the word scanner
c. After the word scanner and before compilation
d. At a step boundary after compilation
e. None of the above
28
Macro Triggers
During word scanning, two special token sequences are
recognized as macro triggers:
%name-token a macro statement, function, or call
&name-token a macro variable reference
29
Macro Statements
Macro statements:
begin with a percent sign (%), followed by a name
token
end with a semicolon
30
The %PUT Statement
The %PUT statement writes:
text to the SAS log
%PUT
%PUTtext;
text;
31
The %PUT Statement
Example: Use a %PUT statement to write text to the
SAS log.
32
33
19.02 Quiz
In the example below, why are no quotation marks used?
%PUT
%PUT Hi
Hi Mom!;
Mom!;
34
19.02 Quiz – Correct Answer
In the example below, why are no quotation marks used?
%PUT
%PUT Hi
Hi Mom!;
Mom!;
Because the macro facility is a text processing facility, quotation marks are
not required to distinguish text.
35
The Macro Processor
The macro processor handles macro triggers, including
macro language statements, macro functions, macro calls,
and macro variable resolution.
Compiler
Macro Processor
Word
Scanner
Input
Stack
36
Program Flow
The %PUT statement is submitted.
Compiler
Macro Processor
Word
Scanner
Input %put
%put Hi
Hi Mom!;
Mom!;
Stack
37
Program Flow
The statement is tokenized.
Compiler
Macro Processor
Word %%
Scanner put
put
Hi
Hi
Mom
Mom
!!
;;
Input
Stack
38
Program Flow
When a macro trigger is encountered, it is passed to the
macro processor for evaluation.
Compiler
Macro Processor
Word %put
%put
Scanner
Hi
Hi
Mom
Mom
!!
;;
Input
Stack
39
Program Flow
The macro processor recognizes the trigger as the beginning
of a macro statement, and requests tokens until a semicolon is
encountered. It then executes the statement.
Compiler
Macro Processor
Word %put
%put Hi
Hi Mom!;
Mom!;
Scanner
Input
Stack
40
41
Exercise
42