scott bass sas macros

38
Macro: So What’s Really Going On? Scott Bass Senior ITG Associate Macquarie Group Services Australia

Upload: janardhan-reddy

Post on 07-Apr-2018

229 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Scott Bass Sas Macros

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

Page 2: Scott Bass Sas Macros

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

Page 3: Scott Bass Sas Macros

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.

Page 4: Scott Bass Sas Macros

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.

Page 5: Scott Bass Sas Macros

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?

Page 6: Scott Bass Sas Macros

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.

Page 7: Scott Bass Sas Macros

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

Page 8: Scott Bass Sas Macros

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

Page 9: Scott Bass Sas Macros

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

Page 10: Scott Bass Sas Macros

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

Page 11: Scott Bass Sas Macros

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

Page 12: Scott Bass Sas Macros

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

Page 13: Scott Bass Sas Macros

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

Page 14: Scott Bass Sas Macros

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

Page 15: Scott Bass Sas Macros

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

Page 16: Scott Bass Sas Macros

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

Page 17: Scott Bass Sas Macros

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

Page 18: Scott Bass Sas Macros

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

Page 19: Scott Bass Sas Macros

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.

Page 20: Scott Bass Sas Macros

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;

Page 21: Scott Bass Sas Macros

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

Page 22: Scott Bass Sas Macros

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

Page 23: Scott Bass Sas Macros

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

Page 24: Scott Bass Sas Macros

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

Page 25: Scott Bass Sas Macros

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)

Page 26: Scott Bass Sas Macros

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

Page 27: Scott Bass Sas Macros

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.

Page 28: Scott Bass Sas Macros

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;

Page 29: Scott Bass Sas Macros

8/3/2019 Scott Bass Sas Macros

http://slidepdf.com/reader/full/scott-bass-sas-macros 29/38

Sample Program Results

Page 30: Scott Bass Sas Macros

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

Page 31: Scott Bass Sas Macros

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

Page 32: Scott Bass Sas Macros

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;

Page 33: Scott Bass Sas Macros

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.

Page 34: Scott Bass Sas Macros

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.

Page 35: Scott Bass Sas Macros

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

Page 36: Scott Bass Sas Macros

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

Page 37: Scott Bass Sas Macros

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/ 

Page 38: Scott Bass Sas Macros

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