Referring to SAS Data Files 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 data 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 a nickname that you can use to refer to the data library during a SAS session or job. You will probably want to use a libref when:

  • 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. See "Assigning a Libref to Several Directories (Concatenating Directories)" on page 115 for more information.

Librefs can be stored in the SAS registry. See "Customizing Your SAS Registry Files" on page 16 for more information.

Assigning Librefs

You can use any of the following methods to assign a SAS libref:

  • the LIBNAME statement

  • the LIBNAME function

  • the LIBASSIGN command

  • the LIBNAME window

  • the SAS Explorer window.

A libref assignment remains in effect for the duration of the SAS job, session, or process unless you either 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 data library to SAS, associates an engine with the library, enables you to specify options for the library, and assigns a libref to it. For details about LIBNAME statement syntax, see "LIBNAME Statement" on page 301.

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" on page 250.

Using the LIBASSIGN Command

Perform the following steps to assign a libref using the LIBASSIGN command:

  1. Issue the LIBASSIGN command in the command window. The New Library dialog box opens.

  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 drop-down menu. Depending on the engine that you specify, the fields in the Library Information area might change.

  4. Click the Enable at startup box to assign this libref when you invoke SAS.

  5. Specify the necessary information for the desired SAS data library in the Library Information area. Depending on the engine selected, there may or may 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 opens.

  2. From the File pull-down menu, select New . The New Library dialog box opens.

  3. Fill in the fields in the New Library dialog box, described in "Using the LIBASSIGN Command" on page 112.

  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 pull-down menu, select New when the Libraries node in the tree structure is active. The New dialog box opens.

  2. Select Library , and then click OK . The New Library dialog box opens.

  3. Fill in the fields in the New Library dialog box, described in "Using the LIBASSIGN Command" on page 112.

  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" on page 250 or "LIBNAME Statement" on page 301

  • Select the Enable at startup box when you assign a libref using the LIBASSIGN command, LIBNAME window, or Explorer Window. This will save the libref in the SAS Registry. For more information about these methods, see "Assigning Librefs" on page 112.

  • Use environment variables as librefs. Include these environment variables in your startup files so that these variables are set when SAS is invoked.

Accessing a Permanent SAS Data Library using a Libref

After you have defined a libref, you can use the libref in one of two ways to access a permanent SAS data 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 specifies the name of the file being read or created.

  • as the value of the USER= option. (See "Using One-Level Names To Access Permanent Files ( User Data Library)" on page 120 for details.)

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; 



SAS 9.1 Companion for UNIX Environments
SAS 9.1 Companion For Unix Enivronments
ISBN: 1590472101
EAN: 2147483647
Year: 2004
Pages: 185
Authors: SAS Institute

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net