Previous Page | Next Page

Understanding SAS Data Libraries

Referencing SAS Data Sets in a SAS Data Library


Understanding Data Set Names

Every SAS data set has a two-level name of the form libref.filename. You can always reference a file with its two-level name. However, you can also use a one-level name (just filename) to reference a file. By default, a one-level name references a file that uses the libref WORK for the temporary SAS data library.

Note:   This section separates the issues of permanent versus temporary files and one-level versus two-level names. Other topics in this documentation and most SAS documentation assume typical use of the WORK libref and refer to files that are referenced with a one-level name as temporary and to files that are referenced with a two-level name as permanent.  [cautionend]

Operating Environment Information:    The documentation that is provided by the vendor for your operating environment provides information about how to create temporary and permanent files. From the point of view of SAS, files in the WORK library are temporary unless you specify the NOWORKINIT and NOWORKTERM options and the files in all other SAS data libraries are permanent. However, your operating environment's point of view might be different. For example, the operating environment might enable you to create a temporary directory or a z/OS data set, that is, one that is deleted when you log off. Because all files in a SAS data library are deleted if the underlying operating environment structure is deleted, the way the operating environment views the SAS data library determines whether the library endures from one session to the next.  [cautionend]


Using a One-Level Name

Typically, when you reference a SAS data set with a one-level name, SAS by default uses the libref WORK for the temporary library. For example, the following program creates a temporary SAS data set named WORK.GRADES:

data grades;
   infile 'file-specification';
   input Name $ 1-14 Gender $ 15-20 Section $ 22-24 Grade;
run;

However, if you want to use a one-level name to reference a permanent SAS data set, you can assign the reserved libref USER. When USER is assigned and you reference a SAS data set with a one-level name, SAS by default uses the libref USER for a permanent SAS data library. For example, the following program creates a permanent SAS data set named USER.GRADES. Note that you assign the libref USER as you do any other libref.

libname user 'SAS-data-library';

data grades;
   infile 'file-specification';
   input Name $ 1-14 Gender $ 15-20 Section $ 22-24 Grade;
run;

Therefore, when you reference a SAS data set with a one-level name, SAS

  1. looks for the libref USER. If it is assigned to a SAS data library, then USER becomes the default libref for one-level names.

  2. uses WORK as the default libref for one-level names if the libref USER has not been assigned.

If USER is assigned, then you must use a two-level name (for example, WORK.TEST) to access a temporary data set in the WORK library. For example, if USER is assigned, then to print the data set WORK.GRADES requires a two-level name in the PROC PRINT statement:

proc print data=work.grades;
run;

If USER is assigned, then you need to make only one change in order to use the same program with files of the same name in different SAS data libraries. Instead of specifying two-level names, simply assign USER differently in each case. For example, the following program concatenates five SAS data sets in SAS-data-library-1 and puts them in a new SAS data set, WEEK, in the same library:

libname user 'SAS-data-library-1';

data week;
   set mon tues wed thurs fri;
run;

By changing just the name of the library in the LIBNAME statement, you can combine files with the same names in another library, SAS-data-library-2:

libname user 'SAS-data-library-2';

data week;
   set mon tues wed thurs fri;
run;

Note:   At your site, the libref USER might be assigned for you when you start a SAS session. Your SAS Support Consultant will know whether the libref is assigned.  [cautionend]


Using a Two-Level Name

You can always reference a SAS data set with a two-level name, whether the libref you use is WORK, USER, or some other libref that you have assigned. Usually, any two-level name with a libref other than WORK references a permanent SAS data set.

In the following program, the LIBNAME statement establishes a connection between the SAS name INTRCHEM and SAS-data-library, which is the physical name for the location of an existing z/OS data set or a directory, for example. The DATA step creates the SAS data set GRADES in the SAS data library INTRCHEM. SAS uses the INPUT statement to construct the data set from the raw data in file-specification.

libname intrchem 'SAS-data-library';

data intrchem.grades;
   infile 'file-specification';
   input Name $ 1-14 Gender $ 15-20 Section $ 22-24 Grade;
run;

When the SAS data set INTRCHEM.GRADES is created, you can read from it by using its two-level name. The following program reads the file INTRCHEM.GRADES and creates a new SAS data set named INTRCHEM.FRIDAY, which is a subset of the original data set:

data intrchem.friday;
   set intrchem.grades;
      if Section='Fri';
run;

The following program displays the SAS data set INTRCHEM.FRIDAY:

proc print data=intrchem.friday;
run;

Previous Page | Next Page | Top of Page