标题: A macro design pattern by PROC FCMP [打印本页] 作者: shiyiming 时间: 2011-8-25 14:00 标题: A macro design pattern by PROC FCMP From Dapangmao's blog on sas-analysis
<div class="separator" style="clear: both; text-align: center;"><a href="http://2.bp.blogspot.com/-UMLvE1BqFfU/TlXXn13L_DI/AAAAAAAAAvQ/UIzexhKqEsE/s1600/adapter.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="301" src="http://2.bp.blogspot.com/-UMLvE1BqFfU/TlXXn13L_DI/AAAAAAAAAvQ/UIzexhKqEsE/s400/adapter.png" width="400" /></a></div><br />
We all heard horrible stories that someone tried to piece together a bunch of nice functioning macros for a big macro, and ended up with a failed and undebuggable system. Part of reasons can be about encapsulation: not all SAS programmers have the good habit to localize the macro variables by %local statement; the leaking macro variables may ruin the attempt to utilize multiple macros written by different SAS programmers. To solve this problem, Mark Tabladillo brought the concepts of <a href="http://analytics.ncsu.edu/sesug/2004/TU11-Tabladillo.pdf">encapsulation, polymorphism, and inheritance</a> into nested macros. And he raised several <a href="http://www2.sas.com/proceedings/sugi31/173-31.pdf">design patterns</a> for macros to emulate the object-oriented languages.<br />
<br />
The FCMP procedure, besides its original purpose as a function compiler, could encapsulate macros by <a href="http://support.sas.com/resources/papers/proceedings10/326-2010.pdf">its RUN_MACRO function</a>. The macro-based functions seem like more safe modules than the macros themselves. Erin, Daniel and Himesh in <a href="http://support.sas.com/resources/papers/proceedings11/291-2011.pdf">their SAS Global 2011 paper </a>showed an example to build a complicated reporting system for academic performances. Their principle is to construct a few macro-embedded functions by PROC FCMP and then incorporate them with an interface macro. Here I modified their codes a little to increase the number of macros and showed the relationship among the elements in the UML diagram above. The stucture is similar to the adapter pattern, one of the many OOP design patterns, with PROC FCMP as a wrapper.<br />
<br />
Overall, functionalizing our macros or our colleagues’ macros by PROC FCMP is an alternative way to integrate them for a ‘big’ purpose.<br />
<br />
<pre style="background-color: #ebebeb; border: 1px dashed rgb(153, 153, 153); color: #000001; font-size: 14px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"><code>
/*******************READ ME*********************************************
* THE CODES BELOW ARE COPIED AND MODIFIED FROM ERIN LYNCH, DANIEL
* O’CONNOR, HIMESH PATEL OF SAS INSTITUTE
*
* THE ORIGINAL CODE AND RAW DATA CAN BE FOUND FROM THEIR PAPER
* MY REPORTING REQUIRES A FULL STAFF—HELP!
* PAPER 291, SAS GLOBAL FORUM 2011
* support.sas.com/resources/papers/proceedings11/291-2011.pdf
*
****************END OF READ ME******************************************/
%macro linedata_prep;
%let linedata=%sysfunc(dequote(&linedata.));
ods _all_ close;
data linedata;
set &linedata;
district=substr(district,1,8)||' '||substr(district,9,1);
run;
proc sort data= linedata out=sorted_linedata;
by district year;
run;
proc sort data= linedata out=districts(keep=district) nodupkey;
by district;
run;
%mend;
proc template;
define style Styles.Charter;
parent = styles.printer;
style Body from Document
"Undef margins so we get the margins from the printer or SYS option" /
marginbottom = _undef_
margintop = _undef_
marginright = _undef_
marginleft = _undef_
pagebreakhtml = html('PageBreakLine')
backgroundimage="Your.png";
end;
run;