70 likes | 84 Views
Learn how to import data into SAS from different sources, examine file structures for accurate import, create temporary and permanent datasets, handle quirks while importing, and utilize SAS syntax effectively. Practice importing various file formats and manipulating datasets in SAS.
E N D
Bringing Data into SAS • From Menu: • File • Import Data • Spreadsheet example first • Pick file by browsing • Select Library and Member (we will talk about this on the next slide • Create file that does import • Nothing happens! Meaning, you don’t see any results, but file is created • Look at import file that is created • Caution – need to worry about formats that are carried along during import. They may not be obvious!
Bringing Data into SAS • Important Concepts in SAS • Permanent and Temporary datasets (tables) • Libraries Libname class “c:\data\class\fw893”; data species; infile "c:\data\class\fw893\fish_species.txt"; input species_code common $ 12-32 family $ genus $ species $; run; data class.species; infile "c:\data\class\fw893\fish_species.txt"; input species_code common $ 12-32 family $ genus $ species $; run; { This creates a temporary table named species { This creates a permanent table named species in the directory specified in the libname
Bringing Data into SAS • ASCII files • May seem a bit harder than an Excel file, but more general • First need to examine structure of file so can import properly. I frequently use Notepad or similar simple text editor • Count columns, names of fields, etc.
Bringing Data into SAS • Fish Species Table example: • Species Code column 1-2 • Common name column 12-32 • Family column 62-75 • Genus column 112-125 • Species column 162-174 • Show SAS code
Bringing Data into SAS • Important “Quirks” • Numeric fields can be imported without specifying column placement • Variables that are alphanumeric are followed by a dollar sign • If alphanumeric data do not have a space between them, AND text is 8 characters or less, you can import without specifying column placement. • Need to be careful if try to read beyond end of line by having too many fields (need to watch for ending fields that are empty)
Bringing Data into SAS • Once you have a permanent dataset (table) created, you can read it in using the following syntax: Libname class “c:\data\class\fw893”; data class.species; infile "c:\data\class\fw893\fish_species.txt"; input species_code common $ 12-32 family $ genus $ species $; run; data dummy; set class.species; run; You can think of the “DATA” step as meaning “create the following table” You can think of the “SET” command as meaning “use the following table”
In-class exercise/homework • Import the data in sample_list.txt and print out the table in SAS • Import the data in sample_list.xls files into SAS and print out the resulting table. How do they compare • Try to read in the transect data set (which is a permanent data set) from the class drive, and print off this dataset. Note you will need to specify the correct library name.