A Stored Macro Pack the beginnings

advertisement
A Stored Macro Pack:
The Beginnings
Jacob D. Gundrum
Department of Medical Research
Gundersen Lutheran Medical Foundation
La Crosse, Wisconsin, USA
Background




Repetitive programming tasks exist.
Repeated tasks can be coded as macro(s).
Macros can be compiled and stored.
Stored/compiled macros can be referenced from a
session.
So

How about creating a directory of commonly used
macros?


No need to reinvent the wheel or produce another one.
Just use the ones you have.
Intent
To provide:
1. some framework to create a macro pack tool kit.
2. some examples to start with.
Method / Organization
Project 1
Create a directory with
stored/compiled macros
%macro a(…)
%macro b(…)
%macro c(…)
…
%macro z(…)
1. Open path to stored macros
2. Make macro calls as needed
Project 2
1. Open path to stored macros
2. Make macro calls as needed
…
Project n
1. Open path to stored macros
2. Make macro calls as needed
Two Main Parts
1. Macro code, where you
 Create source code then compile and store the compiled macro
2. Project / session code, where you
 open the path to the compiled / stored macros
 call / invoke the those macros
Part 1: Macro Code
Project 1
Create a directory with
stored/compiled macros
%macro a(…)
%macro b(…)
%macro c(…)
…
%macro z(…)
1. Open path to stored macros
2. Make macro calls as needed
Project 2
1. Open path to stored macros
2. Make macro calls as needed
Project n
1. Open path to stored macros
2. Make macro calls as needed
Storing and Compiling a Macro
To compile and store a macro, simply execute the 2 code sets below:
1.
Tell SAS where to put the compiled macro (run once per session):
libname <name> ‘<path to where to put the compiled macro>';
options mstored sasmstore=<name>;
2.
For each macro, add the “/ store” command at the end of the macro
definition line (run macro code as many times as you need in a
session):
%macro x(<parameter list>) / store;
<macro code>
%mend x;
Storing and Compiling a Macro (cont.)
Notes:
 The stored macro is compiled.
 i.e. it’s not stored as source code

There appear to be options that I have not investigated thus far.
 check out further documentation
Part 2: Project / Session Code
Project 1
Existing directory with
stored compiled macros
%macro a(…)
%macro b(…)
%macro c(…)
…
%macro z(…)
1.
2.
Open path to stored macros
Make macro calls as needed
Project 2
1.
2.
Open path to stored macros
Make macro calls as needed
Project n
1.
2.
Open path to stored macros
Make macro calls as needed
Using the Stored Macros in a Project
To use any of the macros you have stored and compiled:
1.Tell SAS where the compiled macros are located and that you want to
use them:
libname <name> ‘<path to where the compiled macros exist>';
options mstored sasmstore=<name>;
(execute the above statement once per session)
2. At any point in the session, just call the macros as usual:
• %x(<parameter list>);
Examples for Data Preparation

Example macros:

summarize missing values


identify order errors in an ordered sequence of variables


See the CheckOrder.sas file
convert raw variables to their formatted versions


See the EmptySummary.sas file
See the Format_To_Raw.sas file
Why data prep macros?



time consuming
repetitious
common issue
Reference
 SAS 9.2 Documentation
 Search for: Introduction to Storing and Reusing Macros
The End
Thanks for Watching and Listening
Download