340 likes | 520 Views
How Sas Processes StatementS. Advanced SAS Adapted from JE Blum, UNCW and the SAS 9.3 Macro Language Reference. An Example. Here is code that will produce summary statistics. %LET DSN=sasuser.diabetes; %LET CLASS=SEX; %LET variables=age height weight pulse;
E N D
How Sas Processes StatementS Advanced SAS Adapted from JE Blum, UNCW and the SAS 9.3 Macro Language Reference
An Example • Here is code that will produce summary statistics. %LET DSN=sasuser.diabetes; %LET CLASS=SEX; %LET variables=age height weight pulse; Title 'Summary Statistics'; procmeans data=&DSN maxdec=2; class &CLASS; var &variables; run; quit;
Macro Variables • The Macro variables DSN, Class, and Variables store information that will be used to make code “dynamic”. • All macro variables are character (special functions will be required to do numerical operations). • Macro variables follow the same naming conventions as other variables; however, they are always preceded by an ampersand (&) when referenced.
What is the Macro Facility Doing? • When the SAS program runs, the macro language’s primary function is as a text substitution facility and/or code generator. • To fully understand how macro processing works, we have to look at how SAS processes submitted code.
Program Processing and Flow • A SAS program can be any combination of: • Global statements • Data steps and PROC steps • Structured Query Language (SQL) • SAS Component Language (SCL) • SAS macro language • All submitted programs, regardless of content, are delivered to the input stack.
Compilation and Execution • When the code is delivered to the input stack, SAS… • Reads text in the input stack • Routes text to the appropriate compiler • Suspends activity when a step boundary is reached. • Executes compiled code if there are no errors • Repeats as necessary.
The Word Scanner--Tokenization • The word scanner breaks up the text into units known as tokens. • The process (open code): • The word scanner passes tokens to the compiler, and the compiler requests tokens until it receives a semicolon. • The compiler performs a syntax check on the statement.
Tokens • There are four types of tokens: • Literals: Any quoted string “Any text string” ‘Any text string’ • Numbers: Strings of digits. May include decimal points and scientific notation 23 23.7 .7 23.7E-10 • Names: Strings of characters beginning with a letter or underscore libname _n_ mmddyy10. fred • Special: Special characters $ ( ) . & %
Macro Processing Compiler Macro Processor Word Scanner %LET Input Stack DSN=sasuser.diabetes; %LET CLASS=SEX; %LET variables=age height weight pulse; Title 'Summary Statistics'; procmeans data=&DSN maxdec=2; class &CLASS; var &variables; run; quit;
Macro Processing Compiler Macro Processor %LET Word Scanner %LET The % acts as a macro trigger. The % and the following name token are sent to the macro processor. Input Stack DSN=sasuser.diabetes; %LET CLASS=SEX; %LET variables=age height weight pulse; Title 'Summary Statistics'; procmeans data=&DSN maxdec=2; class &CLASS; var &variables; run;
Macro Processing Compiler Macro Processor %LET Word Scanner DSN = Sasuser . Diabetes ; The word scanner will continue to deliver tokens to the macro processor until a semicolon is reached Input Stack DSN=sasuser.diabetes; %LET CLASS=SEX; %LET variables=age height weight pulse; Title 'Summary Statistics'; procmeans data=&DSN maxdec=2; class &CLASS; var &variables; run;
Macro Processing Compiler Macro Processor Word Scanner Symbol Table Variable Value DSN SASUSER.DIABETES Input Stack %LET CLASS=SEX; %LET variables=age height weight pulse; Title 'Summary Statistics'; procmeans data=&DSN maxdec=2; class &CLASS; var &variables; run; The %let statement assigns a value to the macro variable
Process continues with other two %LET Statements Compiler Macro Processor Word Scanner Symbol Table Variable Value DSN SASUSER.DIABETES CLASS SEX VARIABLES age height weight pulse Input Stack Title 'Summary Statistics'; procmeans data=&DSN maxdec=2; class &CLASS; var &variables; run;
Tokens process up to first macro variable Compiler Title 'Summary Statistics'; procmeans data= Macro Processor Word Scanner &DSN Symbol Table Variable Value DSN SASUSER.DIABETES CLASS SEX VARIABLES age height weight pulse Input Stack maxdec=2; class &CLASS; var &variables; run;
Value of &DSN found in Symbol table Compiler Title 'Summary Statistics'; procmeans data= Macro Processor &DSN Word Scanner Symbol Table Variable Value DSN SASUSER.DIABETES CLASS SEX VARIABLES age height weight pulse Input Stack maxdec=2; class &CLASS; var &variables; run;
Value of &DSN found in Symbol table Compiler Title 'Summary Statistics'; procmeans data=SASUSER.DIABETES; Macro Processor &DSN Word Scanner Symbol Table Variable Value DSN SASUSER.DIABETES CLASS SEX VARIABLES age height weight pulse Input Stack maxdec=2; class &CLASS; var &variables; run;
Substitutions continue for other tokens and macro variables Compiler Title 'Summary Statistics'; procmeans data=SASUSER.DIABETES maxdec=2; class Macro Processor Word Scanner &CLASS Symbol Table Variable Value DSN SASUSER.DIABETES CLASS SEX VARIABLES age height weight pulse Input Stack ; var &variables; run;
Until compiler has complete program with substitutions made. Compiler Title 'Summary Statistics'; procmeans data=SASUSER.DIABETES maxdec=2; class; var age height weight pulse; run; Macro Processor Symbol Table Word Scanner Variable Value DSN SASUSER.DIABETES CLASS SEX VARIABLES age height weight pulse Input Stack
End of boundary found (RUN;) so program executes… Compiler Title 'Summary Statistics'; procmeans data=SASUSER.DIABETES maxdec=2; class; var age height weight pulse; run; Macro Processor Symbol Table Word Scanner Variable Value DSN SASUSER.DIABETES CLASS SEX VARIABLES age height weight pulse Input Stack
Automatic Macro Variables • By default, SAS has several macro variables established on its own. These variables • are created at the invocation of the SAS session • are global (always available) • some have been assigned values by SAS (some are null) • values can be re-assigned by users in some cases
Viewing Macro Variables • The macro language has its own “put” statement. • Syntax: %put text; • Writes a new line to the SAS Log • No quotes are required around text • Resolves any macro triggers in text • Can be used in open code
Viewing Automatic Macro Variables • The statement: Writes names and values of all automatic macro variables to the Log. Put that statement in the code, run, and look at LOG… %put _automatic_;
How SAS Compiles a Macro • Similar to previous example – reads from Input Stack into Word Scanner creates compiled macro in catalog • See pages 34ff in SAS Macro reference • Suppose this code is in the Input Stack %macro simple(dsn=,variable=); proc means data=&DSN; var &variable; run; %mend simple; %simple(dsn="C:\sasdata\somedata",variable=time1-time4);
Similar to previous example, Input Stack is read into Word Scanner. It is then processed and put into a Macro Catalog Macro Catalog Macro Processor Word Scanner Symbol Table Input Stack %macro simple(dsn=,variable=); proc means data=&DSN; var &variable; run; %mend simple; %simple(dsn="C:\sasdata\somedata", variable=time1-time4);
The Macro is now in the catalog Macro Catalog Macro Processor %macro simple(dsn=,variable=); proc means data=&DSN; var &variable; run; %mend simple; Word Scanner Symbol Table Input Stack %simple(dsn="C:\sasdata\somedata", variable=time1-time4); Lines remaining in input stack
The Macro Processor recognizes this code as a Macro call, begins execution, creates a symbol table… Macro Catalog Macro Processor %macro simple(dsn=,variable=); proc means data=&DSN; var &variable; run; %mend simple; Word Scanner Symbol Table Dsn "C:\sasdata\somedata“ Variable time1-time4 Input Stack %simple(dsn="C:\sasdata\somedata", variable=time1-time4);
The data=&DSN in put in the input stack, scanned, processed, and the value of &DSN is read form the symbol table and placed into the macro code. Macro Catalog Macro Processor %macro simple(dsn=,variable=); proc means data="C:\sasdata\somedata“ ; var &variable; run; %mend simple; Word Scanner Symbol Table Dsn "C:\sasdata\somedata“ Variable time1-time4 Input Stack data=&DSN
Once all macro variables are resolved, code in macro is run proc means data="C:\sasdata\somedata“; var time1-time4; run; And SAS returns to the remaining code stream… Note : %IF, %DO and other macro statements may make the compile process more complicated. This was a simple example.
Scopes of Macro Variables • Global – Exist for the duration of the SAS session and can be referenced anywhere • Local – exists only during the exception of the macro and have no value outside the macro • Scopes can be nested • Macro variables are stored in symbol tables, and these tables can have global or local status • Note: The %SYMEXIST function can be used to determine if a variable is currently available.
Global Macro Variables Example %Let address=1028 madison avenue; data _NULL_; file print; put "%upcase(&address)"; put "%propcase(&address)"; put "%scan(&address,1)"; put "%length(&address)"; run;