*Slide 9; data citiday; set sashelp.citiday; collection_date=date; run; /* Code for year */ %let year=1991; title "Citibase Data for &year"; data citiday&year; set citiday; if year(collection_date)=&year; run; proc print data=citiday&year (obs=50); run; /* Incorrect code for year */ %let year=1991; title 'Citibase Data for &year'; data citiday&year; set citiday; if year(collection_date)=&year; run; proc print data=citiday&year (obs=50); run; *Slide 12; *Read in the boat data; proc format; value $typ "cat"="catamaran" "sch"="schooner" "yac"="yacht"; data boats; input Port $ 1-7 Locomotion $ 9-13 Type $ 15-17 Price 19-23; format type $typ.; datalines; Maalea sail sch 75.00 Maalea sail yac 32.95 Lahaina sail cat 62.00 Maalea power cat 22.00 Maalea sail sch 47.50 Maalea power cat 28.99 Maalea power yac 19.99 Maalea power cat 29.50 Lahaina power yac 29.95 Maalea sail cat 42.95 ; run; *Automatic variables; title "Yacht Rentals"; title2 "Data from &syslast"; footnote "Created at &systime &sysday, &sysdate9"; footnote2 "on &sysscp system using Release &sysver"; footnote3 "by User &sysuserid"; proc tabulate data=boats format=dollar9.2; class locomotion type; var price; table type, mean=type*price; run; *This works to fix possible centering problem; title "Yacht Rentals"; title2 "Data from %cmpres(&syslast)"; footnote "Created at &systime &sysday, &sysdate9"; footnote2 "on &sysscp system using Release &sysver"; footnote3 "by User &sysuserid"; proc tabulate data=boats format=dollar9.2; class locomotion type; var price; table type, mean=type*price; run; *Slide 15; title2; footnote; /* Code for month */ /* This requires some special handling of char/num forms */ %let month=JAN; title "Citibase Data for &month"; data citiday&month; set citiday; cdate=put(collection_date,date9.); cmonth=substr(cdate,3,3); if cmonth="&month"; run; proc print data=citiday&month (obs=50); run; * Alternative approach with poor title and data set name labels; %let month=1; title "Citibase Data for &month"; data citiday&month; set citiday; month2=month(collection_date); *&month is coerced from character string '1' to numeric value 1; if month2="&month"; run; proc print data=citiday&month (obs=50); run; *Slide 21; %let month=OCT; options symbolgen; title "Citibase Data for &month&year"; data citiday&month&year; set citiday1991; cdate=put(collection_date,date9.); cmonth=substr(cdate,3,3); if cmonth="&month" and year(collection_date)=&year; run; proc print data=citiday&month&year (obs=50); run; %put Year is &year and Month is &month for citiday&month&year; *Slide 23; title; options symbolgen; %let demo=%str(data sail; set boats; if locomotion='sail'; run;); &demo; proc print; run; %let demo=data sail%str(;) set boats%str(;) if locomotion='sail'%str(;) run%str(;); &demo; run; proc print; run; *Slide 24; options symbolgen; %let text=Hawaii%str(%')s Sailboat Rates; title &text; proc print; run; %let text=%str(Today%'s Weather); %put text is interpreted as &text; *Slide 25; title; options symbolgen; %let cite=%nrstr( (Grego, Li, Lynch & Sethuraman, 2012)); %put cite is interpreted as &cite; *Slide 26; %let text=%bquote(Hawaii's Sailboat Rates); title &text; proc print; run; ' /* ignore this single quote */ *Slide 27; /* A demonstration of %scan */ title; data hsb2; input id female race ses prog read write math science socst; datalines; 70 0 4 1 1 57 52 41 47 57 121 1 4 2 3 68 59 53 63 61 86 0 4 3 1 44 33 54 58 31 141 0 4 3 3 63 44 47 53 56 172 0 4 2 2 47 52 57 53 61 113 1 4 2 2 44 52 51 63 61 50 0 3 2 1 50 59 42 53 61 11 0 1 2 2 34 46 45 39 36 84 0 4 2 1 63 57 54 51 63 48 1 3 2 2 57 55 52 50 51 75 1 4 2 3 60 46 51 53 61 60 1 4 2 2 57 65 51 63 61 95 0 4 3 2 73 60 71 61 71 104 0 4 3 2 54 63 57 55 46 38 0 3 1 2 45 57 50 31 56 115 0 4 1 1 42 49 43 50 56 76 0 4 3 2 47 52 51 50 56 195 0 4 2 1 57 57 60 56 52 ; %let indvars=write math female socst; %let indvar=%scan(&indvars,2); run; title "Regression of Reading score on &indvar"; proc reg data=hsb2; model read=&indvar; run; * You can choose multiple independent variables; * by changing the definition of the macro variable; %let indvars=write math female socst; %let indvar= %scan(&indvars,2) %scan(&indvars,4) ; run; title "Regression of Reading score on &indvar"; proc reg data=hsb2; model read=&indvar; run; /* A not terribly practical application showing how %scan, %length, %index and &substr complement each other */ %let indvarscan=%scan(&indvars,4); %let ivscanl=%length(&indvarscan); %let indvarpos=%index(&indvars,socst); *How could this line of code be improved?; %let indvar=%substr(&indvars,&indvarpos,&ivscanl); title "Regression of Reading score on &indvar"; proc reg data=hsb2; model read=&indvar; run; /* %UPCASE versus %QUPCASE */ * %QUPCASE masks special characters (like &), that is, it treats them as ordinary text; %let a = begin; %let b=%nrstr(&a); %put UPCASE produces: %UPCASE(&b); %put QUPCASE produces: %QUPCASE(&b); %put double UPCASE produces: %UPCASE(%UPCASE(&b)); *Slide 28; *Running birthday; %let bday='18Oct1959'd; *Standard output format doesn't work; %put I was born &bday; *The first argument to sysfunc needs to be a SAS function; %put I was born %sysfunc(putn(&bday,worddate31.)); *We need to remove leading blanks; *left() is not interpreted as a function; %put I was born left(%sysfunc(putn(&bday,worddate31.))); *%left reads the comma as an extra argument and fails; %put I was born %left(%sysfunc(putn(&bday,worddate31.))); *These work; %put I was born %left(%qsysfunc(putn(&bday,worddate31.))); %put I was born %cmpres(%qsysfunc(putn(&bday,worddate31.))); * Unlike %sysfunc, here %qsysfunc masks the comma character; *Set up a running age calculation; %put %sysfunc(putn(&bday,best.)); %put %sysfunc(today(),best.); *Computing elapsed days was hard to do on the run, so I split it up; %let today=%sysfunc(today(),best.); %put I am %left(%sysfunc(putn(&today-&bday,best.))) days old; *Slide 29; %let lib=work; %let year=1991; %let month=AUG; %put Year is &year and Month is &month for &lib.citiday&month&year; *Fix lib; %put Year is &year and Month is &month for &lib..citiday&month&year; title "Citi Data for &month&year"; data &lib..citiday&month&year; set citiday&year; cdate=put(collection_date,date9.); cmonth=substr(cdate,3,3); if cmonth="&month" and year(collection_date)=&year; run; proc sgplot data=&lib..citiday&month&year; histogram DSIJPND; run;