Referring to SAS Files by Using Librefs in UNIX Environments

Techniques for Referring to a SAS File

If you want to read or write to a permanent SAS file, you can refer to the SAS file in one of two ways:
  • Refer to the data file directly by using its pathname in the appropriate statements (such as DATA, SET, MERGE, UPDATE, OUTPUT, and PROC).
  • Assign a libref to the SAS library (directory) that contains the data file and use the libref as the first level of a two-level filename.

What Is a Libref?

A libref is an alias that you can use to refer to the library during a SAS session or job. You will probably want to use a libref when one of the following is true:
  • The data file pathname is long and must be specified several times within a program.
  • The pathname might change. If the pathname changes, you need to change only the statement assigning the libref, not every reference to the file.
  • Your application will be used on other platforms. Using librefs makes it easier to port an application to other operating environments.
  • You need to concatenate libraries. For more information, see Assigning a Libref to Several Directories (Concatenating Directories).
Librefs can be stored in the SAS registry. For more information, see Customizing Your SAS Registry Files.

Assigning Librefs

Methods for Assigning Librefs

You can use any of the following items to assign a SAS libref:
  • LIBNAME statement
  • LIBNAME function
  • DMLIBASSIGN command
  • LIBNAME window
  • SAS Explorer window
A libref assignment remains in effect for the duration of the SAS job, session, or process unless you clear the libref or use the same libref in another LIBNAME statement or LIBNAME function.
If you assign a libref from a SAS process, that libref is valid only within that SAS process. If you clear a libref from within a SAS process, that libref is not cleared from other SAS processes.

Using the LIBNAME Statement

The LIBNAME statement identifies a SAS library to SAS, associates an engine with the library, enables you to specify options for the library, and assigns a libref to it. For information about LIBNAME statement syntax, see LIBNAME Statement: UNIX.

Using the LIBNAME Function

The LIBNAME function takes the same arguments and options as the LIBNAME statement. For more information about the LIBNAME function, see LIBNAME Function in SAS Functions and CALL Routines: Reference.

Using the DMLIBASSIGN Command

Perform the following steps to assign a libref using the DMLIBASSIGN command:
  1. Issue the DMLIBASSIGN command in the command window.
    The New Library dialog box appears.
  2. Specify the libref in the Name field.
  3. Specify an engine for the libref in the Engine field by selecting the default engine or another engine from the menu. Depending on the engine that you select, the fields in the Library Information area might change.
  4. Click Enable at startup to assign this libref when you invoke SAS.
  5. Specify the necessary information for the SAS library in the Library Information area. Depending on the engine that you select, there might not be a Path field available for input.
  6. Specify LIBNAME options in the Options field. These options can be specific to your host or engine, including options that are specific to a SAS engine that accesses another software vendor's relational database system.
  7. Click OK.

Using the LIBNAME Window

Perform the following steps to assign a libref from the LIBNAME window:
  1. Issue the LIBNAME command in the command window.
    The LIBNAME window appears.
  2. From the File menu, select New.
    The New Library dialog box appears.
  3. Complete the fields in the New Library dialog box as described in Using the DMLIBASSIGN Command.
  4. Click OK.

Using the SAS Explorer Window

Perform the following steps to assign a libref from the SAS Explorer window:
  1. From the File menu, select New when the Libraries node in the tree structure is active.
    The New dialog box appears.
  2. Select Library, and then click OK.
    The New Library dialog box appears.
  3. Complete the fields in the New Library dialog box as described in Using the DMLIBASSIGN Command.
  4. Click OK.

Permanently Assigning a Libref

You might want to save a libref so that it is valid between SAS sessions. You can assign a libref permanently by using one of the following methods:
  • Specify the LIBNAME statement or LIBNAME function in an autoexec file. For more information, see LIBNAME Function in SAS Functions and CALL Routines: Reference or LIBNAME Statement: UNIX.
  • Select Enable at startup when you assign a libref using the DMLIBASSIGN command, LIBNAME window, or SAS Explorer window. Selecting this option will save the libref in the SAS registry. For more information about these methods, see Assigning Librefs.
  • Use environment variables as librefs. Include these environment variables in your start-up files so that these variables are set when SAS is invoked.

Accessing a Permanent SAS Library by Using a Libref

After you have defined a libref, you can use the libref in one of two ways to access a permanent SAS library:
  • as the first level of a two-level SAS filename:
    libref.member-name
    where libref is the first-level name referring to the directory where the file is stored, and member-name is the name of the file being read or created.
  • as the value of the USER= option. (For more information, see Using One-Level Names to Access Permanent Files (User Library).)
For example, these SAS statements access the data file Final.sas7bdat in the Sales library that is stored in the /users/myid/mydir directory:
libname sales '/users/myid/mydir';
data sales.final;