1 / 33

How Sas Processes StatementS

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;

clive
Download Presentation

How Sas Processes StatementS

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. How Sas Processes StatementS Advanced SAS Adapted from JE Blum, UNCW and the SAS 9.3 Macro Language Reference

  2. 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;

  3. 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.

  4. 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.

  5. 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.

  6. 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.

  7. 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.

  8. 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 $ ( ) . & %

  9. 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;

  10. 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;

  11. 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;

  12. 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

  13. 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;

  14. 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;

  15. 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;

  16. 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;

  17. 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;

  18. 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

  19. 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

  20. Program now runs…

  21. 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

  22. Some Automatic Macro Variables

  23. 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

  24. 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_;

  25. 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);

  26. 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);

  27. 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

  28. 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);

  29. 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

  30. 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.

  31. 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.

  32. 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;

  33. END

More Related