Getting Data into SAS ® Four General Categories: Create SAS data set from raw data files This is what we’ve been doing so far Enter directly into SAS data set Convert other software’s data files into SAS data sets Read other software’s data files directly
Getting Data into SAS ® CAUTION! Why are ASCII and Text files better to work with? Because they’re permanent! Most other ways that you can read data into SAS data sets are temporary – they last while your session is open They must be converted into permanent data sets to be reused later Data are easily lost
Getting Data into SAS ® Raw data files: The DATA step is versatile enough to read almost any type of raw data file (ASCII, Text) Internal, External, List files, Columns, Non- standard (dates, dollars, etc.) Import Wizard reads particular types of raw data files like comma-separated values (CSV), and other delimited files Can also import from Excel and Access
Getting Data into SAS ® Import Wizard: File Menu Import Data Select the type of data (Excel, Access, Comma Delimited, Tab Delimited) Next, choose the location of the file you want to import
Getting Data into SAS ® Import Wizard: Choose a SAS library (WORK is temporary, save in other to keep) Name the data set (“Member”) Last window gives you the option to save the PROC IMPORT commands
Getting Data into SAS ® If you’re not going to modify the data in any way, you can run procedures with the DATA command specifying where your data set is SAS library.File name
Getting Data into SAS ® To read the data into a program, start with the “IMPORT” commands that you saved, then add your program SAS library.file name Original File Type of File
Getting Data into SAS ® Entering Data Directly with Viewtable Window The “Viewtable” window will let you both view & modify existing SAS data sets as well as create new SAS data sets Displays SAS data sets (also called “tables”) in a tabular view
Getting Data into SAS ® Viewtable window Tools Menu Table Editor Start typing & SAS will determine numeric v. character data, or Right click on a letter (column) & choose “Column Attributes”
Getting Data into SAS ® Viewtable window Enter your data Save your data: File menu Save As… Either select an existing library, or create a new library Name your file (‘Member Name’)
Getting Data into SAS ® If you’re not going to modify the data in any way, you can run direct DATA commands
Getting Data into SAS ® Now, to read the data into a program, you use an “INFILE” statement with the location of the file you created
Getting Data into SAS ® Reading Other Software’s Data Files Directly In most cases, SAS/ACCESS for PCs will allow you to directly import Excel, Lotus, and dBase You use the IMPORT procedure, and add a “DBMS” code for the type of data: PROC IMPORT DATAFILE = ‘filename’ OUT = data-set DBMS = identifier REPLACE;
Getting Data into SAS ® For Excel, Lotus, and dBase files Type of FileExtensionDBMS Identifier Microsoft Excel.xlsEXCEL* EXCEL5 EXCEL4 Lotus.wk4WK4.wk3WK3.wk1WK1 dBase.dbfDBF *The same identifier is used for Excel, Excel2002, Excel2000, Excel97
To read Access files, instead of DATAFILE, you use TABLE and DATABASE PROC IMPORT TABLE = ‘table-name’ OUT = data-set DBMS = identifier REPLACE; DATABASE = ‘database-path’ Type of FileExtensionDBMS Identifier Microsoft Access.mdbACCESS* ACCESS97 *The same identifier is used for Access, Access2002, Access2000
Other Useful Things Arrays, correction commands, etc.
Array Statements Sometimes you want to do the same thing to many variables. Instead of writing a long series of IF statements, you can build an array In SAS, an array is a group of variables They need to be all numeric or all character ARRAY name (n) variable-list;
Array Statements Next, you tell SAS what you want it to do to this group of variables by creating a ‘DO Loop’ (everything between DO and END) ARRAY name (n) variable-list; DO code; IF something THEN something; END;
Array Statements This array changes all values of “9” (refused) to missing data for the four variables in the array
Means You may want to calculate a mean response value for two cross-tabbed variables What was the mean response for the question on parking for people in each age category?
Correcting a Data Error If you find that data has been incorrectly entered into the data set, rather than correcting it in the ASCII set, add a command to the SAS program Tells the program that before running anything else, it must change the value for ‘age’ in id #123 to 35
Create a Random Number List Sometimes you need to randomly select cases from your dataset to work with for analysis or assessment purposes SAS can generate a random number list for you This example creates 25 random numbers
The SAS ® System There’s really not much you can’t do in the way of working with data sets in SAS Just a matter of learning the right code and a little trial and error!