Chapter Contents |
Previous |
Next |
SAS Companion for the Microsoft Windows Environment |
There are several ways to specify a libref:
Note: You can eliminate the LIBNAME statement by directly specifying the drive name and the library name within quotes. An example follows:
data "d:\a";
Accessing the New Library Dialog Box Using the Graphical-User Interface |
To open the New Library dialog box using the toolbar, click on the New Library icon which looks like a file cabinet.
To open the New Library dialog box using a command, type libassign in the command box.
To open the New Library dialog box using Explorer:
Note: When a second Explorer window is
open on the right side of the SAS workspace, you can by-pass the New... dialog
box to open the New Library dialog box if you right-mouse click on the Libraries folder and select
New....
For more information on the New Library window and Explorer, see the SAS System Help.
Assigning SAS Libraries Using the LIBNAME Statement or Function |
The LIBNAME statement has the following basic syntax:
LIBNAME libref <engine-name> 'SAS-data-library' |
An explanation of all the arguments in this statement can be found in LIBNAME and SAS Language Reference: Dictionary.
You can also use the LIBNAME function within your SAS programs to assign librefs. For more information about the LIBNAME function, see SAS Language Reference: Dictionary.
libname test V8 'c:\mysasdir';This statement indicates that Version 8 SAS files stored in the folder C:\MYSASDIR are to be accessed using the libref TEST. Remember that the engine specification is optional.
libname mycurr '.';
libname income ('c:\revenue','d:\costs');This statement indicates that the two folders, C:\REVENUE and D:\COSTS, are to be treated as a single SAS data library. When you concatenate SAS data libraries, the SAS System uses a protocol (a set of rules) for accessing the libraries, depending on whether you are accessing the libraries for read, write, or update.
Furthermore, you may concatenate multiple libraries by specifying only their librefs, as in the following example:
libname sales (income revenue);This statement indicates that two libraries that are identified by librefs INCOME and REVENUE are treated as a single SAS data library whose libref is SALES.
For more information, see Understanding How Concatenated SAS Data Libraries Are Accessed and SAS Language Reference: Dictionary.
Note: The concept of library concatenation also applies
when specifying system options, such as the SASHELP and SASMSG options. For
information on how to specify multiple folders in options such as these, see Syntax for Concatenating Libraries in SAS System Options.
libname oldlib V6 'c:\sas610';
As another example, if you plan to share SAS files between Version 8 under Windows and Release 6 under Windows, you should use the V6 engine when assigning a libref to the SAS data library. Here is an example of specifying the V6 engine in a LIBNAME statement:
libname lib6 V6 'c:\sas6';
The V6 engine is particularly useful in your Version 8 SAS session if you are going to be accessing the same SAS files from a Release 6 SAS session. Remember that while Version 8 can read Release 6 SAS data sets, Release 6 cannot read Version 8 SAS data sets.
For more information about using engine names in the
LIBNAME statement, see Using SAS Files from Other Releases with Version 8 for Windows
and Reading BMDP, OSIRIS and SPSS Files.
You can also refer to the LIBNAME statement in SAS Language Reference: Dictionary.
libname test 'c:\mysasdir'; libname mylib ('c:\mydata','d:\tempdata'); libname oldlib V6 'c:\sas6';
For more information about how to create and use a SAS
autoexec file, see SAS Autoexec File.
libname one V6 'c:\mydir'; libname two V8 'c:\mydir';
Data sets referenced by the libref ONE are created and accessed using the compatibility engine (V6), whereas data sets referenced by the libref TWO are created and accessed using the default engine (V8). You can also have multiple librefs (using the same engine) for the same SAS data library. For example, the following two LIBNAME statements assign the librefs MYLIB and INLIB (both using the V8 engine) to the same SAS data library:
libname mylib V8 'c:\mydir\datasets'; libname inlib V8 'c:\mydir\datasets';Because the engine name and SAS data library specifications are the same, the librefs MYLIB and INLIB are identical and can be used interchangeably.
Assigning SAS Libraries Using Environment Variables |
The availability of environment variables makes it simple
to assign resources to the SAS System prior to invocation.
-set test c:\sas\mysasdata
When you assign an environment variable, the SAS System does not resolve the environment reference until the environment variable name is actually used. For example, if the TEST environment variable is defined in your SAS configuration file, the environment variable TEST is not resolved until it is referenced by the SAS System. Therefore, if you make a mistake in your SET option specification, such as miss-typing a folder name, you do not receive an error message until you use the environment variable in a SAS statement.
Because Windows filenames can contain spaces or single quotes as part of their names, you should enclose the name of the physical path in double quotes when specifying the SET option. If you use the SET option in an OPTIONS statement, you must use quotation marks around the filename. For complete syntax of the SET system option, see SET.
Any environment variable name you use with a system option in your SAS configuration file must be defined as an environment variable before it is used. For example, the following SET option must appear before the SASUSER option that uses the environment variable TEST:
-set test "d:\mysasdir" -sasuser !test
In the following example, environment variables are used with concatenated libraries:
-set dir1 "c:\sas\base\sashelp" -set dir2 "d:\sas\stat\sashelp" -sashelp (!dir1 !dir2)
Note that when you reference environment variables in your SAS configuration file or in a LIBNAME statement in your SAS programs, you must precede the environment variable name with an exclamation point (!).
It is recommended that you use the SET system option
in your SAS configuration file if you invoke the SAS System through a Windows
shortcut.
You can execute a Windows SET command prior to invoking the SAS System to
create a Windows environment variable. You must define the environment variable
prior to invoking the SAS System; you cannot define environment variables
for SAS System use from a command prompt window from within a SAS session.
Operating Environment Information for Windows 95 and Windows NT Users:
If you define an environment variable in a Command Prompt window, and then start SAS from the Start menu (or another shortcut), SAS will not recognize the environment variable.
The environment variables you define with the SET command can be used later within the SAS System as librefs. In the following example, the Windows SET command is used to define the environment variables PERM and BUDGET:
SET PERM="C:\MYSASDIR" SET BUDGET="D:\SAS\BUDGET\DATA"
When you reference an external environment variable (one assigned with the Windows SET command) in your SAS programs (such as in a DATA or MERGE statement or in a SAS command), a note informing you the environment variable has been used is written to the SAS log. SAS does not recognize the environment variable as a libref until after you use it at least once during your SAS session, so the library name does not appear as a node in the SAS Explorer window until then.
Listing Libref Assignments |
If you are running the SAS System interactively, use the SAS Explorer window to view the active librefs. The SAS Explorer window lists all the librefs active for your current SAS session, along with the engine and the physical path for each libref. Any environment variables you have defined as librefs are listed, provided you have used them in your SAS session. If you have defined an environment variable as a libref but have not used it yet in a SAS program, the SAS Explorer window does not list it.
Note: You can use the LIBNAME command to invoke an 'Active
Libraries' window which is a contents-only SAS Explorer that lists the active
libraries, providing you with a quick view of your current libraries.
You can use the following LIBNAME statement to write the active librefs to the SAS log:
libname _all_ list;
Clearing Librefs |
You can clear a libref by using either the SAS Explorer window or a LIBNAME statement.
To clear a libref by using the SAS Explorer window, simply right-click on the node for the libref that you want to clear and select Delete. (For more information about using the SAS Explorer window to manage libraries, see The Little SAS Book.)
To clear a libref by using the LIBNAME statement, submit a LIBNAME statement using this syntax:
LIBNAME libref|_all_ <clear>; |
If you specify a libref, only that libref is cleared. If you specify the keyword _all_, all the librefs you have assigned during your current SAS session are cleared. (SASUSER, SASHELP, and WORK remain assigned.)
Note: When you clear a libref defined by an environment
variable, the variable remains defined, but it is no longer considered a libref--that
is, it is not listed in the SAS Explorer window. You can use the variable
in another LIBNAME statement to create a new libref.
The SAS System automatically clears the association between librefs and their respective data libraries at the end of your job or session. If you want to associate the libref with a different SAS data library during the current session, you do not have to end the session or clear the libref. The SAS System automatically reassigns the libref when you use it to name a new library from within either the SAS Explorer window or a LIBNAME statement.
Understanding How Concatenated SAS Data Libraries Are Accessed |
libname old ('c:\mysasdir','d:\saslib'); proc print data=old.species; run;The same would be true if you opened OLD.SPECIES for update with the FSEDIT procedure.
libname old ('c:\mysasdir','d:\saslib'); data old.species; x=1; y=2; run;
If a copy of the OLD.SPECIES data set exists in the
second folder, it is not replaced.
libname test ('c:\sas','d:\mysasdir'); data test.species; set test.species; if value1='y' then value2=3; run;
In this case, 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 (C:\SAS). The SET statement opens the existing TEST.SPECIES data set in the second (D:\MYSASDIR) folder, according to the input rules. Therefore, the original TEST.SPECIES data set is not updated; rather, two TEST.SPECIES data sets exist, one in each folder.
Using the SASUSER Data Library |
The SAS System automatically creates a SAS data library with the libref SASUSER. This library contains, among other SAS files, your user profile catalog. By default under Windows, the SASUSER libref points to a folder called "My SAS Files\V8", located under the working folder of your current SAS session. If a SASUSER folder does not exist, the SAS System creates one. You can use the SASUSER system option to make the SASUSER libref point to a different SAS data library. For more information about your profile catalog, see Profile Catalog. For more information about the SASUSER system option, see SASUSER. The SAS System stores other files besides the profile catalog in the SASUSER folder. For example, the sample data sets provided with SAS/ASSIST software are stored in this folder.
Using the WORK Data Library |
The WORK data library is the storage place for temporary SAS files. By default under Windows, the WORK data library is created as a subfolder of the SASWORK folder. This subfolder is named #TDnnnnn, as discussed in WORK Data Library. Temporary SAS files are available only for the duration of the SAS session in which they are created. At the end of that session, they are deleted automatically. By default, any file that is not assigned a two-level name is automatically considered to be a temporary file. A special libref of WORK is automatically assigned to any temporary SAS data sets created. For example, if you run the following SAS DATA step to create the data set SPORTS, a temporary data set named WORK.SPORTS is created:
data sports; input @1 sport $10. @12 event $20.; cards; volleyball co-recreational swimming 100-meter freestyle soccer team ;
If you display the SAS Explorer window now, you will see the SPORTS data set in the WORK folder.
You can display all the temporary data sets that are created during this session from the SAS Explorer window by double-clicking the Libraries folder icon and then double-clicking the Work folder icon. From the Active Libraries (LIBNAME) window, double-click the Work folder icon to see the temporary data sets.
-set myvar c:\ tempdir -work !myvar use
The SET option associates the MYVAR environment variable with the C:\TEMPDIR folder. Then the WORK option tells the SAS System to use that folder for the SAS WORK data library. The USE suboption in the WORK option tells the SAS System to store temporary SAS files in the folder specified by the WORK option. If you do not specify the USE suboption, by default the SAS System creates a subfolder beneath the specified folder. This subfolder is named #TDnnnnn, where nnnnn is a unique number associated with your SAS session. When you exit your SAS session, these folders and any files they contain are removed.
Note: If you specify the USE suboption in the default configuration file, you will not be able to run multiple SAS sessions on the same machine, since each SAS session requires a unique WORK folder.
Also, do not assign an environment variable named TEMP
or TMP, as these variable names are usually already used by Windows.
libname user 'c:\mysasdir';
When you set the USER libref to a folder as in the previous example, if you want to create or access a temporary data set you must specify a two-level name for the data set with WORK as the libref. You can also assign the USER libref when you invoke the SAS System by using the USER system option. For more information about the USER system option, refer to USER and SAS Language Reference: Dictionary.
Note: You can assign other engines to the USER libref
if you want the data sets that are saved with one-level names to be stored
in a format for use with other releases of the SAS System.
Chapter Contents |
Previous |
Next |
Top of Page |
Copyright 1999 by SAS Institute Inc., Cary, NC, USA. All rights reserved.