Using SAS Files |
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. See Assigning a Libref to Several Directories (Concatenating Directories) for more information.
Assigning Librefs |
You can use any of the following methods to assign a SAS libref:
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.
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.
The LIBNAME function takes the same arguments and options as the LIBNAME statement. For more information about the LIBNAME function, see SAS Language Reference: Dictionary.
Perform the following steps to assign a libref using the DMLIBASSIGN command:
Issue the DMLIBASSIGN command in the command window.
The New Library dialog box opens.
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.
Click Enable at startup to assign this libref when you invoke SAS.
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.
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.
Perform the following steps to assign a libref from the LIBNAME window:
Issue the LIBNAME command in the command window.
The LIBNAME window opens.
From the File menu, select New.
The New Library dialog box opens.
Complete the fields in the New Library dialog box as described in Use the DMLIBASSIGN Command.
Perform the following steps to assign a libref from the SAS Explorer window:
From the File menu, select New when the Libraries node in the tree structure is active.
The New dialog box opens.
Select Library, and then click OK.
The New Library dialog box opens.
Complete the fields in the New Library dialog box as described in Use the DMLIBASSIGN Command.
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 the LIBNAME function in SAS Language Reference: Dictionary or the 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 |
as the value of the USER= option. (See Using One-Level Names to Access Permanent Files (User Library) for information.)
libname sales '/users/myid/mydir'; data sales.final;
Copyright © 2010 by SAS Institute Inc., Cary, NC, USA. All rights reserved.