Chapter Contents

Previous

Next
SAS Companion for UNIX Environments

Assigning a Libref to Several Directories (Concatenating Directories)

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

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

libname income ('revenue','costs');
This statement indicates that the two directories, revenue and costs, are to be treated as a single SAS data library.

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

libname income ('corpsale','retail'); 
libname costs ('salaries','expenses'); 
libname profits (income,'capgain',costs);
This statement indicates that the five directories, corpsale, retail, salaries, expenses, and capgain, are to be treated as a single SAS data library.

When you concatenate SAS data libraries, the SAS System 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.) See Understanding How Concatenated SAS Data Libraries Are Accessed for more information.

See SAS Language Reference: Dictionary for complete documentation on the LIBNAME statement.


Understanding How Concatenated SAS Data Libraries Are Accessed

When you use the concatenation feature to specify more than one physical directory for a libref, the SAS System uses the protocol shown 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, the SAS System 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. For example, suppose you submit the following statements and TEST.SPECIES originally exists only in the second directory, mysasdir:

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, the SAS System 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 ( mysasdir) directory, 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.


Chapter Contents

Previous

Next

Top of Page

Copyright 1999 by SAS Institute Inc., Cary, NC, USA. All rights reserved.