*Slide 9; libname perm '/home/grego1/STAT 541'; run; /* Save ecoli as a permanent SAS data set in perm before proceeding */ /* Code is commented out so we don't over-ride permanent data set proc import out=perm.ecoli datafile="/home/grego1/STAT 541/EColi.xlsx" dbms=XLSX replace; run; */ *Create some artificial 2010 dates; data ecoli2010; set perm.ecoli; if collection_date>'01JUL2009'd then collection_date=collection_date+365; run; /* Code for year */ %let year=2010; title "E Coli Data for &year"; data perm.ecoli&year; set ecoli&year; if year(collection_date)=&year; run; proc print data=perm.ecoli&year (obs=10); run; /* Incorrect code for year */ %let year=2010; title 'E Coli Data for &year'; data perm.ecoli&year; set ecoli&year; if year(collection_date)=&year; run; proc print data=perm.ecoli&year (obs=10); 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 during session started at &systime &sysday, &sysdate9"; footnote2 "on &sysscp system using Release &sysver"; footnote3 "by User &sysuserid"; run; proc tabulate data=boats format=dollar9.2; class locomotion type; var price; table type, mean=type*price; run; *This works to fix possible printing/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; *It might be fun to experiment with %let monthq="JAN" as well; title "E Coli Data for &month 2009"; data perm.ecoli&month; set perm.ecoli; cdate=put(collection_date,date9.); cmonth=substr(cdate,3,3); if cmonth="&month"; run; proc print data=perm.ecoli&month (drop=cdate cmonth obs=10); run; * Alternative approach with poor title and data set name labels; %let month=1; title "E Coli Data for &month 2009"; data perm.ecoli&month; set perm.ecoli; month2=month(collection_date); *&month is coerced from character string '1' to numeric value 1; * if month2=&month also works; if month2="&month"; run; proc print data=perm.ecoli&month (obs=10); run; *Slide 21; %let month=OCT; options symbolgen; title "E Coli Data for &month&year"; data ecoli&month&year; set ecoli2010; cdate=put(collection_date,date9.); cmonth=substr(cdate,3,3); if cmonth="&month" and year(collection_date)=&year; run; proc print data=ecoli&month&year (obs=10); run; %put Year is &year and Month is &month for ecoli&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; *Don't run this!; %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; %let libperm=%bquote(/home/grego1/STAT 541); libname perm "&libperm"; run; *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; /* 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); %let indvar=%substr(&indvars,&indvarpos,&ivscanl); title "Regression of Reading score on &indvar"; proc reg data=hsb2; model read=&indvar; run; *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--with quite a proliferation of info in the LOG; %put I was born %left(%qsysfunc(putn(&bday,worddate31.))); %put I was born %cmpres(%qsysfunc(putn(&bday,worddate31.))); *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; *Make sure ecoli2010 is in the permanent SAS data set; %let lib=work; %let year=2010; %let month=AUG; %put Year is &year and Month is &month for &lib.ecoli&month&year; *Fix lib; %put Year is &year and Month is &month for &lib..ecoli&month&year; %let libperm=%bquote(/home/grego1/STAT 541); libname perm "&libperm"; run; data &lib..ecoli&month&year; set perm.ecoli&year; cdate=put(collection_date,date9.); cmonth=substr(cdate,3,3); if cmonth="&month" and year(collection_date)=&year; run; title "E Coli Data for &month&year"; proc print; run; proc sgplot data=&lib..ecoli&month&year; histogram EColi; run;