The qualities which SAS® macros share with object-oriented languages account for the power of macro
programming. This paper illustrates some examples of specific design patterns which can be partially or fully
implemented with the SAS macro language. The material is intermediate to advanced, and assumes knowledge of
macros and macro variables. The goal is to illustrate best practices for SAS macro programming.
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Sas® Macro Design Patterns
1. SAS® Macro Design
g
Patterns
Mark Tabladillo Ph D
Ph.D.
Software Developer, MarkTab Consulting
Associate Faculty, University of Phoenix
Atlanta, GA
,
2. Introduction
Experienced SAS programmers use
macros regularly
The Macro Facility has two components
The macro processor (engine or mechanism)
The macro language (syntax)
3. Power of SAS Macros
Power Source Definition Examples
Function The SAS System has added specific • %LENGTH
macro functions. • %EVAL
• %SYSFUNC
See SAS Institute (2005)
Implementation The SAS System defines specific ways • Global versus local macro
that macro variables and macros variables
resolve. • %DO inside macros
See SAS Institute (2005)
Structure The SAS Macro language has some To some extent:
characteristics of object-oriented
object- • Encapsulation
languages. • Polymorphism
• Inheritance
See Tabladillo (2005)
4. SAS Macro Design Patterns
Focus:
Focus: structural power
Approach:
A h
Approach: provide examples on how
id l h
macros can implement some aspects of
object- i
object-oriented d i patterns
bj d design
Mission:
Mission: to provide specific design ideas
for structuring macros in the most
powerful way possible
5. Outline
First:
First: Illustrate three specific ways to trick the
SAS macro language to emulate some
properties of object-oriented languages.
object-
Second:
Second: Answer what a design pattern is and why
design patterns are useful.
Third: List seven specific design patterns and
provide SAS macro language examples.
Finally: Provide some general application
advice.
6. Overview
How to trick the SAS Macro
Language
Why design patterns are useful
Six specific design patterns
General application development advice
pp p
7. How to Trick the SAS Macro
Language
Trick One – emulate inheritance by
cutting and pasting text
There is no %INHERIT
8. How to Trick the SAS Macro
Language
Trick Two – emulate all types of variables
with macro variables
Macro variables are inherently character
9. How to Trick the SAS Macro
Language
%macro interface(char, num, array, macro);
( , , y, );
%local major;
%macro breakarray(m, n, p, q);
data _null_;
call symput('major',&m);
run;
%put &m &n &p &q;
%mend breakarray;
%macro submacro(a, b);
%put &a &b;
%mend submacro;
%put &char &num &array ¯o;
* Break up the array;
%breakarray&array.;
%put &major;
* Call the named macro;
%¯o.;
%mend interface;
%interface (char=character, num=2005, array=(1,2,3,4),
macro=submacro(a=5,b=c));
10. How to Trick the SAS Macro
Language
Trick Three – emulate public functions
(or methods) by applying nested macros
Macros do not expose internal functions
However, developers can write nested
macros
11. Overview
How to trick the SAS Macro Language
Wh d i patterns are useful
Why design f l
Six specific design patterns
General application development advice
12. Design Pattern Fundamentals
Pattern – a solution to a problem in
context
Patterns provide a way to solve
commonly recurring problems with two
l i bl ih
goals:
Reuse Solutions
Establish Common Terminology
13. Overview
How to trick the SAS Macro Language
Wh d i patterns are useful
Why design f l
Six specific design patterns
General application development advice
14. FACADE: SIMPLIFY A
COMPLEX SYSTEM
Façade Pattern Key Features
Intent You want to simplify how to use an existing system. You need to
define your own interface
interface.
Problem You need to use only a subset of a complex system. Or you need to
interact with the system in a particular way.
Solution The Facade presents a new interface for the client of the existing
system to use.
Consequences The Façade simplifies the use of the required subsystem. However,
since the Façade is not complete, certain functionality may be
unavailable to the client.
il bl h li
15. FACADE: SIMPLIFY A
COMPLEX SYSTEM
FULLY IMPLEMENTED EXAMPLE
%macro facade(dataset);
proc freq data=&dataset.;
tables year*sales/list missing;
run;
proc means data=&dataset.;
var annualSales totalEmployees sumTurnover;
run;
%mend facade;
%facade(dataset=sales.ca200503);
%facade(dataset=sales.ca200510);
%facade(dataset=sales.ca200511);
%facade(dataset=sales ca200511);
16. STRATEGY: USE DIFFERENT BUSINESS
RULES DEPENDING ON CONTEXT
Strategy Pattern Key Features
Intent Allows you to use different business rules or algorithms depending on the
context in which they occur.
Problem The selection of an algorithm that needs to be applied depends upon the
client making the request or the data being acted upon. If you simply
have a rule in place that does not change, you do not need a Strategy
pattern.
Solution Separates the selection of the algorithm from the implementation of the
algorithm. Allows for the selection to be made based upon context.
Consequences • The Strategy pattern defines a family of algorithms.
• Switches and/or conditionals can be eliminated.
• You must invoke all algorithms in the same way (they must all have the
same interface).
interface)
17. STRATEGY: USE DIFFERENT BUSINESS
RULES DEPENDING ON CONTEXT
FULLY IMPLEMENTED EXAMPLE
data work.taxExample;
length grossAmount totalTax 8;
grossAmount = 34000;
run;
%macro calculateTaxes(dataset,county);
%macro taxStrategy(county);
% ( )
%macro DeKalb;
totalTax = grossAmount * 0.06;
%mend deKalb;
%macro Fulton;
totalTax = grossAmount * 0 075;
0.075;
%mend Fulton;
%macro Gwinnett;
totalTax = grossAmount * 0.06;
%mend Gwinnett;
%if &county. = DeKalb or &county. = Fulton or &county. = Gwinnett %then %&county.;
%mend taxStrategy;
data &dataset.;
set &dataset.;
%taxStrategy(&county.);
run;
proc print d t
i t data=&dataset.;
d t t
run;
%mend calculateTaxes;
%calculateTaxes(dataset=work.taxExample, county=Fulton);
18. SINGLETON: HAVE ONLY
ONE OBJECT AVAILABLE
Decorator Pattern Key Features
Intent You want to have only one of an object but there is no global object
that controls the instantiation of the object.
Problem Several different client objects need to refer to the same thing and
you want to ensure that you do not have more than one of them.
Solution Guarantees one instance.
Consequences Clients need not concern themselves whether an instance of the
singleton object exists.
g j
19. SINGLETON: HAVE ONLY ONE
OBJECT AVAIABLE
Fully Implemented Singleton Pattern:
Only one SAS Macro is allowed
Only one SAS local named variable
Only one SAS global named variable
Only one instance of a base SAS session
y
20. DECORATOR: ATTACH ADDITIONAL
RESPONSIBILITIES DYNAMICALLY
Decorator Pattern Key Features
Intent Attach additional responsibilities to an object dynamically.
Problem The object that you want to use does the basic functions you require.
However, you may need to add some additional functionality to
the object occurring before or after the object’s base
functionality.
y
Solution Allows for extending the functionality of an object without resorting
to subclassing.
Consequences Functionality that is to be added resides in small objects. The
advantage is the ability to dynamically add this function before or
after the core functionality.
21. DECORATOR: ATTACH ADDITIONAL
RESPONSIBILITIES DYNAMICALLY
PARTIALLY IMPLEMENTED DECORATOR PATTERN
%macro messyMacro(num,char);
* Messy Macro;
%put MessyMacro &num. &char.;
%mend messyMacro;
%macro decoratorBefore(parameters);
%put Before;
%messyMacro¶meters.;
%mend decoratorBefore;
%
%macro d t B f
decoratorBeforeAndAfter(parameters);
A dAft ( t )
%put Before;
%messyMacro¶meters.;
%put After;
%mend decoratorBeforeAndAfter;
%macro decoratorAfter(parameters);
%messyMacro¶meters.;
%put After;
%mend decoratorAfter;
%decoratorBeforeAndAfter(parameters=(num=1,char=SAS));
22. TEMPLATE METHOD: APPLY DISCIPLINE TO
THE FUNCTIONS AVAILABLE INSIDE MACROS
Template Method Pattern Key Features
Intent Define the skeleton of an algorithm in an operation, deferring
some steps to subclasses. Redefine the steps in an algorithm
without changing the algorithm’s structure
Problem There is a procedure or set of steps to follow that is consistent
at one level of detail, but individual steps may have different
implementations at a lower level of detail.
Solution Allows for definition of substeps that vary while maintaining a
consistent basic process.
Consequences Templates provide a good platform for code reuse. They are
also helpful in ensuring the required steps are implemented.
23. TEMPLATE METHOD: APPLY DISCIPLINE TO
THE FUNCTIONS AVAILABLE INSIDE MACROS
%macro accessSAS(connectParameters,selectRecordParameters); %macro accessSQLServer(connectParameters,selectRecordParameters);
%macro connectDB(num,char); %macro connectDB(num,char);
%put SAS ConnectDB; %put SQL Server ConnectDB;
%mend connectDB; %mend connectDB;
%macro selectRecords(num,char); %macro selectRecords(num,char);
%put SAS Record Selection; %put SQL Server Record Selection;
%mend selectRecords; %mend selectRecords;
%macro doQuery; %macro doQuery;
* doQuery is the same for all macros; * doQuery is the same for all macros;
%connectDB&connectparameters.; %connectDB&connectparameters.;
%selectRecords&selectRecordParameters.; %selectRecords&selectRecordParameters.;
%mend doQuery; %mend doQuery;
%doQuery; %doQuery;
%mend accessSAS; %mend accessSQLServer;
PARTIALLY IMPLEMENTED TEMPLATE METHOD:
Two different macros, same nested macros
%accessSAS(connectParameters=(num=1,char=SAS),
selectRecordParameters=(num=2,char=SAS));
%accessSQLServer(connectParameters=(num=1,char=SQLServer),
selectRecordParameters=(num=2,char=SQLServer));
24. ANALYSIS MATRIX: HIGH-
HIGH-
VOLUME MACRO PROCESSING
Analysis Matrix Method Pattern Key Features
Intent Provide a systematic way to surface varying dimensions of change.
Problem Variations in the system need to be made explicit so that applying
design patterns becomes easier.
Solution Create a matrix which documents the concepts which are varying.
p y g
Matrix creation can help the developer discover the concepts that
are varying, find points of commonality, and uncover missing
requirements.
Consequences The final design is optimized for the surfaced dimensions, and
dimensions
refactoring may be required if dimensions are added or removed.
25. ANALYSIS MATRIX: HIGH-
HIGH-
VOLUME MACRO PROCESSING
U.S. Sales Canadian Sales German Sales
Calculate freight Combination Combination Combination
Verify address Combination Combination Combination
Calculate tax Combination Combination Combination
Money Combination Combination Combination
Dates Combination Combination Combination
Maximum Combination Combination Combination
weight
Analysis Matrix Example (Shalloway and Trott, 2005, p. 286)
26. Overview
How to trick the SAS Macro Language
Why design
Wh d i patterns are useful f l
Six specific design patterns
General application development
advice
27. General Application Advice
The
Th examples ill
l illustrate conceptual design
ld i
patterns
The examples could be used as is, or
combined with one another
Consider upgrading to SAS/AF
Most optionally licensed products represent
procedures which extend base SAS
28. Conclusion
This presentation:
p
Provided tricks for object-oriented emulation
object-
Defined and defended design patterns
Demonstrated fully or partially implemented
design patterns
Provided application advice
Read more (books or web) on design patterns
to deepen your topical understanding
29. References
Fowler, Martin (1999), Refactoring: Improving the Design of Existing
Code,
Code, Reading, MA: Addison Wesley Longman, Inc.
Gamma, E., Helm, R., Johnson, R., Vlissides, J. (1995), Design
Patterns: Elements of Reusable Object-Oriented Software, Reading MA:
Object- Software, Reading,
Addison Wesley Longman, Inc.
SAS Institute Inc. (2005), SAS OnlineDoc® 9.1.3, Cary, NC: SAS
9.1.3,
Institute, Inc.
Shalloway, A., and Trott, J. (2005), Design Patterns Explained: a New
Perspective on Object-Oriented Design (Second Edition), Boston MA:
Object- Edition), Boston,
Addison-
Addison-Wesley, Inc.
Tabladillo, M. (2005), “Macro Architecture in Pictures”, SUGI
(
Proceedings,
Proceedings, 2005.
30. Contact Information
Mark Tabladillo
Software Developer, MarkTab C
S f D l M kT b Consulting
li
http://www.marktab.com