PPTX - PhilaSUG

advertisement
SAS
®
Macros
101
How I learned to stop worrying and love macros
Alex Chaplin
BCS USA Section
The Macro Concept
• Reuse the same code in different SAS programs
• Simplify repetitive tasks
• Build flexibility into SAS code
• Hide code
Data preparation for stepping through examples
• In PC SAS. Select Help / Learning SAS Programming and then click OK
on the prompt in order to get the datasets referenced in the examples
into the SASUSER directory.
SAS Macro display options
Turning options on
options mcompilenote=noautocall symbolgen mprint mlogic;
• mcompilenote
Macro compilation message
• symbolgen
Values assigned to macro variables
• mprint
Macro code
• mlogic
Macro logic
Turning options off
options mcompilenote=none nosymbolgen nomprint nomlogic;
Example 1 code
options symbolgen;
proc sql noprint;
select count(*)
into :numrows
/* Assign proc sql host variable :numrows */
from sasuser.schedule
where year(begin_date)=2002;
%let rows=&numrows; /* Assign :numrows to macro variable */
%put There are &rows courses in 2002; /*Print message */
quit;
Example 1 partial log
9693 %let rows=&numrows; /* Assign :numrows to macro variable */
SYMBOLGEN: Macro variable NUMROWS resolves to
4
9694 %put There are &rows courses in 2002; /*Print message */
SYMBOLGEN: Macro variable ROWS resolves to 4
There are 4 courses in 2002  Here is the message
Example 2 code
proc sql noprint; /* Assign observations into &rows sql host variables */
select course_code, location, begin_date format=mmddyy10.
into :crsid1-:crsid&rows, :place1-:place&rows, :date1-:date&rows
from sasuser.schedule
where year(begin_date)=2002
order by begin_date;
quit;
%let city=place;
%let n=2;
%put &&&city&n;
/* Please don't panic */
Example 2 partial log
9759 %let city=place;
9760 %let n=2;
9761 %put &&&city&n; /* Please don't panic */
SYMBOLGEN: && resolves to &. Forward re-scan rule
SYMBOLGEN: Macro variable CITY resolves to place
SYMBOLGEN: Macro variable N resolves to 2
SYMBOLGEN: Macro variable PLACE2 resolves to Boston
Boston  Here is the result of %put &&&city&n;
Forward re-scan rule
• Macro processor scans and rescans from left to right to
resolve two ampersands to one ampersand.
SYMBOLGEN: && resolves to &.
• Can have any number of ampersands but more than 3
is rare.
Example 3 code – Our first macro
options symbolgen mcompilenote=noautocall mprint mlogic;
%macro course_info(ccyy,dtfmt); /* Macro start. Takes year and date format */
proc sql noprint;
select course_code, location, begin_date format=&dtfmt
into :crsid1-:crsid&rows, :place1-:place&rows, :date1-:date&rows
from sasuser.schedule
where year(begin_date)=&ccyy.
order by begin_date;
quit;
%put &date2;
%mend course_info;
/*Macro end */
Example 3 partial log
Options mcompilenote=noautocall;
NOTE: The macro COURSE_INFO completed compilation without errors.
11 instructions 460 bytes.
Example 3 – Calling our macro
%course_info(2002,date9.)
%course_info(2001,mmddyy10.)
/* No semi-colon */
Example 3 – Partial log output 1
%course_info(2002,date9.)
MLOGIC(COURSE_INFO): Parameter CCYY has value 2002
MLOGIC(COURSE_INFO): Parameter DTFMT has value date9.
SYMBOLGEN: Macro variable DTFMT resolves to date9.
SYMBOLGEN: Macro variable CCYY resolves to 2002
MPRINT(COURSE_INFO): select course_code, location, begin_date format=date9.
into
:crsid1-:crsid12, :place1-:place12, :date1-:date12 from sasuser.schedule where
year(begin_date)=2002 order by begin_date;
MLOGIC(COURSE_INFO): %PUT &date2
SYMBOLGEN: Macro variable DATE2 resolves to 21JAN2002  date9 format
Example 3 – Partial log output 2
%course_info(2001,mmddyy10.)
MLOGIC(COURSE_INFO): Parameter CCYY has value 2001
MLOGIC(COURSE_INFO): Parameter DTFMT has value mmddyy10.
SYMBOLGEN: Macro variable DTFMT resolves to mmddyy10.
SYMBOLGEN: Macro variable CCYY resolves to 2001
MPRINT(COURSE_INFO): select course_code, location, begin_date
format=mmddyy10. into
:crsid1-:crsid12, :place1-:place12, :date1-:date12 from sasuser.schedule where
year(begin_date)=2001 order by begin_date;
MLOGIC(COURSE_INFO): %PUT &date2
SYMBOLGEN: Macro variable DATE2 resolves to 01/22/2001  mmddyy10 format
Further reading
• 249-2012: A Tutorial on the SAS® Macro Language John J. Cohen
• SUGI 28: Nine Steps to Get Started Using SAS(r) Macros Jane
Stroupe
• SAS(R) 9.3 Macro Language: Reference
Acknowledgement
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. ® indicates USA registration.
Other brand and product names are registered trademarks or
trademarks of their respective companies.
Download