Docsity

Importing and Merging Data from CSV Files using SAS, Study notes of Statistics

Toggle sidebar
bg1
Statistical Programming in SAS Bailer
Chapter 2 (was Ch. 8) (September 21, 2008; 4:54 p.m.)
Constructing a data set for analysis: reading, combining, managing
and manipulating data sets
2. Constructing a data set for analysis: reading, combining, managing and manipulating
data sets
1. Temporary versus permanent status of data sets – LIBNAME
2. Reading data into a SAS data set
2.1 Reading data directly as part of a program – anyone for datalines?
2.2 Reading data sets that were saved as text – infile can be your friend
2.3 Sometimes variables are in particular columns or in particular formats
2.4 Reading comma-separated values – text files with “,” delimiters
2.5 Reading Excel spreadsheets directly
2.6 Reading SPSS data files – the little (SPSS) engine that could
3. Writing out a file or constructing a simple report
4. Concatenating data sets/adding observations – SET
5. Merging data sets/adding variables – MERGE
6 Database processing with PROC SQL
7. Next steps and a summary
Exercises
2.1. Temporary versus permanent status of data sets – LIBNAME
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
brood1
brood2
brood3
24.0
3.0
10.0
10.0
30.0
5.0
11.0
12.0
32.5
5.5
12.0
15.0
34.0
6.0
14.0
15.0
36.0
6.0
15.0
17.0
1
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a

Partial preview of the text

Download Importing and Merging Data from CSV Files using SAS and more Study notes Statistics in PDF only on Docsity!

Chapter 2 (was Ch. 8) (September 21, 2008; 4:54 p.m.)

Constructing a data set for analysis: reading, combining, managing

and manipulating data sets

  1. Constructing a data set for analysis: reading, combining, managing and manipulating data sets
  2. Temporary versus permanent status of data sets – LIBNAME
  3. Reading data into a SAS data set 2.1 Reading data directly as part of a program – anyone for datalines? 2.2 Reading data sets that were saved as text – infile can be your friend 2.3 Sometimes variables are in particular columns or in particular formats 2.4 Reading comma-separated values – text files with “,” delimiters 2.5 Reading Excel spreadsheets directly 2.6 Reading SPSS data files – the little (SPSS) engine that could
  4. Writing out a file or constructing a simple report
  5. Concatenating data sets/adding observations – SET
  6. Merging data sets/adding variables – MERGE 6 Database processing with PROC SQL
  7. Next steps and a summary Exercises

2.1. Temporary versus permanent status of data sets – LIBNAME

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

Lower Upper

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;

2.2. Reading data into a SAS data set

Common starting point for a statistical collaborator or consultant:

  • meet with a colleague or client who is interested in securing assistance in exploring

relationships among a collection of variables, testing for treatment differences, or other really fun stuff.

  • Critical question: do you have your data in an electronic format? The answer to this

question varies across disciplines. Colleagues from the biological and medical sciences

  • brood 80 10 total
  • brood
  • brood
                    • 36. - 8. - 13. - 18. - brood 160 10 total - brood - brood - 23. - 2. - 8. - 10. - 27. - 6. - 10. - 11. - 29. - 6. - 12. - 11. - 30. - 6. - 12. - 12. - 31. - 6. - 13. - 13. - brood 235 10 total - brood - brood - 7. - 2. - 0. - 0. - 13. - 4. - 0. - 6. - 16. - 6. - 3. - 6. - 21. - 6. - 10. - 9. - 27. - 7. - 13. - 10. - brood 310 10 total - brood - brood - 0. - 0. - 0. - 0. - 5. - 5. - 0. - 0. - 6. - 5. - 0. - 0. - 6. - 6. - 0. - 0. - 15. - 7. - 10. - 0.

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

2.2.2 Reading data sets that were saved as text – infile can be your friend

/* Notes:

  1. Two spaces separate each entry in the input data file. A global replace of tabs with two spaces was performed before saving the text file.
  2. The first line contains the names of the variables, and the data begins on the 2nd line of the file.
  3. Fort Worth had missing values for POP and INCOME and "." were entered in the data file.
  4. Increased LENGTH to 39 to accomodate GREENSBORO-WINSTON-SALEM... */ options nodate nocenter; data SMSA_from_txt; infile "C:\Users\baileraj\Desktop\SMSA-DASL-2space-sep.txt" firstobs=2; length city $ 39; input city & JanTemp JulyTemp RelHum Rain Mortality Education PopDensity pct_NonWhite pct_WC pop pop_per_house income HCPot NOxPot S02Pot NOx; run; ods rtf bodytitle; proc print data=SMSA_from_txt; var city JanTemp JulyTemp RelHum NOxPot S02Pot NOx; run; ods rtf close;

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

Display 2.15: SAS LOG with the echo of the nitrofen variable values

animal=1 conc=0 brood1=3 brood2=14 brood3=10 total=27 ERROR=0 N=

animal=2 conc=0 brood1=5 brood2=12 brood3=15 total=32 ERROR=0 N=

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=49 conc=310 brood1=6 brood2=0 brood3=0 total=6 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

2.5 Merging data sets/adding variables – MERGE

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

2.6 Database processing with PROC SQL

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;