In this section, we'll review how to read data already contained in one SAS data set into another SAS data set.
Example 13.1 Section
The following program uses the DATA step's SET statement to create a temporary SAS data set called work.penngolf, which is identical to the permanent SAS data set called stat481.penngolf:
Copy the code below:
OPTIONS PS = 58 LS = 72 NODATE NONUMBER;
LIBNAME stat481 'C:\yourdrivenIfame\Stat481WC\01sasdata\sasndata';
PROC PRINT data = penngolf NOOBS;
title 'The penngolf data set';
Of course, the LIBNAME statement just tells SAS that the nickname for the directory in which the permanent SAS data set resides is stat481. Now for the DATA step:
- Because a one-level name is used, the DATA statement tells SAS to create a temporary data set called penngolf. Recall that SAS stores all temporary data sets in a temporary library called work. Therefore, we can refer to the created data set either by its one-level name penngolf, or by its two-level name work.penngolf.
- The SET statement tells SAS to assign the data in the existing permanent SAS data set stat481.penngolf — observation by observation — to the temporary SAS data set called penngolf. Because the variables in the existing data set have already been named, no INPUT statement is necessary. In fact, if you were to include an INPUT statement, SAS would croak back at you.
That's all there is to it. Before we have you run this thing, let me confess something to you — this has got to be the silliest program around! There is really no good reason for taking a permanent SAS data set and simply turning it into a temporary SAS data set without including other statements in the DATA step to modify the data set in some way. Doing so just reduces the efficiency of your programs. Instead, you should just go ahead and use the permanent SAS data set in any procedure you want. What I am begging for here is not to let me (or anyone else!) see you mimic this kind of DATA step in your future programs. I certainly wouldn't want you to pick up any bad habits from this course.
That confession made, let's run the program! Before the program will work for you, you will, of course, need to download and save the penngolf data set (click to save!) in a convenient location on your computer. Then, launch the SAS program, and edit the LIBNAME statement so it reflects the location in which you saved the data set on your computer. Then, run the program, and review the output to familiarize yourself with the data set that we'll work with throughout the remainder of the lesson.