*Slide 9; 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 ; run; %macro printsubset(gender,titletext); proc print; where female=&gender; title "&titletext"; run; %mend printsubset; %printsubset(1,Female Students) %printsubset(0,Male Students) /* Slides 16 through 18 */ /* Macro comment vs regular comment */ options mprint mlogic symbolgen; %macro printsubset(gender,titletext); proc print; %*FEMALE is actually a gender indicator--this statement will not be executed; *This statement will be executed; where female=&gender; title "&titletext"; run; %mend printsubset; %printsubset(1,Female Students) *Slide 23; /* The regsimp macro */ %macro regsimp(dep=,indep=); proc reg data=hsb2; model &dep=&indep; run; %mend; %regsimp(dep=read,indep=write); /* Mixed parameters */ %macro regsimp(dep,indep=); proc reg data=hsb2; model &dep=&indep; run; %mend; /*Simple linear regression */ %regsimp(read,indep=write); /* Regression with no independent variable */ /* This should print the sample mean and */ /* variance and the one-sample t test */ %regsimp(read); *Slide 32; *%IF demo; data todate; input transaction $20. @22 date mmddyy10. debit credit; datalines; Dues 9/30/2007 . 25.00 Dues 9/30/2007 . 25.00 Dues 12/31/2007 . 50.00 Postage 11/1/2007 93.45 . Receipt-Caryn 10/30/2007 261.30 . ; run; data all; input transaction $20. @22 date mmddyy10. debit credit; datalines; Dues 9/30/2007 . 25.00 Dues 9/30/2007 . 25.00 Dues 12/31/2007 . 50.00 Postage 11/1/2007 93.45 . Receipt-Caryn 10/30/2007 261.30 . Dues 3/31/2008 . 25.00 Postage 5/1/2007 92.68 . Dues 6/30/2008 . 25.00 ; run; %macro fiscal(report); %if %upcase(&report)=FY %then %do; title 'Fiscal Year Debits and Credits'; proc means data=all; var debit credit; run; %end; %else %do; title 'To-Date Debits and Credits'; proc means data=todate; var debit credit; run; %end; %mend fiscal; %fiscal(FY) *%IF demo to insert text; %macro dembplot(var,group=); proc sgplot data=hsb2; title "Boxplot of &var"; vbox &var %if &group ne %then %do; /category=&group datalabel=id; %end; %else %do; /datalabel=id; %end; run; %mend; %dembplot(read,group=female); %dembplot(write); *Refinement of above code; %macro dembplot(var,group=); proc sgplot data=hsb2; title "Boxplot of &var"; vbox &var /datalabel=id %if &group ne %then %do; category=&group %end; ; run; %mend; %dembplot(read,group=female); %dembplot(write); *Slide 34; *Simple example of %DO; title; options nomlogic nomprint nosymbolgen; data a; array x x1-x100; do i=1 to 10; do over x; x=rannor(0); end; output; end; run; %macro rename; data b; set a; %do i=1 %to 100; drop x&i; y&i=x&i; %end; run; %mend rename; %rename proc sql outobs=5; select * from a; select * from b; quit; *Slide 35; * Example of %DO and %EVAL; /* The regall macro */ %macro regall(dep=,indep=); %local k indx; %let k=1; %let indx=%scan(&indep,&k); %do %while("&indx" ne ""); proc reg data=hsb2; model &dep=&indx; run; %let k=%eval(&k+1); %let indx=%scan(&indep,&k); %end; %mend; %regall(dep=read,indep=write math female socst); /* An example of filename-pipe to read in all files from a given directory */ filename cohort08 '/home/grego1/STAT 541/Traffic_Count_Data_2012.csv'; *Read in the traffic count data for all 46 counties; data all; infile cohort08 dlm=','; input ctyid reach roadtype roadid aadt year reachtxt :$55.; run; *Create Excel files for three of the counties and save them in a directory with no other files in it; data abbeville aiken allendale; set all; if ctyid=1 then output abbeville; else if ctyid=2 then output aiken; else if ctyid=3 then output allendale; run; *Export data sets--see webpage for an alternative coding example (using PROC IMPORT) if this does not work; PROC EXPORT DATA= WORK.Abbeville OUTFILE= "/home/grego1/STAT 541/Abbeville.xlsx" DBMS=XLSX REPLACE; SHEET="Abbeville"; RUN; PROC EXPORT DATA= WORK.Aiken OUTFILE= "/home/grego1/STAT 541/Aiken.xlsx" DBMS=XLSX REPLACE; SHEET="Aiken"; RUN; PROC EXPORT DATA= WORK.Allendale OUTFILE= "/home/grego1/STAT 541/Allendale.xlsx" DBMS=XLSX REPLACE; SHEET="Allendale"; RUN; options mprint mlogic symbolgen; %macro drive(dir); %local filrf rc did memcnt name i; /* Assigns a fileref to the directory and opens the directory */ %let rc=%sysfunc(filename(filrf,&dir)); %let did=%sysfunc(dopen(&filrf)); /* Loops through entire directory */ %do i = 1 %to %sysfunc(dnum(&did)); /* Retrieve name of each file */ %let name=%qsysfunc(dread(&did,&i)); /* If directory name call macro again */ %if %qscan(&name,2,.) = %then %do; %drive(&dir/%unquote(&name)) %end; %else %do; filename="&dir/&name"; output; %end; %end; /* Closes the directory and clear the fileref */ %let rc=%sysfunc(dclose(&did)); %let rc=%sysfunc(filename(filrf)); %mend drive; /* Parameter is the directory of where your files are stored. */ data mylist; %drive(/home/grego1/STAT 541 Traffic) run; *We'll create a macro to read the filenames from mylist, save them as macro variables, ; *then stack them in a single data set; %macro importdata; * Creating multiple macro variables in DATA Step; data _null_; set mylist; call symput('num_files',_n_); call symput('file'||compress(_n_,' '),filename); run; %do j=1 %to &num_files; proc import datafile= "&file&j" OUT=file_&j dbms=xlsx replace; getnames=yes; run; %end; /* Stack all the datasets created so far */ proc sql; create table final like file_1; *Empty data set final; quit; %do j=1 %to &num_files; data final; set final file_&j; run; %end; %mend; /* Invoke the macro*/ %let mydir=/home/grego1/STAT 541 Traffic/; %importdata; /* Another approach to the above set of commands that works in the Windows version of SAS; /* This does not work in SAS Studio, since the critical filename-pipe command function can't be used; filename cohort08 'e:\STAT 541\Traffic_Count_Data_2012.csv'; *Read in the traffic count data for all 46 counties; data all; infile cohort08 dlm=','; input ctyid reach roadtype roadid aadt year reachtxt :$55.; run; *Create Excel files for three of the counties and save them in a directory with no other files in it; data abbeville aiken allendale; set all; if ctyid=1 then output abbeville; else if ctyid=2 then output aiken; else if ctyid=3 then output allendale; run; *Export data sets--see webpage for an alternative coding example (using PROC IMPORT) if this does not work; PROC EXPORT DATA= WORK.Abbeville OUTFILE= "e:\STAT 541 Traffic\Abbeville.xlsx" DBMS=XLSX REPLACE; SHEET="Abbeville"; RUN; PROC EXPORT DATA= WORK.Aiken OUTFILE= "e:\STAT 541 Traffic\Aiken.xlsx" DBMS=XLSX REPLACE; SHEET="Aiken"; RUN; PROC EXPORT DATA= WORK.Allendale OUTFILE= "e:\STAT 541 Traffic\Allendale.xlsx" DBMS=XLSX REPLACE; SHEET="Allendale"; RUN; *Here's the important piece of code--the demo of filename-pipe; /* Pipe is a filename option that executes a piece of DOS code (the DIR or directory listing command) and saves the content in the temporary file mylist. The Excel filenames are then saved as entries in the data set MYLIST */ %let mydir=e:\STAT 541 Traffic\; *pipe writes DOS output to external file--/B simplifies output; *pipe does not work for SAS Studio directories, so these commands need to be replaced filename mylist pipe "%str(dir %"&mydir%" /B)" lrecl=5000; data mylist; infile mylist truncover; input filename $ 50.; run; quit; *We'll create a macro to read the filenames from mylist, save them as macro variables, ; *then stack them in a single data set; %macro importdata; * Creating multiple macro variables in DATA Step; data _null_; set mylist; call symput('num_files',_n_); call symput('file'||compress(_n_,' '),compress(filename)); run; %do j=1 %to &num_files; proc import datafile= "&mydir\&&file&j" OUT=file_&j dbms=xlsx replace; getnames=yes; run; %end; /* Stack all the datasets created so far */ proc sql; create table final like file_1; *Empty data set final; quit; %do j=1 %to &num_files; data final; set final file_&j; run; %end; %mend; /* Invoke the macro*/ %importdata;