2.2 - SAS Data Libraries

2.2 - SAS Data Libraries

Before we can really get a handle on the distinction between temporary and permanent SAS data sets, we need to understand what SAS refers to as SAS data libraries. In short, a SAS library is simply a collection of SAS files that are stored in the same folder or directory on your computer. Other files can be stored in the same folder or directory, but only the files that have SAS file extensions are recognized as part of the SAS library.

Depending on the library name that you use when you create a SAS file, such as a SAS data set, SAS files are stored either temporarily or permanently as follows:

  • When creating a SAS file, if you use the library name Work or you don't specify a library name at all, then the file is stored in a temporary SAS library called Work. When you close out the SAS session in which you created the SAS file, the temporary library and all of its files are removed from your computer's memory.
  • If you use a library name other than the default library name Work when creating a SAS file, then the file is stored permanently until you delete it. That is, you can use permanent SAS libraries and their contents in subsequent SAS sessions.

Referencing Permanent SAS Files

Regardless of whether a SAS data set is temporary or permanent, SAS always refers to the data set by a two-level name:

libref.filename

In the two-level name, libref is the (nick)name that you gave the SAS data library that contains the SAS data set, and filename is the name of the file itself. For example:

in order to print the permanent data set called Back that is stored in the SAS library called Stat480, we have to refer to the permanently stored SAS data set as:

Stat480.Back

as in the following code:

PROC print data = Stat480.Back;
RUN;

In this example, we know the SAS data set called Back is permanent, because the SAS library name is Stat480, not Work.

Referencing Temporary SAS Files

As stated previously, regardless of whether a SAS data set is temporary or permanent, SAS always refers to the data set by a two-level name:

libref.filename

Now, we know that the libref of a temporary data set is always Work. Therefore, we can refer to any SAS data set stored temporarily in Work by:

Work.filename

where filename is the name of the file itself. For example:

in order to print the temporary SAS data set called Temp1 that is stored in the default temporary SAS library called Work, we can refer to the temporarily stored SAS data set as:

Work.Temp1

as in the following code:

PROC PRINT data = work.temp1;
RUN;

Now, although SAS always refers to SAS data sets by their two-level names, it doesn't mean you have to do the same! In this case, SAS lets you take a shortcut by using just the one-level name:

filename

When you specify a one-level name, the default libref Work is assumed. For example, rather than using the previous code to print the temporary SAS data set called Temp1, we could use the following code:

PROC PRINT data = Temp1;
RUN;

Defining Libraries

We have just one more thing to tend to before we complete our discussion of SAS libraries, and that is how to define a library. To do so, we use a LIBNAME statement to tell SAS to associate a a name — called a libref — to one of the folders or directories on your computer that contains SAS files. For example, I have a subdirectory on my computer:

C:\Simon\Stat480WC\sp08\04datastep\sasndata

that contains three SAS data sets (back, log, and qul):

To tell SAS to associate the libref Stat480 with this subdirectory, we simply use a LIBNAME statement as follows:

LIBNAME Stat480 'C:\Simon\Stat480WC\sp08\04dtatstep\sasdata';

In general, the libref can be a nickname of your choosing, as long as it is between 1 and 8 characters long, begins with a letter or underscore, and contains only letters, numbers, or underscores. The specification of the physical name of the library, of course, adheres to the conventions of the Windows operating system. You can use as many LIBNAME statements in a program as necessary to assign the librefs that you need.

Now, after submitting our LIBNAME statement, we can (and should!) look at the log window to verify that SAS assigned the libref successfully. If all went well, we should see a message similar to this in this log window:

32
33  LIBNAME Stat480 'C:\Simon\Stat480WC\sp08\04datastep\sasndata';
NOTE: Libref STAT 480 was successfully assigned as follows:
      Engine:         V9
      Physical Name: C:\Simon\Stat480WC\sp08\04datastep\sasndata
34

Because the LIBNAME statement is a global statement, our Stat480 libref remains in effect until we have modified it, canceled it, or ended the SAS session. That is, the LIBNAME statement assigns the libref for the current SAS session only. Each time you begin a SAS session, you must assign a libref to each permanent SAS data library that contains files that you want to access in that SAS session.

Phewww ... now that we've completed our digression on SAS libraries, let's revisit Example 2.1 on the previous page and create a permanent SAS data set rather than a temporary one.


Legend
[1]Link
Has Tooltip/Popover
 Toggleable Visibility