1 / 15

SAS Macros ® 101

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

teleri
Download Presentation

SAS Macros ® 101

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  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=noautocallsymbolgenmprintmlogic; • mcompilenote Macro compilation message • symbolgen Values assigned to macro variables • mprint Macro code • mlogic Macro logic Turning options off options mcompilenote=none nosymbolgennomprintnomlogic;

  4. Example 1 code options symbolgen; proc sqlnoprint; 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 sqlnoprint; /* 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 symbolgenmcompilenote=noautocallmprintmlogic; %macro course_info(ccyy,dtfmt); /* Macro start. Takes year and date format */ proc sqlnoprint; 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® MacroLanguageJohn J. Cohen • SUGI 28: Nine Steps to Get Started Using SAS(r) MacrosJane Stroupe • 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.

More Related