SAS Macros 101 How I learned to stop

  • Slides: 15
Download presentation
SAS ® Macros 101 How I learned to stop worrying and love macros Alex

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

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

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 /*

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

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

Example 2 code proc sql noprint; /* Assign observations into &rows sql host variables */ select course_code, location, begin_date format=mmddyy 10. into : crsid 1 -: crsid&rows, : place 1 -: place&rows, : date 1 -: 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; /*

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 PLACE 2 resolves to Boston Here is the result of %put &&&city&n;

Forward re-scan rule • Macro processor scans and rescans from left to right to

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,

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 : crsid 1 -: crsid&rows, : place 1 -: place&rows, : date 1 -: date&rows from sasuser. schedule where year(begin_date)=&ccyy. order by begin_date; quit; %put &date 2; %mend course_info; /*Macro end */

Example 3 partial log Options mcompilenote=noautocall; NOTE: The macro COURSE_INFO completed compilation without errors.

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, date 9. ) %course_info(2001, mmddyy 10. )

Example 3 – Calling our macro %course_info(2002, date 9. ) %course_info(2001, mmddyy 10. ) /* No semi-colon */

Example 3 – Partial log output 1 %course_info(2002, date 9. ) MLOGIC(COURSE_INFO): Parameter CCYY

Example 3 – Partial log output 1 %course_info(2002, date 9. ) MLOGIC(COURSE_INFO): Parameter CCYY has value 2002 MLOGIC(COURSE_INFO): Parameter DTFMT has value date 9. SYMBOLGEN: Macro variable DTFMT resolves to date 9. SYMBOLGEN: Macro variable CCYY resolves to 2002 MPRINT(COURSE_INFO): select course_code, location, begin_date format=date 9. into : crsid 1 -: crsid 12, : place 1 -: place 12, : date 1 -: date 12 from sasuser. schedule where year(begin_date)=2002 order by begin_date; MLOGIC(COURSE_INFO): %PUT &date 2 SYMBOLGEN: Macro variable DATE 2 resolves to 21 JAN 2002 date 9 format

Example 3 – Partial log output 2 %course_info(2001, mmddyy 10. ) MLOGIC(COURSE_INFO): Parameter CCYY

Example 3 – Partial log output 2 %course_info(2001, mmddyy 10. ) MLOGIC(COURSE_INFO): Parameter CCYY has value 2001 MLOGIC(COURSE_INFO): Parameter DTFMT has value mmddyy 10. SYMBOLGEN: Macro variable DTFMT resolves to mmddyy 10. SYMBOLGEN: Macro variable CCYY resolves to 2001 MPRINT(COURSE_INFO): select course_code, location, begin_date format=mmddyy 10. into : crsid 1 -: crsid 12, : place 1 -: place 12, : date 1 -: date 12 from sasuser. schedule where year(begin_date)=2001 order by begin_date; MLOGIC(COURSE_INFO): %PUT &date 2 SYMBOLGEN: Macro variable DATE 2 resolves to 01/22/2001 mmddyy 10 format

Further reading • 249 -2012: A Tutorial on the SAS® Macro Language John J.

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

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 product names are registered trademarks or trademarks of their respective companies.