SAS_Macro_Libraries

CENTRAL SAS MACRO LIBRARIES
Golo von Schwerdtner, Team Lead Senior Analytic Consultant, Thomson Reuters
Ken Schmidt, Senior Analytic Consultant, Thomson Reuters
THOMSON REUTERS Healthcare
October 2011
MACRO BASICS
• The SAS Macro Language is a powerful tool for
extending the capabilities of the SAS system
• SAS Macro main function is a replacement of text
• Macro variables are defined by an ampersand (&)
• Macro functions are defined by a percent sign (%)
• Three types of macro functions:
©2011 Thomson Reuters
– Stand alone: %macro_a
– Parameter: %macro_b(p1, p2)
– Parameter with defaults: %macro_c(p1=,p2=&var1.)
2
MACRO IS A TEXT GENERATOR
• One of its strengths is to create and store reusable
code
• The user supplies all the information needed by the
macro
• There are four ways to store and reuse macro
code:
©2011 Thomson Reuters
– In your program – single instance ~ promotes
promulgation of similar macros
– Through the use of %INCLUDE
– Autocall Libraries or Central Macro Libraries
– Stored Compiled Macro Facility
3
MACRO USES AND ADVANTAGES
• Defining parameters in one place:
– Avoid looking through 1,000s of lines of code to update a
parameter
– Hardcoding – Just Say NO! Hardcoding is when data
specific information is simply typed into programs
– Allows for simpler, better quality code updates or reuse
– Consciously program to avoid hard code
©2011 Thomson Reuters
• Defining universal logic
– When many users running on the same data, centralized
logic can avoid errors and individual differences
4
AUTOCALL LIBRARY
AUTOCALL LIBRARY: WHY?
• Macro libraries allow you to control and manage
the proliferation of your macros
• Macro libraries are extremely useful, and not really
all that complicated
©2011 Thomson Reuters
• Macro libraries are used to avoid the problem of
macro cloning by providing a single location for all
macro definitions
• Once in a library, there will only ONE macro
definition to maintain, and it can be used by as
many programs as is needed
6
AUTOCALL LIBRARY: WHY?
• Better organization
• Larger groups, projects, or organizations with
multiple programmers
©2011 Thomson Reuters
• Autocall library can speed up programming time,
eliminate errors, and fix assumptions
7
AUTOEXEC PROGRAM: HOW?
• Necessary to set up autocall library
• SAS will execute ‘autoexec.sas’ at start of any SAS
session
• Program name is ‘autoexec.sas’
• Location is environment specific
• Default location for PC-SAS 9.2:
©2011 Thomson Reuters
– C:\Program Files\SAS\SASFoundation\9.2\autoexec.sas
• Allows customization
8
AUTOCALL LIBRARY: HOW?
• Setting up the central macro library
1. Determine the macro library location
2. In the autoexec program create a filename referencing
the macro library
3. In the autoexec program set the ‘sasautos’ option to
reference the filename
©2011 Thomson Reuters
•
Example of Code of 2 and 3:
filename my_macro "x:\my_macros";
options sasautos=(my_macro) mautosource;
4. Populate the macro library with macro programs
• Steps 2 and 3 must be run at startup of a SAS
session. Running in ordinary code will not set the
option
9
AUTOCALL LIBRARY: HOW?
• The macro is contained in a program saved as an
ordinary ‘.sas’ file
• The program must have the same name as the
macro call
• The program must be saved in the central macro
library
©2011 Thomson Reuters
• The program may only contain a single macro
• Example of program ‘agecalc.sas’:
%macro agecalc(date=,dob=);
floor(yrdif(&dob,&date,'Act/Act'));
%mend agecalc;
10
AUTOCALL LIBRARY: TIPS
• Useful options when running macros:
– MPRINT - Outputs macro resolved code to log
– MLOGIC – Outputs resolution of macro logic to log.
Example the value of a condition of a %if statement
– SYMBOLGEN – Outputs the resolved value of macro
variables to log
©2011 Thomson Reuters
– MERROR – Outputs a ‘WARNING’ to the log when a
macro reference is not found
– MRECALL – Searches the autocall library every time a
macro is called. Useful for debugging
options mprint mlogic symbolgen merror
mrecall;
11
AUTOCALL LIBRARY: TIPS
• Define a robust vetting process
– Any errors or logic flaws may affect multiple programs or
algorithms
• Define development and QC locations to test
macros before moving to production
• Avoid duplication on multiple autocall libraries
©2011 Thomson Reuters
• When calling macros SAS defaults to ‘work’
• Autocall library as a controlled or limited access
location
12
AUTOCALL LIBRARY: TIPS
• If more than one autocall library is referenced, only
macro will be compiled where sharing the same
macro name
• If a macro with the same call has been compiled in
work, the autocall library version will not be used
filename my_macro "x:\my_macros";
filename my_macr2 "x:\my_macro2";
options sasautos=(my_macro my_macr2) mautosource;
©2011 Thomson Reuters
x:\my_macro\agecalc.sas
%macro agecalc(date=,dob=,age=);
&age.=floor(yrdif(&dob,&date,
'Act/Act'));
%mend agecalc;
x:\my_macro2\agecalc.sas
%macro agecalc(date=,dob=);
floor(yrdif(&dob,&date,'Act/Act'));
%mend agecalc;
13
AUTOCALL LIBRARY: TIPS
• Developing a set of coding standards and
guidelines for the project or team
• Add comments to document purpose and revision
level of the macro
• Good communication
©2011 Thomson Reuters
• Be aware of differences between platforms or
operating environments
14
EXAMPLES
• Creating a central logic for determining final claim
adjudication
– Standardizing assumptions in MSIS
• Creating a central matching logic
– Matching Medicaid provider numbers with NPI
– Matching local beneficiary Ids to SSN
• Creating easy to use tools for data analysis
©2011 Thomson Reuters
– Quick summarization to identify patterns
15
EXAMPLES
©2011 Thomson Reuters
• Create tools for rerouting QC
– Example
/* Macro Purpose: This macro reroutes
default libraries in order to prevent
overwriting permanent files during
the course QC. */
%macro review;
libname staging "x:\qc";
libname prod "x:\qc";
%mend review;
16
USEFUL MACRO TIPS
• Macro variables: global versus local
• Creating macro variables from a dataset
– Remember the maximum length is 65534
©2011 Thomson Reuters
– Using Proc SQL
proc sql noprint;
Keeps log clean
select "'" || cpt_code || "'“ Selects values
into : mylist
Macro variable
separated by ',‘ Separates values by a comma
from my.cptdata; Input dataset
quit;
• Shortcut for eliminating leading and trailing spaces
on macro variables: %let macrovar=&macrovar.
17
CONCLUSION
• Macro autocall libraries allow the macro developer
to store, maintain, and manage large numbers of
macros
• Using autocall macro libraries is not difficult nor is it
overly complicated
• Autocall macro libraries foster an environment that
promotes:
©2011 Thomson Reuters
– good programming standards
– eliminates hard coding
– unnecessary duplication
18
©2011 Thomson Reuters
QUESTIONS?
19
REFERENCES
• http://support.sas.com
• Art Carpenter, Carpenter’s Complete Guide to the
SAS Macro Language, 2nd Edition, SAS Publishing
©2011 Thomson Reuters
• Kirk Lafler, “SAS Macro Programming Tips and
Techniques – Hands-on Workshop, SAS Global
Forum 2009”
20
ACKNOWLEDGMENTS
• SAS and all other SAS Institute Inc. product or
service names are registered trademarks or
trademarks of SAS Institute Inc. in the USA and
other countries
©2011 Thomson Reuters
• The authors express their gratitude to CMS Review
of Provider Medicaid Integrity Contract DHHS
Program for the review of this presentation
21
FOR ADDITIONAL INFORMATION
Please email:
Golo von Schwerdtner
[email protected]
Ken Schmidt
©2011 Thomson Reuters
[email protected]
22