Previous Page | Next Page

Using SAS Files

Assigning a Libref to Several Directories (Concatenating Directories)


Introduction to Concatenating Directories

You can use the LIBNAME statement to assign librefs and engines to one or more directories, including the Work directory.

If you have SAS data sets located in multiple directories, you can treat these directories as a single SAS library by specifying a single libref and concatenating the directory locations, as in the following example:

libname income ('/u/2007/revenue', '/u/2007/costs');

This statement indicates that the two directories, /u/2007/revenue and /u/2007/costs, are to be treated as a single SAS library.

If you have already assigned librefs to your SAS libraries, you can use these librefs to indicate that you want to concatenate the libraries, as in this example:

libname income ('/u/2007/corpsale', '/u/2007/retail'); 
libname costs ('/u/2007/salaries', '/u/2007/expenses'); 
libname profits (income, costs, '/u/2007/capgain');

This statement indicates that the five directories, /u/2007/corpsale, /u/2007/retail, /u/2007/salaries, /u/2007/expenses, and /u/2007/capgain, are to be treated as a single SAS library.


How SAS Accesses Concatenated Libraries

When you concatenate SAS libraries, SAS uses a protocol for accessing the libraries, which depends on whether you are accessing the libraries for read, write, or update. (A protocol is a set of rules.)

SAS uses the protocol in the following sections to determine which directory is accessed. (The protocol illustrated by these examples applies to all SAS statements and procedures that access SAS files, such as the DATA, UPDATE, and MODIFY statements in the DATA step, and the SQL and APPEND procedures.)


Accessing Files for Input and Update

When a SAS data set is accessed for input or update, the first SAS data set that is found by that name is the one that is accessed. For example, if you submit the following statements and the data set old.species exists in both directories, the one in the mysasdir directory is the one that is printed:

libname old ('mysasdir','saslib'); 
proc print data=old.species; 
run;

The same would be true if you opened old.species for update with the FSEDIT procedure.


Accessing Files for Output

If the data set is accessed for output, it is always written to the first directory, provided that the directory exists. If the directory does not exist, an error message is displayed. For example, if you submit the following statements, SAS writes the old.species data set to the first directory (mysasdir), and replaces any existing data set with the same name:

libname old ('mysasdir','saslib');  
data old.species;
x=1;
y=2;
run;

If a copy of the old.species data set exists in the second directory, it is not replaced.


Accessing Data Sets with the Same Name

If you use the DATA and SET statements to access data sets with the same name, the DATA statement uses the output rules and the SET statement uses the input rules. When you execute the following statements, assume that test.species originally exists only in the second directory, mysasdir. Execute the following statements:

libname test ('sas','mysasdir');
data test.species;
set test.species;
if value1='y' then
   value2=3;
run;

The DATA statement opens test.species for output according to the output rules; that is, SAS opens a data set in the first of the concatenated libraries (sas). The SET statement opens the existing test.species data set in the second directory (mysasdir), according to the input rules. Therefore, the original test.species data set is not updated. After the DATA step executes, two test.species data sets exist, one in each directory.

Previous Page | Next Page | Top of Page