Every SAS file is stored in a SAS library, which is a collection of SAS files such as SAS data sets and catalogues. A SAS library is typically a group of SAS files in the same folder or directory.
Depending on the libref you use, you can store SAS files in a temporary SAS library or in permanent SAS libraries.
You can refer to the SAS website for more information on the SAS system libraries.
What is a SAS Library?
A SAS library is a folder that is located on a user’s disk drive or on the server if you are using SAS on Demand. SAS libraries allow users to safely store the data sets and user-defined formats so that they can be accessed without having to reload or re-read them from an external file every time SAS is started.
SAS datasets/files are stored either temporarily or permanently depending on the library name that you use while creating SAS datasets.
That is, you can use permanent SAS libraries and their contents in consequent SAS sessions.
When you write a DATA statement such as
The datasets are stored in a temporary library if you use the libname WORK, or you don’t specify any libname. In the above example, SAS creates a temporary dataset TEST in the WORK library.
The temporary library and all its files are removed once you close the SAS session.
SAS data set names actually have two-part names in the form:
The part of the name before the period is called a libref (short for library reference), and this tells SAS where to store (or retrieve) the data set.
The part of the name after the period identifies the name you want to give the data set.
Files are stored permanently until you delete it if you use a permanent library when creating a SAS file as in the below example test dataset is stored under mydata Permanent SAS Library.
How to create a permanent library in SAS?
The LIBNAME statement associates the name of the library, or libref, with the physical location of the library.
Rules for Libref
- Should not be more than 8 characters in length
- must be a valid SAS name
- the location must be enclosed in quotation marks
- the statement must end with a semicolon
libname mydata '/folders/9to5sas/';
Now, let’s check the log. The note indicates that libref mydata is successfully assigned.
libname mydata '/folders/9to5sas/';
NOTE: Libref MYDATA was successfully assigned as follows:
Physical Name: /folders/9to5sas/
How to Create Permanent SAS Data Sets?
Temporary SAS files that are created during the session are stored in a special workspace that is assigned the default libref as Work.
If you don’t specify a libref when you create a file (or if you specify Work), the file is stored in the temporary SAS library.
SAS adds the default libref Work, so this DATA statement is equivalent to
To create your own permanent dataset, create your own libref using a LIBNAME statement and use that libref in the two-level SAS data set name.
How to assign Library in SAS?
To reference a SAS file, use a two-level name as libref.filename libref where libref is the name for the SAS library that contains the file and filename is the name of the file.
A period (.) separates the libref and filename. So, to reference temporary SAS files, you can specify the default libref Work, a period, and the filename.
Alternatively, you can simply use a one-level name to reference a file in a temporary SAS library.
Referencing a SAS file in any library except Work indicates that the SAS file is stored permanently.
How to find the physical Path of the SAS Library?
There are a number of tools available to the SAS programmer that can be used to determine the physical path information of any SAS libraries.
Using the PATHNAME function
If you know a libref or fileref, the PATHNAME function can be used to return the physical location of SAS libraries.
%LET LOC = %SYSFUNC(PATHNAME(WORK)); %PUT &LOC.;
The above function would return the physical location of the WORK Library path in the log as below.
Using libname or Proc Options statements
The SAS Work library contains temporary files that are used during a SAS session. To determine the physical location of the SAS Work library within a SAS session, submit one of the following statements:
LIBNAME WORK LIST;
PROC OPTIONS OPTION=WORK; RUN;
Both of the above statements returns some of the useful information along with the physical path in the SAS log.
NOTE: Libref= WORK Scope= IOM ROOT COMP ENV Engine= V9 Access= TEMP Physical Name= /tmp/SAS_work891A000009A3_localhost.localdomain/SAS_work5B7D000009A3_localhost.localdomain Filename= /tmp/SAS_work891A000009A3_localhost.localdomain/SAS_work5B7D000009A3_localhost.localdomain Inode Number= 671536 Access Permission= rwx------ Owner Name= sasdemo File Size= 4KB File Size (bytes)= 4096
Viewing the Contents of SAS Libraries
You can use a PROC CONTENTS step to view the contents of a SAS library. When you specify the keyword ALL in the PROC CONTENTS statement, the step displays a list of all the SAS files that are in the specified SAS library.
PROC CONTENTS DATA=work.ALL NODS; RUN;
How to automatically assign libraries while starting the SAS session?
The association of libref to the specified SAS library will continue throughout your session or until you disassociate them using the CLEAR option.
So, do you have to use the “libname statement” every time you read a permanent dataset with SAS?
Not so, because you can automate this task in SAS which will automatically assign libname when you start your SAS session.
How to automatically assign libraries in SAS Enterprise Guide?
- Open SAS Management Console.
- Go to Data Library Manager, find the library you want to modify, right-click for Properties.
- Go to Options tab – click Advanced Options button – check Library is Pre-Assigned checkbox.
- You should have
METAAUTORESOURCESoption enabled on your application server.
How to automatically create and assign a permanent Library in SAS Studio?
You can create a permanent library in SAS Studio by any of the below methods:
- On the left pane click on LIBRARY.
- Click on the 1st icon which says New Library.
- Once, the New Library windows pop out, enter the Library Name, path and check the “Re-create this library at start-up” option.
Another method to create a permanent library is to edit the autoexec file.
- Select Options > Autoexec file.
- Enter the code that you want to include in the autoexec.sas file.
- To validate your syntax, click Run. The Log tab opens so that you can view the log.
How to remove a library reference in SAS?
In SAS, the libname statement is a very powerful one that can be used to generate a library reference for permanent SAS datasets. However, if you use the libname function with the libref as the only argument, it removes the library reference.
So, if you use the below libname statement, the library MYLIB will be cleared.
To disassociate a libref from a SAS data library, the following forms of the LIBNAME statement can also be used, where libref is the libref of the data library that you want to clear:
LIBNAME libref <CLEAR>;