


















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!



















Display 2.1: Defining a library and referencing a data set within that library libname myfiles "\muserver2\users\b\baileraj\public.www\classes\sta402\data";
ods rtf bodytitle; proc means data=myfiles.nitrofen maxdec=1 min q1 median q3 max ; class conc; var total brood1 brood2 brood3; run; ods rtf close;
Display 2.2: Concentration-specific five-number summary for brood counts
conc N Obs Variable Minimum
Lower Quartile Median
Upper Quartile Maximum 0 10 total brood brood brood
conc N Obs Variable Minimum Quartile Median Quartile Maximum Display 2.3: Example code that generates a temporary data set data junk; do kk = 0 to 10 by 0.01; x = kk; y = 3 + 2kk + rannor(0); output; end; run; proc plot data=junk; * or equivalently, data=work.junk; plot yx; run; proc reg data=junk; model y=x; run;
Common starting point for a statistical collaborator or consultant:
relationships among a collection of variables, testing for treatment differences, or other really fun stuff.
question varies across disciplines. Colleagues from the biological and medical sciences
Baltimore, MD 35 77 55 43 1071.29 9.6 6441 24.4 43. 2199531 3.44 32368 43 38 206 38 ; ods rtf bodytitle; proc print data=SMSA_subset; var city JanTemp JulyTemp RelHum NOxPot S02Pot NOx; run; ods rtf close;
Did this work?
Display 2.5: PROC PRINT of the misread subset of SMSA observations
Obs City JanTemp JulyTemp RelHum NOxPot S02Pot NOx (^1) Akron,. 27 71 21 15 59 (^2) Albany-S. 23 72 8 10 39 (^3) Allentow.. 29 31856 6 6 (^4) Atlanta,. 45 79 18 8 24 (^5) Baltimor. 35 77 43 38 206
SAS Log (slightly edited) error message associated with processing the first observation is
NOTE: Invalid data for JanTemp in line 46 8-9. RULE: ----+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+-- Akron, OH 27 71 59 36 921.87 11.4 3243 8.8 42.6 660328 3.34 29560 21 15 59 101 15 city=Akron, JanTemp=. JulyTemp=27 RelHum=71 Rain=59 Mortality=36 Education=921.87 PopDensity=11. pct_NonWhite=3243 pct_WC=8.8 pop=42.6 pop_per_house=660328 income=3.34 HCPot=29560 NOxPot= S02Pot=15 NOx=59 ERROR=1 N=
Display 2.6: Reading the SMSA data with a length declaration and code to allow embedded blanks in the value of a character value. data SMSA_subset; length city $ 27; input city & JanTemp JulyTemp RelHum Rain Mortality Education PopDensity pct_NonWhite pct_WC pop pop_per_house income HCPot NOxPot S02Pot NOx; datalines; Akron, OH 27 71 59 36 921.87 11.4 3243 8.8 42.6 660328 3.34 29560 21 15 59 15 Albany-Schenectady-Troy, NY 23 72 57 35 997.87 11.0 4281 3.5 50.7 835880 3.14 31458 8 10 39 10 Allentown, Bethlehem, PA-NJ 29 74 54 44 962.35 9.8 4260 0.8 39.4 635481 3. 31856 6 6 33 6 Atlanta, GA 45 79 56 47 982.29 11.1 3125 27.1 50.2 2138231 3.41 32452 18 8 24 8 Baltimore, MD 35 77 55 43 1071.29 9.6 6441 24.4 43.7 2199531 3.44 32368 43 38 206 38 ;
ods rtf bodytitle; proc print data=SMSA_subset; var city JanTemp JulyTemp RelHum NOxPot S02Pot NOx; run; ods rtf close;
Display 2.7: PROC PRINT output from the correctly read subset of the SMSA observations
Obs City JanTemp JulyTemp RelHum NOxPot S02Pot NOx (^1) Akron, OH 27 71 59 15 59 15 (^2) Albany-Schenectady- Troy, NY
23 72 57 10 39 10
(^3) Allentown, Bethlehem, PA-NJ
29 74 54 6 33 6
(^4) Atlanta, GA 45 79 56 8 24 8 (^5) Baltimore, MD 35 77 55 38 206 38
/* Notes:
Display 2.9: Output from PROC PRINT of the SMSA subset read using infile
Obs City JanTemp JulyTemp RelHum NOxPot S02Pot NOx (^1) Akron, OH 27 71 59 15 59 15 (^2) Albany-Schenectady-Troy, NY 23 72 57 10 39 10
Obs City JanTemp JulyTemp RelHum pop
pop_per _house income 17 Dayton-Springfield, OH 30 75 58 942083 3.35 30232 18 Denver, CO 30 73 38 1428836 3.15 39099 19 Detroit, MI 27 74 59 4488072 3.44 33858 20 Flint, MI^24 72 61 450449 3.53^32000 21 Fort Worth, TX 45 85 53. 3.. 22 Grand Rapids, MI 24 72 61 601680 3.37 29915 23 Greensboro-Winston-Salem-High Point, NC
40 77 53 851851 3.45 29450
24 Hartford, CT 27 72 56 715923 3.25 37565 25 Houston, TX 55 84 59 2735766 3.35 39558
2.2.5 Reading Excel spreadsheets directly
Display 2.12: Importing an Excel spreadsheet using IMPORT or the Excel engine with a LIBNAME /* Assumes SAS/ACCESS engines are available / / IMPORT code produced by the IMPORT WIZARD */
PROC IMPORT OUT= WORK.DASL_FROM_EXCEL DATAFILE= "C:\Documents and Settings\baileraj\Desktop\DASL-SMSA.xls" DBMS=EXCEL REPLACE; SHEET="Sheet1$"; GETNAMES=YES; MIXED=NO; SCANTEXT=YES; USEDATE=YES; SCANTIME=YES; RUN;
/* ------------------------- via EXCEL engine with LIBNAME */ options nodate; title ;
libname spreads excel "C:\Documents and Settings\baileraj\Desktop\DASL-SMSA.xls" header=yes;
ods rtf bodytitle; /* list the contents of the spreadsheet that was read */ proc contents data=spreads.all varnum; run;
/* set contents of the 1st worksheet to SAS data set */ data DASL_from_Libname_Excel_engine; set spreads.'sheet1$'n; run;
proc print data=DASL_from_Libname_Excel_engine;
run; ods rtf close;
Display 2.13: Contents of the SAS library produced when referencing an Excel spreadsheet using a LIBNAME with an Excel engine.
Directory Libref (^) SPREADS Engine (^) EXCEL Physical Name (^) C:\Documents and Settings\baileraj\Desktop\DASL-SMSA.xls Schema/Owner (^) Admin
# Name Member Type
DBMS Member Type (^1) Sheet1$ DATA TABLE 2 Sheet2$ DATA TABLE 3 Sheet3$ DATA TABLE
Data Set Name (^) SPREADS.'Sheet1$'n Observations (^). Member Type (^) DATA Variables (^) 17 Engine (^) EXCEL Indexes (^) 0 Created (^). Observation Length (^) 0 Last Modified (^). Deleted Observations (^) 0 Protection Compressed (^) NO Data Set Type Sorted (^) NO Label Data Representation (^) Default Encoding (^) Default
animal=3 conc=0 brood1=6 brood2=11 brood3=17 total=34 ERROR=0 N= ... animal=48 conc=310 brood1=4 brood2=0 brood3=0 total=4 ERROR=0 N=
animal=50 conc=310 brood1=5 brood2=0 brood3=0 total=5 ERROR=0 N=
Display 2.16: Producing a formatted report with an annotated print out of the nitrofen data libname myfiles "\muserver2\users\b\baileraj\public.www\classes\sta402\data";
proc sort data=myfiles.nitrofen; by conc; data NULL; set myfiles.nitrofen; by conc; file 'C:\Users\baileraj\Desktop\nitrofen-put.txt';
/* Write out header text information for the first observation in each conc/ if FIRST.conc then do; put 6'+-----' '+'; put 'Conc = ' conc / @1 'Brood 1' @10 'Brood 2' @20 'Brood 3' @30 'TOTAL' / @1 '-------' @10 '-------' @20 '-------' @30 '-----'; end;
/* Write out the data for all records */ put @5 brood1 3. @14 brood2 3. @24 brood3 3. @32 total 3.;
data NULL; /* add last line to the output file / file 'C:\Users\baileraj\Desktop\nitrofen-put.txt' MOD; put 6'+-----' '+' // 'Data from C. dubia Nitrofen exposure data [Bailer & Oris 1993]'; run;
Display 2.17: Contents of the nitrofen-put.txt file constructed by PUT
+-----+-----+-----+-----+-----+-----+ Conc = 0 Brood 1 Brood 2 Brood 3 TOTAL
3 14 10 27 5 12 15 32 6 11 17 34 6 12 15 33 6 15 15 36 5 14 15 34 6 12 15 33
5 13 12 30 3 10 11 24 6 11 14 31 +-----+-----+-----+-----+-----+-----+
... lines deleted... +-----+-----+-----+-----+-----+-----+ Conc = 310 Brood 1 Brood 2 Brood 3 TOTAL
6 0 0 6 6 0 0 6 7 0 0 7 0 0 0 0 5 10 0 15 5 0 0 5 6 0 0 6 4 0 0 4 6 0 0 6 5 0 0 5 +-----+-----+-----+-----+-----+-----+
Data from C. dubia Nitrofen exposure data [Bailer & Oris 1993]
Display 2.18: Creating external data files containing the contents of a SAS data set libname myfiles "\muserver2\users\b\baileraj\public.www\classes\sta402\data";
/* create a TEXT file with the nitrofen data */ data NULL; set myfiles.nitrofen; file 'C:\Users\baileraj\Desktop\nitrofen-TEXT.txt'; put @1 conc @10 animal @15 brood1 @20 brood2 @25 brood3 @30 total;
/* create a CSV file with the nitrofen data */ data NULL; set myfiles.nitrofen; file 'C:\Users\baileraj\Desktop\nitrofen-CSV.csv' dsd; put conc animal brood1 brood2 brood3 total;
/* create a CSV file with the nitrofen data and a row with variable names */ data NULL; file 'C:\Users\baileraj\Desktop\nitrofen-CSV2.csv' dsd; put "conc,animal,brood1,brood2,brood3,total";
data NULL; set myfiles.nitrofen; file 'C:\Users\baileraj\Desktop\nitrofen-CSV2.csv' dsd MOD; put conc animal brood1 brood2 brood3 total;
run;
Display 2.19: First 4 lines from the 3 data files constructed using PUT statements nitrofen-TEXT.txt 0 1 3 14 10 27 0 2 5 12 15 32 0 3 6 11 17 34 0 4 6 12 15 33
Display 2.21: Combining observations for 5 separate concentration-specific data sets into a single analysis data set data conc_0; input total @@; conc = 0; * define a variable containing the value of the nitrofen conc.; datalines; 27 32 34 33 36 34 33 30 24 31 ; data conc_80; input total @@; conc = 80; * define a variable containing the value of the nitrofen conc.; datalines; 33 33 35 33 36 26 27 31 32 29 ; data conc_160; input total @@; conc = 160; * define a variable containing the value of the nitrofen conc.; datalines; 29 29 23 27 30 31 30 26 29 29 ; data conc_235; input total @@; conc = 235; * define a variable containing the value of the nitrofen conc.; datalines; 23 21 7 12 27 16 13 15 21 17 ; data conc_310; input total @@; conc = 310; * define a variable containing the value of the nitrofen conc.; datalines; 6 6 7 0 15 5 6 4 6 5 ; data set_all5; set conc_0 conc_80 conc_160 conc_235 conc_310;
ods rtf bodytitle; proc print data=set_all5; var conc total; run; ods rtf close;
Display 2.22: Combined data set formed from concatenating the 5 concentration-specific data sets
Obs conc total
Obs conc total (^1) 0 27 … (^) … … (^11) 80 33 … (^) … … (^20) 80 29 (^21) 160 29 … (^) … … (^30) 160 29 (^31) 235 23 … (^) … … (^40) 235 17 (^41) 310 6 … (^) … … (^50) 310 5
Display 2.23: Adding variables / "columns" – merging data sets Observation ID
Variable 1
Variable
Variable 3 1 2 3 4 5 6 7
MERGED WITH Observation ID
Variable
Variable 5 1 2 3 4 5 6 7
data all_subset; merge SMSA_subset_weather SMSA_subset_demog SMSA_subset_pollution; by city;
ods rtf bodytitle; proc print data=all_subset; var city JanTemp mortality NOx; run; ods rtf close;
Display 2.25: Resulting common SMSA data file after the merging
Obs city JanTemp Mortality NOx (^1) Akron, OH 27 921.87 15 (^2) Albany-Schenectady-Troy, NY 23 997.87 10 (^3) Allentown, Bethlehem, PA-NJ 29 962.35 6 4 Atlanta, GA 45 982.29 8 5 Baltimore, MD 35 1071.29 38
The big idea is that PROC SQL looks at data sets as “tables” that can be manipulated, processed and displayed as a result of command queries. The basic syntax, excerpted from the SAS help documents, includes:
PROC SQL; CREATE TABLE name; SELECT variables FROM list-of-tables/dataset GROUP BY /* optional / HAVING / optional */ ;
Display 2.26: Requesting that a simple SQL table be constructed from a SAS data set containing the highest nitrofen concentration data data conc_310; input total @@; conc = 310; * define a variable containing the value of the nitrofen conc.; datalines; 6 6 7 0 15 5 6 4 6 5 ;
ods rtf bodytitle; proc sql; select conc,total from conc_310; run; ods rtf close;
Display 2.27: Table with the total number of young produced in the highest nitrofen concentration condition
conc total 310 6 310 6 310 7 310 0 310 15 310 5 310 6 310 4 310 6 310 5 The real power of PROC SQL begins to surface as you manipulate table relations. For example, we concatentate the 5 nitrofen concentration data sets using a union of tables operation in Display 2.28. In the PROC SQL command, we construct a SAS data set “all_sql” as a result of the line create table all_sql as
and select all variables from each data set as part of the
select * from conc_
command (the * is a wildcard representing the selection of all variables. The union option “stacks” the table into a resultant table while order by conc,id;
sorts the values by id within conc.
Display 2.28: Containing data sets using union of tables in PROC SQL data conc_0; input total @@; conc = 0; * define a variable containing the value of the nitrofen conc.; id=n; * define an animal ID corresponding the observation number; datalines; 27 32 34 33 36 34 33 30 24 31 ; data conc_80; input total @@; conc = 80; * define a variable containing the value of the nitrofen conc.; id=n; * define an animal ID corresponding the observation number; datalines; 33 33 35 33 36 26 27 31 32 29 data conc_160; input total @@;
data SMSA_subset_weather; length city $ 27; input city & JanTemp JulyTemp RelHum Rain; datalines; Akron, OH 27 71 59 36 Albany-Schenectady-Troy, NY 23 72 57 35 Baltimore, MD 35 77 55 43 Allentown, Bethlehem, PA-NJ 29 74 54 44 Atlanta, GA 45 79 56 47 ;
data SMSA_subset_demog; length city $ 27; input city & Mortality Education PopDensity pct_NonWhite pct_WC pop pop_per_house income; datalines; Akron, OH 921.87 11.4 3243 8.8 42.6 660328 3.34 29560 Albany-Schenectady-Troy, NY 997.87 11.0 4281 3.5 50.7 835880 3.14 31458 Baltimore, MD 1071.29 9.6 6441 24.4 43.7 2199531 3.44 32368 Allentown, Bethlehem, PA-NJ 962.35 9.8 4260 0.8 39.4 635481 3.21 31856 Atlanta, GA 982.29 11.1 3125 27.1 50.2 2138231 3.41 32452 ;
data SMSA_subset_pollution; length city $ 27; input city & HCPot NOxPot S02Pot NOx; datalines; Akron, OH 21 15 59 15 Albany-Schenectady-Troy, NY 8 10 39 10 Baltimore, MD 43 38 206 38 Allentown, Bethlehem, PA-NJ 6 6 33 6 Atlanta, GA 18 8 24 8 ; proc sql; create table SMSA_subset_sql as select * from SMSA_subset_weather w, SMSA_subset_demog d, SMSA_subset_pollution p where w.city=d.city and d.city=p.city;
ods rtf bodytitle; proc print data=SMSA_subset_sql; var city JulyTemp education S02Pot; run; ods rtf close;
Display 2.31: Table / data set produced by the inner join of the 3 SMSA subset data sets.
Obs City JulyTemp Education S02Pot (^1) Akron, OH 71 11.4 59 (^2) Albany-Schenectady-Troy, NY 72 11.0 39 (^3) Baltimore, MD 77 9.6 206
Obs City JulyTemp Education S02Pot (^4) Allentown, Bethlehem, PA-NJ 74 9.8 33 (^5) Atlanta, GA 79 11.1 24
Display 2.32: Illustrating Left/Right/Full outer joins with a subset of the SMSA observations
data SMSA_subset_weather2; length city $ 27; input city & JanTemp JulyTemp RelHum Rain; datalines; Akron, OH 27 71 59 36 Baltimore, MD 35 77 55 43 Allentown, Bethlehem, PA-NJ 29 74 54 44 Atlanta, GA 45 79 56 47 ;
data SMSA_subset_demog2; length city $ 27; input city & Mortality Education PopDensity pct_NonWhite pct_WC pop pop_per_house income; datalines; Akron, OH 921.87 11.4 3243 8.8 42.6 660328 3.34 29560 Albany-Schenectady-Troy, NY 997.87 11.0 4281 3.5 50.7 835880 3.14 31458 Baltimore, MD 1071.29 9.6 6441 24.4 43.7 2199531 3.44 32368 Allentown, Bethlehem, PA-NJ 962.35 9.8 4260 0.8 39.4 635481 3.21 31856 ;
ods rtf bodytitle; /* inner join / conventional join */ proc sql; title "inner join"; select w.city,JanTemp,JulyTemp,Education,income from SMSA_subset_weather2 as w,SMSA_subset_demog2 as d where w.city=d.city;
/* LEFT outer join - with duplicate columns */ proc sql; title "LEFT outer join with duplicate columns"; select * from SMSA_subset_weather2 as w left join SMSA_subset_demog2 as d on w.city=d.city;
/* LEFT outer join - eliminating duplicate columns using COALESCE */ proc sql; title "LEFT outer join eliminating duplicate columns"; select coalesce(w.city, d.city),JanTemp,JulyTemp,Education,income from SMSA_subset_weather2 as w left join SMSA_subset_demog2 as d on w.city=d.city;