Did you know that you can concatenate multiple directories
together in a LIBNAME statement?
Most programs have a 1-to-1 correlation between a LIBNAME statement
and a SAS data library. However, it
doesn’t need to be that way in real life!
You can have multiple directories specified in a LIBNAME statement. Consider this example:
libname ctemp ("c:\temp",
"c:\temp\usertemp");
data class;
set ctemp.class(where=(sex="M"));
run;
proc sort
data=class out=ctemp.sortedmales;
by
descending age;
run;
libname
ctemp list;
In the example, we are concatenating two directories
(c:\temp and c:\temp\usertemp) together to form the CTEMP SAS data
library. When you use “CTEMP” as the
first level of a two-level SAS data set name, SAS will search first c:\temp,
and then c:\temp\usertemp for that SAS data set. If there is a SAS data set with the same name
in both libraries, SAS uses the first one it comes across.
Check out the log from this program:
1
libname ctemp ("c:\temp", "c:\temp\usertemp");
NOTE: Libref CTEMP was successfully
assigned as follows:
Levels: 2
Engine(1): V9
Physical Name(1): c:\temp
Engine(2): V9
Physical Name(2): c:\temp\usertemp
2
3
data class;
4
set
ctemp.class(where=(sex="M"));
5
6
run;
The note highlighted in yellow, above shows that SAS recognizes the
concatenation of the two directories to form one SAS data library.
NOTE: There were 10 observations read
from the data set CTEMP.CLASS.
WHERE sex='M';
NOTE: The data set WORK.CLASS has 10 observations and 5 variables.
NOTE: DATA statement used (Total process
time):
real time 0.01 seconds
cpu time 0.01 seconds
The note highlighted in yellow, above shows that SAS read the CLASS data
set from the CTEMP directory. In fact,
CLASS is located in c:\temp\usertemp, which is the second directory in the
concatenation.
7
8
proc sort data=class out=ctemp.sortedmales;
9
by descending age;
10
run;
NOTE: There were 10 observations read
from the data set WORK.CLASS.
NOTE: SAS sort was used.
NOTE: The data set CTEMP.SORTEDMALES
has 10 observations and 5 variables.
NOTE: PROCEDURE SORT used (Total process
time):
real time 0.01 seconds
cpu time 0.01 seconds
The note highlighted in yellow, above shows that SAS stored the
SORTEDMALES data set in the CTEMP directory.
By default, SAS stores all new data sets in c:\temp, which is the first
directory in the concatenation.
11
12
libname ctemp list;
NOTE: Libref= CTEMP
Scope= DMS Process
Levels= 2
-Level 1-
Engine= V9
Physical Name= c:\temp
Filename= c:\temp
-Level 2-
Engine= V9
Physical Name= c:\temp\usertemp
Filename= c:\temp\usertemp
In the example above, the LIST option on the LIBNAME
statement is used to list the attributes of the CTEMP libref; including the
directories that are concatenated together.
As long as you understand the rules of the road, (SAS
processes the first data set it finds with a specific name when traversing the
concatenated files; and SAS writes new data sets to the first directory in the
concatenation) concatenating directories together for a single SAS data library
can be very useful!
The hack above is an excerpt from the book: Did You Know That? Essential Hacks for Clever SAS Programmers
https://tinyurl.com/y83aleb4
Best of luck in all your SAS endeavors!
---MMMMIIIIKKKKEEEE
(aka Michael A. Raithel)
Author of the new cult classic for computer programmers: It Only Hurts When I Hit <ENTER>
Print edition: http://tinyurl.com/z8bzx2e
Kindle edition: http://tinyurl.com/zypgqa7
Print edition: http://tinyurl.com/z8bzx2e
Kindle edition: http://tinyurl.com/zypgqa7
No comments:
Post a Comment