Download presentation
Presentation is loading. Please wait.
Published byMarcia Cain Modified over 9 years ago
1
SAS Macros ® 101 How I learned to stop worrying and love macros Alex Chaplin BCS USA Section
2
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.
3
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 mlogicMacro logic Turning options off options mcompilenote=none nosymbolgen nomprint nomlogic;
4
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;
5
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
6
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 */
7
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;
8
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.
9
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 */
10
Example 3 partial log Options mcompilenote=noautocall; NOTE: The macro COURSE_INFO completed compilation without errors. 11 instructions 460 bytes.
11
Example 3 – Calling our macro %course_info(2002,date9.) /* No semi-colon */ %course_info(2001,mmddyy10.)
12
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
13
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
14
Further reading 249-2012: A Tutorial on the SAS® Macro Language John J. Cohen 249-2012: A Tutorial on the SAS® Macro Language SUGI 28: Nine Steps to Get Started Using SAS(r) Macros Jane Stroupe SUGI 28: Nine Steps to Get Started Using SAS(r) Macros SAS(R) 9.3 Macro Language: Reference
15
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.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.