How to Parameterize Your SAS Program

advertisement
How To Parameterize Your SAS Program
How to Parameterize Your SAS Program
By
Charles Patridge
PDPC, Ltd.
172 Monce Road
Burlington, CT 06013 USA
Phone: 860-673-9278
Email: Charles_S_Patridge@prodigy.net
Web: http://www.sconsig.com
Presented to Boston Area SAS User Group
June 1, 2005
Intended Audience
Beginning to Novice SAS Programmers
1
How To Parameterize Your SAS Program
times, beginning SAS programmers develop their programs to perform a task
Many
assigned to them without much of any consideration as to how their creation will
change in the future due to the ever changing needs of their management and/or
environment. As such, many of these beginning SAS programmers will "cut and paste"
their prior creations into "new" creations to formulate another solution based upon what
they have already created to answer a new but similar business problem.
we end up with a series of similar, yet slightly different SAS programs
Afterwards,
which become harder to maintain as we move forward into creating more "similar"
programs. What happens when the very basic component of our original program has to
change due to data changes or another application is created with similar needs?
our existing programs to another directory, make a series of changes to all
Clone
our program code, setup new filename(s) or libname(s), and then "Voila", we
of
are ready to go!
or spending a little more time on the very first SAS program, and thinking
Reflection
about what could change on the next request, how could I use the same program to
answer a new question without changing the original code?
this presentation "How to Parameterize Your SAS Program" will walk
Officiously,
you through a simple SAS program, make a few changes to answer new
problem(s), evolve the same program so that it can answer a variety of questions. And
finally, ending up with a program that can be used over and over again by many
applications resulting in a "Plug-N-Play" methodology of developing future applications.
back, clear your thoughts, take a deep breath, and open your mind to doing things
Sitdifferently!
I hope you can see the potential of parameterizing your SAS programs.
2
How To Parameterize Your SAS Program
options ls=175 center date number pageno=1;
title1 "How To Parameterize Your SAS Program";
title2 "Sample of Data used for Presentation";
title3 "Guest Book Visitors to WWW.SCONSIG.COM";
proc print
data=cfdata.guestbk uniform label split="/";
var date name email url source;
format date date9.;
run;
See Figure 1 for output
3
How To Parameterize Your SAS Program
Figure 1
4
How To Parameterize Your SAS Program
Figure 1
continued
5
How To Parameterize Your SAS Program
options ls = 76;
proc format;
value wkdayfmt
1="SUN" 2="MON" 3="TUE" 4="WED"
5="THU" 6="FRI" 7="SAT";
run;
data guestbk;
set cfdata.guestbk;
format date date9. ;
weekday = weekday( date);
run;
proc freq data=guestbk;
tables weekday / missing;
format weekday wkdayfmt. ;
run;
See Figure 2 for output
6
How To Parameterize Your SAS Program
Figure 2
How To Parameterize Your SAS Program
Sample of Data used for Presentation
Guest Book Visitors to WWW.SCONSIG.COM
5
Cumulative Cumulative
WEEKDAY
Frequency
Percent
Frequency
Percent
----------------------------------------------------SUN
11
6.0
11
6.0
MON
32
17.5
43
23.5
TUE
41
22.4
84
45.9
WED
29
15.8
113
61.7
THU
30
16.4
143
78.1
FRI
27
14.8
170
92.9
SAT
13
7.1
183
100.0
7
How To Parameterize Your SAS Program
Title4 "Using an WHERE statement to Subset";
data guestbk1;
set cfdata.guestbk;
where "01JAN1998"D le date le "31DEC1999"D;
weekday = weekday( date);
format date date9.;
run;
proc freq data=guestbk1;
tables weekday / missing;
format weekday wkdayfmt. ;
run;
See Figure 3 for output
8
How To Parameterize Your SAS Program
Figure 3
How To Parameterize Your SAS Program
Sample of Data used for Presentation
Guest Book Visitors to WWW.SCONSIG.COM
Using an WHERE statement to Subset
6
Cumulative Cumulative
WEEKDAY
Frequency
Percent
Frequency
Percent
----------------------------------------------------SUN
7
6.9
7
6.9
MON
20
19.6
27
26.5
TUE
21
20.6
48
47.1
WED
18
17.6
66
64.7
THU
20
19.6
86
84.3
FRI
11
10.8
97
95.1
SAT
5
4.9
102
100.0
9
How To Parameterize Your SAS Program
Title4 "Using LET to set Macro Variables";
%let BDATE = 01JAN1998 ;
%let EDATE = 31DEC1999 ;
data guestbk1;
set cfdata.guestbk;
where "&BDATE"D
le date le "&EDATE"D
weekday = weekday( date);
format date date9.;
run;
proc freq data=guestbk1;
tables weekday / missing;
format weekday wkdayfmt. ;
run;
See Figure 4 for output
10
;
How To Parameterize Your SAS Program
Figure 4
How To Parameterize Your SAS Program
Sample of Data used for Presentation
Guest Book Visitors to WWW.SCONSIG.COM
Using LET to set Macro Variables
7
Cumulative Cumulative
WEEKDAY
Frequency
Percent
Frequency
Percent
----------------------------------------------------SUN
7
6.9
7
6.9
MON
20
19.6
27
26.5
TUE
21
20.6
48
47.1
WED
18
17.6
66
64.7
THU
20
19.6
86
84.3
FRI
11
10.8
97
95.1
SAT
5
4.9
102
100.0
11
How To Parameterize Your SAS Program
Title4 "Using MACRO to set Macro Variables";
%macro seldate1 ( BDATE, EDATE );
data guestbk1;
set cfdata.guestbk;
where "&BDATE"D
le date le "&EDATE"D
weekday = weekday( date);
format date date9.;
run;
proc freq data=guestbk1;
tables weekday / missing;
format weekday wkdayfmt. ;
run;
%mend seldate1;
%seldate1 ( 01JAN1998, 31DEC1999 );
See Figure 5 for output
12
;
How To Parameterize Your SAS Program
Figure 5
How To Parameterize Your SAS Program
Sample of Data used for Presentation
Guest Book Visitors to WWW.SCONSIG.COM
Using MACRO to set Macro Variables
8
Cumulative Cumulative
WEEKDAY
Frequency
Percent
Frequency
Percent
----------------------------------------------------SUN
7
6.9
7
6.9
MON
20
19.6
27
26.5
TUE
21
20.6
48
47.1
WED
18
17.6
66
64.7
THU
20
19.6
86
84.3
FRI
11
10.8
97
95.1
SAT
5
4.9
102
100.0
13
How To Parameterize Your SAS Program
Title4 "Enhancing Macro";
%macro seldate2 ( BDATE, EDATE, SELECT);
data guestbk1;
set cfdata.guestbk;
where "&BDATE"D
le date le "&EDATE"D
weekday = weekday( date);
&SELECT;
format date date9.;
run;
proc freq data=guestbk1;
tables weekday / missing;
format weekday wkdayfmt. ;
run;
%mend seldate2;
/*** Week Day Signatures ***/
%seldate2 ( 01JAN1998, 31DEC1999,
IF 2 LE WEEKDAY LE 6 );
/*** Week End Signatures ***/
%seldate2 ( 01JAN1998, 31DEC1999,
IF NOT ( 2 LE WEEKDAY LE 6) );
See Figure 6 for output
14
;
How To Parameterize Your SAS Program
Figure 6
How To Parameterize Your SAS Program
Sample of Data used for Presentation
Guest Book Visitors to WWW.SCONSIG.COM
Enhancing Macro
9
Cumulative Cumulative
WEEKDAY
Frequency
Percent
Frequency
Percent
----------------------------------------------------MON
20
22.2
20
22.2
TUE
21
23.3
41
45.6
WED
18
20.0
59
65.6
THU
20
22.2
79
87.8
FRI
11
12.2
90
100.0
How To Parameterize Your SAS Program
Sample of Data used for Presentation
Guest Book Visitors to WWW.SCONSIG.COM
Enhancing Macro
10
Cumulative Cumulative
WEEKDAY
Frequency
Percent
Frequency
Percent
----------------------------------------------------SUN
7
58.3
7
58.3
SAT
5
41.7
12
100.0
15
How To Parameterize Your SAS Program
%macro seldate3 ( BDATE, EDATE, SELECT);
Title5 "Start Date = &BDATE /
End Date = &EDATE";
Title6 "Selection = &SELECT";
data guestbk1;
set cfdata.guestbk;
where "&BDATE"D
le date le "&EDATE"D
weekday = weekday( date);
&SELECT;
format date date9.;
run;
proc freq data=guestbk1;
tables weekday / missing;
format weekday wkdayfmt. ;
run;
%mend seldate3;
/*** Week Day Signatures ***/
%seldate3 ( 01JAN1998, 31DEC1999,
IF 2 LE WEEKDAY LE 6 );
/*** Week End Signatures ***/
%seldate3 ( 01JAN1998, 31DEC1999,
IF NOT ( 2 LE WEEKDAY LE 6) );
See Figure 7 for output
16
;
How To Parameterize Your SAS Program
Figure 7
How To Parameterize Your SAS Program
Sample of Data used for Presentation
Guest Book Visitors to WWW.SCONSIG.COM
Enhancing Macro
Start Date = 01JAN1998 / End Date = 31DEC1999
Selection = IF 2 LE WEEKDAY LE 6
11
Cumulative Cumulative
WEEKDAY
Frequency
Percent
Frequency
Percent
----------------------------------------------------MON
20
22.2
20
22.2
TUE
21
23.3
41
45.6
WED
18
20.0
59
65.6
THU
20
22.2
79
87.8
FRI
11
12.2
90
100.0
How To Parameterize Your SAS Program
Sample of Data used for Presentation
Guest Book Visitors to WWW.SCONSIG.COM
Enhancing Macro
Start Date = 01JAN1998 / End Date = 31DEC1999
Selection = IF NOT ( 2 LE WEEKDAY LE 6)
12
Cumulative Cumulative
WEEKDAY
Frequency
Percent
Frequency
Percent
----------------------------------------------------SUN
7
58.3
7
58.3
SAT
5
41.7
12
100.0
17
How To Parameterize Your SAS Program
%macro seldate4 ( BDATE, EDATE, SELECT, SORTBY );
option ls=155;
Title5 "Start Date = &BDATE /
End Date = &EDATE";
Title6 "Selection = &SELECT";
Title7 "Sorted By = &SORTBY ";
data guestbk1;
set cfdata.guestbk;
where "&BDATE"D
le date le "&EDATE"D
;
weekday = weekday( date);
&SELECT;
format date date9.;
run;
proc sort data=guestbk1 out=guestbk1;
by &SORTBY;
run;
proc print data=guestbk1 uniform label split="/";
var date name email url source;
format date date9.;
run;
%mend seldate4;
/*** Week End Signatures ***/
%seldate4 ( 01JAN1998, 31DEC1999,
IF NOT ( 2 LE WEEKDAY LE 6),
NAME EMAIL );
See Figure 8 for output
18
How To Parameterize Your SAS Program
Figure 8
19
How To Parameterize Your SAS Program
Title4 "Using Keyword vs Positional Macros" );
%macro seldate5 ( BDATE=01JAN1996,
EDATE=31DEC1999,
SELECT=,
SORTBY=DATE );
option ls=155;
Title5 "Start Date = &BDATE /
End Date = &EDATE";
Title6 "Selection = &SELECT";
Title7 "Sorted By = &SORTBY ";
data guestbk1;
set cfdata.guestbk;
where "&BDATE"D
le date le "&EDATE"D
weekday = weekday( date);
&SELECT;
format date date9.;
run;
;
proc sort data=guestbk1 out=guestbk1;
by &SORTBY;
run;
proc print data=guestbk1 uniform label split="/";
var date name email url source;
format date date9.;
run;
%mend seldate5;
/*** Week End Signatures ***/
%seldate5 ( EDATE=31DEC1999,
BDATE=01JAN1998,
SORTBY=NAME EMAIL,
SELECT=IF NOT ( 2 LE WEEKDAY LE 6)
);
%seldate5 ( SELECT=IF 2 LE WEEKDAY LE 6 ) ;
%seldate5 ( EDATE=31DEC1998, SORTBY=NAME ) ;
See Figure 9 for output
20
How To Parameterize Your SAS Program
Figure 9
21
How To Parameterize Your SAS Program
Figure 9 continued
22
How To Parameterize Your SAS Program
Figure 9 continued
23
How To Parameterize Your SAS Program
Title4 "What Your Macro is Doing???";
options
mprint
mlogic
symbolgen;
%seldate1 ( 01JAN1998, 31DEC1999 );
options nomprint nomlogic nosymbolgen;
1426
Title4 "What Your Macro is Doing???";
1427
MLOGIC(SELDATE1): Beginning execution.
MLOGIC(SELDATE1): Parameter BDATE has value 01JAN1998
MLOGIC(SELDATE1): Parameter EDATE has value 31DEC1999
1428
options
mprint
mlogic
symbolgen;
1429
%seldate1 ( 01JAN1998, 31DEC1999 );
MPRINT(SELDATE1):
DATA GUESTBK1;
MPRINT(SELDATE1):
SET CFDATA.GUESTBK;
SYMBOLGEN: Macro variable BDATE resolves to 01JAN1998
SYMBOLGEN: Macro variable EDATE resolves to 31DEC1999
MPRINT(SELDATE1):
WHERE "01JAN1998"D LE DATE LE
"31DEC1999"D ;
MPRINT(SELDATE1):
WEEKDAY = WEEKDAY( DATE);
MPRINT(SELDATE1):
FORMAT DATE DATE9.;
MPRINT(SELDATE1):
RUN;
NOTE: The data set WORK.GUESTBK1 has 102 observations and 31
variables.
MPRINT(SELDATE1):
MPRINT(SELDATE1):
MPRINT(SELDATE1):
MPRINT(SELDATE1):
PROC FREQ DATA=GUESTBK1;
TABLES WEEKDAY / MISSING;
FORMAT WEEKDAY WKDAYFMT. ;
RUN;
MLOGIC(SELDATE1): Ending execution.
1430
options nomprint nomlogic nosymbolgen;
24
How To Parameterize Your SAS Program
Title4 "Taking The Next Step - Plug-N-Play";
/*** Production Macro Library ***/
filename pmacro 'pmacro' ;
/*** Test
Macro Library ***/
filename tmacro 'tmacro' ;
/*** Personal
Macro Library ***/
filename mmacro 'mmacro' ;
options mautosource source source2;
OPTIONS SASAUTOS=(PMACRO, TMACRO, MMACRO, SASAUTOS);
/*** Week Day Signatures ***/
%seldate2 ( 01JAN1998, 31DEC1999,
IF 2 LE WEEKDAY LE 6 );
/*** Week End Signatures ***/
%seldate2( 01JAN1998, 31DEC1999,
IF NOT (2 LE WEEKDAY LE 6));
%seldate4 ( 01JAN1998, 31DEC1999,
IF NOT ( 2 LE WEEKDAY LE 6),
NAME EMAIL );
%seldate5 ( EDATE=31DEC1999,
BDATE=01JAN1998,
SORTBY=NAME EMAIL,
SELECT=IF NOT ( 2 LE WEEKDAY LE 6)
);
See Figure 10 for output
25
How To Parameterize Your SAS Program
Figure 10
26
How To Parameterize Your SAS Program
Figure 10 continued
27
How To Parameterize Your SAS Program
Some good sources for learning more about macros are:
1.
2.
3.
4.
SAS
SAS
SAS
SAS
Macro
Guide
Macro
Macro
Language, Reference, #55501
to Macro Processing, #56041
Language, Course Notes, #58213
Facility Tips & Techniques, #55097
Always start with simple program and build upon it with simple
additions. Makes it much easier to debug when you need to
troubleshoot a problem.
KISS (keep it simple stupid)
And, when you have time, look at other people's code to analyze what
is happening or how someone else approached an interesting problem.
A MEND
Charles Patridge
172 Monce Road
Burlington, CT 06013
Phone: 860-673-9278 or 860-675-9026
Email: Charles_S_Patridge@prodigy.net
Website: http://www.sconsig.com
Biography of Charles Patridge
Chuck has been using SAS since 1979 in various positions, companies and consulting assignments. He has worked for
Hartford Steam Boiler Inspection and Insurance Co., LIMRA, CIGNA, Aetna, Coldwell Banker Relocation, Bayer Inc.,
SNETCO, State of Connecticut, Automobile Association of British Columbia, United States Postal Service, Australian Federal
Health Insurance Commission, Australian Government, Applied Psychological Techniques, Alan K. Campbell Public Affairs
Institute, Arthur Andersen Consulting, Paine Webber, The Hartford Financial Services and currently, for Deloitte Consulting
performing Predictive Modeling. In addition, he operates his own company (PDPC, Ltd.) specializing in SAS and “fuzzy
merge” applications as well as statistical analysis in a variety of industries. His area of SAS expertise lies in BASE, STAT,
AF, FSP, Macros, and ETS.
He is the original founder of HASUG in 1983 and currently a member of its steering committee, founder and director of SAS
CONSIG (Consultant Special Interest Group), creator and Webmaster for SAS CONSIG web site. He has presented numerous
papers and speaking engagements to HASUG, BASUG, NYSUG, NESUG and SUGI as well as presentations at the Institute
for Graphic Communications.
Since March 1994, Chuck has been informally operatoring a network (now on the Internet) to link companies, agencies,
consultants and SAS professionals with contracts and full time positions. To date, his efforts has directly and indirectly placed
over 180 individuals with assignments throughout the United States and overseas. The Internet address to make his effort
successful is http://www.sconsig.com.
He holds a BS in Mathematics from Central Connecticut State University, 45 hrs of graduate studies in Statistics, completed
parts I & II of the Society of Actuaries, HIAA and LOMA exams.
28
Download