Lesson #1: Importing Data into SAS Data Sets

Review: List, Column and Formatted Input

We'll take a look at a number of examples here and throughout the course. For the sake of simplicity, whenever possible, we'll read in data internally using a DATALINES statement rather than externally using an INFILE statement. This is the lazy learner's way as it will save us from having to download the data file, save the file on our computers, and edit the INFILE statement to reflect the location of the file. Be aware though that, in practice, nine times out of ten, we'll read data externally using an INFILE statement. And, we're pretty sure that that's why the authors of the textbook chose to read in data externally in nine out of ten of their examples!

Reading Raw Data Separated by Spaces

The examples in this section parallel the examples in the LSB section titled "Reading Raw Data Separated by Spaces."

Example 1.1. Reading data into SAS using list input. The following SAS program illustrates a simple example of using list input to read in the weight and three jumping distances of six toads:

As promised above, the SAS code has been modified just so that we can read the data internally using the DATALINES statement rather than in an external data file using an INFILE statement. Oh, we also adhered to good programming habit by adding a RUN statement to close out the DATA step.

Launch and run the SAS program. Review the output to convince yourself that SAS read in the data as the textbook promised. And, make sure you look for the note in the SAS log concerning going to a new line when the INPUT statement reached past the end of a line.

Example 1.2. Reading data into SAS using list input. Here's another SAS program that illustrates a simple example of using list input to read in the subject number, name, gender, weight, and height of five individuals:

Launch and run the SAS program, and review the output to convince yourself that SAS read in the data as expected.

Reading Raw Data Arranged in Columns

The examples in this section parallel the examples in the LSB section titled "Reading Raw Data Arranged in Columns."

Example 1.3. Reading data into SAS using column input. The following SAS program illustrates a simple example of using column input to read in data concerning onion ring sales at a minor league baseball stadium:

Again, the SAS code has been modified just so that we can read the data internally using the DATALINES statement rather than in an external data file using an INFILE statement. In doing so, note that the each record starts in the leftmost column, i.e., in column 1, so that the column numbers agree with those in the textbook. Launch and run the SAS program. Review the output to convince yourself that SAS read in the data as the textbook promised.

Reading Raw Data Not in Standard Format

The examples in this section parallel the examples in the LSB section titled "Reading Raw Data Not in Standard Format."

Example 1.4. Reading data into SAS using formatted input. The following SAS program illustrates a simple example of using formatted input to read in data concerning results from a local pumpkin-carving contest:

Launch and run the SAS program. Review the output to convince yourself that SAS read in the data as the textbook promised. Also, note that since the variable Date was not formatted for output, the dates appear in the SAS data set as the number of days since January 1, 1960. Recall that in order to tell SAS to print dates that make sense to us, we need to format dates. We can do so here by adding the following FORMAT statement after the INPUT statement and before the DATALINES statement:

FORMAT Date mmddyy10.;

Go ahead and add the FORMAT statement, re-run the SAS program, and take a look at the output to see for yourself.

The LSB section titled "Selected Informats" provides a good list of commonly used informats along with the width range and default width. You might want to flag the section in some way so that you can refer back to it as necessary.

© 2007 The Pennsylvania State University. All rights reserved.