2.3 - Reading Data into Permanent SAS Data Sets

2.3 - Reading Data into Permanent SAS Data Sets

Rather than having created a temporary data set in Example 2.1 that disappears at the end of our SAS session, we could have just as easily created a permanent data set that instead gets stored in a directory of our choosing and is therefore available to us permanently. Let's try that!

Example 2.2

The following SAS program illustrates how to create a permanent SAS data set called stat480.temp2 to read instream data using column input:

The following SAS program illustrates the simplest example of column input.

LIBNAME stat480 'C:\stat480\data\';  *Specifies the SAS data
                                              library (directory);
DATA stat480.temp2;
   input subj 1-4 gender 6 height 8-9 weight 11-13;
  DATALINES;
1024 1 65 125
1167 1 68 140
1168 2 68 190
1201 2 72 190
1302 1 63 115
  ;
RUN;
     
PROC PRINT data=stat480.temp2;
   title 'Output dataset: STAT480.TEMP2';
RUN;

Note: In the upper right-hand corner of the code block you will have the option of copying ( ) the code to your clipboard or downloading ( ) the file to your computer.

LIBNAME stat480 'C:\stat480\data\';  *Assigns a nickname 'stat480' to a 
location (library) accessible to your computer;
DATA stat480.temp2; *A tow-level name indicates a permanent 
  dataset 'temp2.sas7bdat' stored in the location specified above;
    input subj 1-4 gender 6 height 8-9 weight 11-13;
  DATALINES;
1024 1 65 125
1167 1 68 140
1168 2 68 190
1201 2 72 190
1302 1 63 115
  ;
RUN;
PROC PRINT data=stat480.temp2; *Always refer to the permanent 
   data set with a two-level name;
   title 'Output dataset: STAT480.TEMP2';
RUN;

Note that the only differences between this program and the program in Example 2.1 is:

  • there is now a LIBNAME statement. This is how we tell SAS to which of our directories we want the data permanently written. Here, the SAS data set is written to our C:\stat480\data directory.
  • the name of the data set appearing in the DATA statement is now a two-level name. This is how SAS knows to put the permanent dataset temp2 in our C:\stat480\data directory.
  • in subsequent procedures, such as the PRINT procedure here, we refer to the permanent data set by its two-level name.

Launch and run the SAS program. Don't forget that you must make sure that you have already created the C:\stat480\data subdirectory on your computer (or at least some other appropriately placed and named subdirectory). After running the program, note that:

  • the contents and structure of stat480.temp2 is identical to the contents and structure of work.temp1. The only thing that differs between the two datasets is that temp1 is temporary and temp2 is permanent.
  • in the Explorer Window, the dataset temp2 appears in the stat480 library indicating that the data set is permanent.
  • using your Windows Explorer, you can see that the SAS data set is permanently stored in your C:\stat480\data directory.

Legend
[1]Link
Has Tooltip/Popover
 Toggleable Visibility