Upload
janardhan-reddy
View
230
Download
0
Embed Size (px)
Citation preview
8/3/2019 Scott Bass Sas Macros
http://slidepdf.com/reader/full/scott-bass-sas-macros 1/38
Macro:So What’s Really Going On?
Scott BassSenior ITG Associate
Macquarie Group Services Australia
8/3/2019 Scott Bass Sas Macros
http://slidepdf.com/reader/full/scott-bass-sas-macros 2/38
Agenda
Review of SAS compilation process
What is a macro and what does it do?
Details of SAS compilation process
Macro quoting
Function style macros
My Utility Macro Toolkit
8/3/2019 Scott Bass Sas Macros
http://slidepdf.com/reader/full/scott-bass-sas-macros 3/38
Review...
SAS is a compiled, not interpreted, language
SAS does “just in time” compilation before executing
compiled program
• http://en.wikipedia.org/wiki/Just-in-time_compilation
• http://www.gnu.org/software/dotgnu/libjit-doc/libjit_1.html
Compilation occurs at step boundaries, then compiled code
object is executed. 1
Most novice macro errors I see are timing errors due to alack of understanding of the compiled nature of a SASprogram.
1 See Steven First, Katie Ronk, SUGI 31, Paper 107-31, p. 1 for more details.
8/3/2019 Scott Bass Sas Macros
http://slidepdf.com/reader/full/scott-bass-sas-macros 4/38
Review (cont)...
Four phases in a SAS program compilation:
• Macro compilation
• Macro execution
• SAS code compilation 1
• SAS code execution
1 Technically a PROC step is code parsing, not compilation.
Source: Ian Whitlock, SUGI 30, Paper 252-30, p. 2.
8/3/2019 Scott Bass Sas Macros
http://slidepdf.com/reader/full/scott-bass-sas-macros 5/38
So what is a macro?
A macro is analogous to pre-compiler directives in sayC/C++ (but much more powerful)
http://www.devarticles.com/c/a/Cplusplus/C-plus-plus-Preprocessor-The-Code-in-the-Middle/
“Before the compiler interprets your code, the preprocessor handles it.Its task is to scan through your code and to look for preprocessorinstructions; these can for example be used to replace certaintokens with string or numerical values, or to blank out completesections of the code before the compiler sees it. You can recognize
these instructions from the pound (#) symbol that needs to precedeevery preprocessor instruction.”
“We'll first look at what the preprocessor is used for most: stringsubstitution and conditional compilation.”
Sound familiar?
8/3/2019 Scott Bass Sas Macros
http://slidepdf.com/reader/full/scott-bass-sas-macros 6/38
So what is a macro? (cont.)
So, a macro...
• Controls the code that gets passed to thecompiler
• Conditional code generation and execution
• Can use SAS data as metadata via the datastep and PROC SQL interfaces, almostalways for use in the next step boundary.
• It is this last point that makes macro so powerful...but also more complex due to timing issues and intermixing of two distinct languages.
8/3/2019 Scott Bass Sas Macros
http://slidepdf.com/reader/full/scott-bass-sas-macros 7/38
How does SAS compile a program?
Input Stack
Word Scanner
• Tokenization (process done by Word Scanner)Macro Processor
SAS Compiler
Source: http://support.sas.com/onlinedoc/913/getDoc/en/mcrolref.hlp/a001071826.htm
8/3/2019 Scott Bass Sas Macros
http://slidepdf.com/reader/full/scott-bass-sas-macros 8/38
How does SAS compile a program?
Source: http://support.sas.com/onlinedoc/913/getDoc/en/mcrolref.hlp/a001071826.htm
8/3/2019 Scott Bass Sas Macros
http://slidepdf.com/reader/full/scott-bass-sas-macros 9/38
How does SAS compile a program?
Source: http://support.sas.com/onlinedoc/913/getDoc/en/mcrolref.hlp/a001071821.htm
How SAS Processes Statements without Macro Activity
8/3/2019 Scott Bass Sas Macros
http://slidepdf.com/reader/full/scott-bass-sas-macros 10/38
How does SAS compile a program?
Source: http://support.sas.com/onlinedoc/913/getDoc/en/mcrolref.hlp/a001071821.htm
How SAS Processes Statements without Macro Activity
8/3/2019 Scott Bass Sas Macros
http://slidepdf.com/reader/full/scott-bass-sas-macros 11/38
How does SAS compile a program?
Source: http://support.sas.com/onlinedoc/913/getDoc/en/mcrolref.hlp/a001071821.htm
How SAS Processes Statements without Macro Activity
8/3/2019 Scott Bass Sas Macros
http://slidepdf.com/reader/full/scott-bass-sas-macros 12/38
The Macro Processor and Symbol Table
How does SAS compile a program?
Source: http://support.sas.com/onlinedoc/913/getDoc/en/mcrolref.hlp/a001071824.htm
How SAS Processes Statements with Macro Activity
8/3/2019 Scott Bass Sas Macros
http://slidepdf.com/reader/full/scott-bass-sas-macros 13/38
The Macro Processor Examines LET
How does SAS compile a program?
Source: http://support.sas.com/onlinedoc/913/getDoc/en/mcrolref.hlp/a001071824.htm
How SAS Processes Statements with Macro Activity
8/3/2019 Scott Bass Sas Macros
http://slidepdf.com/reader/full/scott-bass-sas-macros 14/38
The Macro Processor Writes to the Symbol Table
How does SAS compile a program?
Source: http://support.sas.com/onlinedoc/913/getDoc/en/mcrolref.hlp/a001071824.htm
How SAS Processes Statements with Macro Activity
8/3/2019 Scott Bass Sas Macros
http://slidepdf.com/reader/full/scott-bass-sas-macros 15/38
The Word Scanner Resumes Tokenization
How does SAS compile a program?
Source: http://support.sas.com/onlinedoc/913/getDoc/en/mcrolref.hlp/a001071824.htm
How SAS Processes Statements with Macro Activity
8/3/2019 Scott Bass Sas Macros
http://slidepdf.com/reader/full/scott-bass-sas-macros 16/38
The Macro Processor Examines &FILE
How does SAS compile a program?
Source: http://support.sas.com/onlinedoc/913/getDoc/en/mcrolref.hlp/a001071824.htm
How SAS Processes Statements with Macro Activity
8/3/2019 Scott Bass Sas Macros
http://slidepdf.com/reader/full/scott-bass-sas-macros 17/38
The Macro Processor Generates Text to the Input Stack
What if the token had been &&FILE&I? (&FILE1=in1, &I=1)
How does SAS compile a program?
Source: http://support.sas.com/onlinedoc/913/getDoc/en/mcrolref.hlp/a001071824.htm
How SAS Processes Statements with Macro Activity
8/3/2019 Scott Bass Sas Macros
http://slidepdf.com/reader/full/scott-bass-sas-macros 18/38
The Word Scanner Completes Processing
How does SAS compile a program?
Source: http://support.sas.com/onlinedoc/913/getDoc/en/mcrolref.hlp/a001071824.htm
How SAS Processes Statements with Macro Activity
8/3/2019 Scott Bass Sas Macros
http://slidepdf.com/reader/full/scott-bass-sas-macros 19/38
How does SAS compile a program?
Summary:
• Macro processor interacts with the input stack, wordscanner, and symbol table.
• It does not directly interact with the compiler
(indirectly through the input stack).
• It therefore does affect the code seen by the compiler.
• ALL macro processor interaction is TEXT.
o In fact, I tend to think of it as without data type
oYou MUST understand how SAS compiles aprogram, and the timing issues involved, tosuccessfully use macro.
8/3/2019 Scott Bass Sas Macros
http://slidepdf.com/reader/full/scott-bass-sas-macros 20/38
Typical novice error
data two;
set one;
by keyvar;
retain num 0;
if last.keyvar then do;
num+1;
call symputx("mvar"||put(num,best.-L),var);
call symputx("num",num);
end;
if &&mvar&num > 100 then stop;
run;
8/3/2019 Scott Bass Sas Macros
http://slidepdf.com/reader/full/scott-bass-sas-macros 21/38
Macro Quoting
Macro quoting is NOT the same as data step quoting.
• Data step quoting is a directive to the compiler that thistoken is a character constant.
• Macro quoting is the use of macro functions to hidemacro processor directives from the macro processor.
• Non-printing control characters, called deltacharacters, are used to accomplish this. 1
• Sure sign of a macro newbie:%IF “&VAR” = “something” ...
• Need to consider two environments:
oMacro compile time
oMacro execution time
1 Source: http://support.sas.com/onlinedoc/913/getDoc/en/mcrolref.hlp/a001061351.htm
8/3/2019 Scott Bass Sas Macros
http://slidepdf.com/reader/full/scott-bass-sas-macros 22/38
Macro Special Characters
“Macro quoting functions tell the macro processor
to interpret special characters and mnemonics astext rather than as part of the macro language.” 1
Special characters and mnemonics:
1 Source: http://support.sas.com/onlinedoc/913/getDoc/en/mcrolref.hlp/tw3514-overvwqt.htm
blank ) = NE; ( | LE
¬ + # LT
^ -- AND GE
~ * OR GT, (comma) / NOT
' < IN
" > EQ
8/3/2019 Scott Bass Sas Macros
http://slidepdf.com/reader/full/scott-bass-sas-macros 23/38
Macro Quoting Functions
Summary of Special Characters and Macro Quoting Functions
By Item Group ItemsMacro QuotingFunctions
A + -- */<>=¬^|~;, #blank AND OR NOTEQ NE LE LT GE GT
IN
all
B &% %NRSTR,%NRBQUOTE,%SUPERQ,%NRQUOTE
C unmatched' "() %BQUOTE,%NRBQUOTE,%SUPERQ, %STR*,%NRSTR*,%QUOTE*,%NRQUOTE*
Source: http://support.sas.com/onlinedoc/913/getDoc/en/mcrolref.hlp/a001061345.htm
8/3/2019 Scott Bass Sas Macros
http://slidepdf.com/reader/full/scott-bass-sas-macros 24/38
Macro Quoting FunctionsBy Function Affects Groups Works at
%STR A, C* macro compilation
%NRSTR A, B, C* macro compilation
%BQUOTE A, C macro execution
%NRBQUOTE A, B, C macro execution
%SUPERQ A, B, C macro execution (preventsresolution)
%QUOTE A, C* macro execution. Requiresunmatched quotation marksand parentheses to bemarked with a percent sign(%).
%NRQUOTE A, B, C* macro execution. Requiresunmatched quotation marks
and parentheses to bemarked with a percent sign(%).
*Unmatched quotation marks and parentheses must be marked with a percent sign (%)when used with %STR, %NRSTR, %QUOTE, and %NRQUOTE.
Source: http://support.sas.com/onlinedoc/913/getDoc/en/mcrolref.hlp/a001061345.htm
8/3/2019 Scott Bass Sas Macros
http://slidepdf.com/reader/full/scott-bass-sas-macros 25/38
Macro Quoting Functions
Mnemonics that help me remember the quoting functions:
• “B” = “batch” macro execution time
• Or simply, %STR and %NRSTR are macrocompilation time, all others are macro execution time
• “NR” = “no resolve” (Item Group B - % &)
Function Mnemonic Masks Timing
%STR Nothing (not B) A, C* Macro compilation
%QUOTE Nothing (not B) A, C* Macro execution(there’s always an exception to the rule!)
%BQUOTE “B” = “batch” A, C Macro execution
%NRSTR Nothing (not B)“NR” = “no resolve”
A, B, C* Macro compilation
%NRQUOTE Nothing (not B)“NR” = “no resolve”
A, B, C* Macro execution(another exception to the rule)
%NRBQUOTE “B” = “batch”
“NR” = “no resolve”
A, B, C Macro execution
%SUPERQ N/A - this one’s just
“Super”!
A, B, C Macro execution (prevents resolution)
8/3/2019 Scott Bass Sas Macros
http://slidepdf.com/reader/full/scott-bass-sas-macros 26/38
Sample Program w/o macro token
%let mvar=A;%let str=%str(A);%let nrstr=%nrstr(A);
%let quote=%quote(A);%let unquote=%unquote(&str);%let example=%nrbquote( 1 + 1 = 3 Today's Test and More );
* continued ... ;
8/3/2019 Scott Bass Sas Macros
http://slidepdf.com/reader/full/scott-bass-sas-macros 27/38
Sample Program w/o macro token
proc sql;select
name, value, value as hexvalue format=$hex8.
from dictionary.macros
wherename in
(“MVAR”,”STR”,”NRSTR”,”QUOTE”,”UNQUOTE”,
”EXAMPLE”)
;
quit; /* See footnote */
Avoids implicit macro variable unquoting: “Some characters in the
above value which were subject to macro quoting have beenunquoted for printing.”
Source: http://www.listserv.uga.edu/cgi-bin/wa?A2=ind0605D&L=sas-l&D=0&P=59246
HUGE thread on macro quoting.
8/3/2019 Scott Bass Sas Macros
http://slidepdf.com/reader/full/scott-bass-sas-macros 28/38
Sample Program w/o macro token
proc sql;select
name, value
from dictionary.macros
wherename = “A”;quit;
%symdel MVAR STR NRSTR QUOTE UNQUOTE;
8/3/2019 Scott Bass Sas Macros
http://slidepdf.com/reader/full/scott-bass-sas-macros 29/38
Sample Program Results
8/3/2019 Scott Bass Sas Macros
http://slidepdf.com/reader/full/scott-bass-sas-macros 30/38
L-Value vs. R-Value
1 Source: Harry Droogendyk, Marje Fecht, SUGI 31, Paper 251-31, p. 12
References:
• http://en.wikipedia.org/wiki/Value_(computer_science)
• http://www.cutthecrap.biz/software/whitepapers/notanlvalue.html
In simple terms, an R-value is something that can go on the
right side of an equation (equal sign)
“Pure macro”, i.e. code completely resolved by the macro
processor and not sent to the compiler, can generate a R-value. This is much easier with the advent of the %sysfuncfunction.
Known as function-style macros. 1
Changes the “contract” between the calling code and the
called code (macro).
8/3/2019 Scott Bass Sas Macros
http://slidepdf.com/reader/full/scott-bass-sas-macros 31/38
Example L-Value vs. R-Value
L-Value:
%macro nobs_lvalue(data=, mvar=nobs);
%* this is required to use outside the macro ;
%global &mvar;
data _null_;if 0 then set &data nobs=nobs;
call symputx("&mvar",nobs);
stop;
run;
%mend;
%nobs_lvalue(data=sashelp.class,mvar=nobs_lvalue);
%put >>> &nobs_lvalue <<< ;
8/3/2019 Scott Bass Sas Macros
http://slidepdf.com/reader/full/scott-bass-sas-macros 32/38
Example L-Value vs. R-Value
R-Value:
%macro nobs_rvalue(data=);
%let dsid=%sysfunc(open(&data));
%if (&dsid) %then %do;
%let nobs=%sysfunc(attrn(&dsid,nobs));%let dsid=%sysfunc(close(&dsid));
%end;
%else %do;
%put Unable to open &data;
%let nobs=-1;
%end;
%* Note: no terminating semi-colon! ;
%unquote(&nobs)
%mend;
8/3/2019 Scott Bass Sas Macros
http://slidepdf.com/reader/full/scott-bass-sas-macros 33/38
Example L-Value vs. R-Value
R-Value:
%let MyMVar=%nobs_rvalue(data=sashelp.zipcode);
%put >>> &MyMVar <<<;
%put %nobs_rvalue(data=sashelp.column);
data _null_;if %nobs_rvalue(data=sashelp.doesnotexist) gt 0
then put "Not empty";
run;
Only a code fragment is returned.
The calling program is responsible for calling the functionstyle macro in a syntactically correct manner.
No need for global macro variable.
8/3/2019 Scott Bass Sas Macros
http://slidepdf.com/reader/full/scott-bass-sas-macros 34/38
My Utility Macro Toolkit
Available for download from the SNUG website are anumber of my utility macros from years of SASprogramming.
My very favourite macros are:
• PARMV – macro parameter validation macro(original author: Tom Hoffman)
• LOOP – loops over a list of tokens calling a child macro foreach token
• SEPLIST – builds a separated list of items
(original author: Richard Devenezia)• NOBS – efficient code to return the number of observations
I also believe the macro header is very well structured andworth a review.
8/3/2019 Scott Bass Sas Macros
http://slidepdf.com/reader/full/scott-bass-sas-macros 35/38
ReferencesGetting Started With Macro , Ian Whitlockhttp://www2.sas.com/proceedings/sugi24/Begtutor/p63-24.pdf
A Serious Look at Macro Quoting , Ian Whitlock (excellent!)http://www2.sas.com/proceedings/sugi28/011-28.pdf
SAS® Macro Design Issues , Ian Whitlock
http://www2.sas.com/proceedings/sugi27/p067-27.pdf
A Second Look at SAS ® Macro Design Issues , Ian Whitlockhttp://www2.sas.com/proceedings/sugi29/244-29.pdf
Macro Bugs – How To Create, Avoid, And Destroy Them ,Ian Whitlock
http://www2.sas.com/proceedings/sugi30/252-30.pdf
8/3/2019 Scott Bass Sas Macros
http://slidepdf.com/reader/full/scott-bass-sas-macros 36/38
References (cont)… Intermediate and Advanced SAS® Macros ,
Steven First, Katie Ronk
http://www2.sas.com/proceedings/sugi31/107-31.pdf
Demystifying the SAS® Macro Facility ,Harry Droogendyk, Marje Fecht (excellent!)
http://www2.sas.com/proceedings/sugi31/251-31.pdf
Advanced Macro Topics , Steven Firsthttp://www2.sas.com/proceedings/sugi27/p020-27.pdf
Macro Architecture in Pictures , Mark Tabladillo Ph.D.(Object Oriented analysis of SAS Macro – very cerebral but
interesting design ideas)http://www2.sas.com/proceedings/sugi30/168-30.pdf
SAS ® 9.1 Macro Language: Reference (esp. Ch 2)http://support.sas.com/onlinedoc/913/getDoc/en/mcrolref.hlp/a
002293969.htm
8/3/2019 Scott Bass Sas Macros
http://slidepdf.com/reader/full/scott-bass-sas-macros 37/38
References (cont)…Richard Devenezia Website
Excellent source of macros and sample SAS code
http://www.devenezia.com/downloads/sas/macros/index.php
Roland’s SAS Macros
Numerous macros specialized for clinical reportinghttp://www.datasavantconsulting.com/roland/
8/3/2019 Scott Bass Sas Macros
http://slidepdf.com/reader/full/scott-bass-sas-macros 38/38
Questions and Contact Details
Scott Bass
Senior ITG Associate
Macquarie Group Services Australia
Email: http://www.linkedin.com/in/scottbass