Depending on the libref you use, you can store SAS files in a temporary SAS library or permanent SAS libraries.
What is a SAS Library?
A SAS library is a folder located on a user’s disk drive or the server if you are using SAS on Demand. SAS libraries allow users to safely store the data sets and user-defined formats to be accessed without reloading or re-reading them from an external file every time SAS is started.
SAS datasets/files are stored temporarily or permanently depending on the library name 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
By default, datasets are stored in the WORK library if you don’t specify any libname.SAS creates a temporary dataset TEST in the WORK library in the above example. Once you close the SAS session, the temporary library and all its files are removed.
SAS data set names has 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.
After the period, the part of the name identifies the name you want to give the data set.
How to create a permanent library in SAS?
The LIBNAME statement associates the name of the library, or libref, with the library’s physical location.
To create a permanent SAS library, use the LIBNAME keyword and then specify the library’s name (called a libref), followed by the directory or folder where you want to store your permanent SAS data sets.
Rules for Libref
- Should not be more than eight characters in length
- must be a valid SAS name
- the location must be enclosed in quotation marks
- the statement must end with a semicolon
The note in the log indicates that libref mydata is successfully assigned.
How to Create Permanent SAS Data Sets?
Temporary SAS files created during the session are stored in a particular workspace that is assigned the default libref as Work.
The temporary library and its contents are deleted when you end the session. Therefore, to store files permanently in a SAS library, you have to assign it a libref other than the default Work.
How to Create Permanent SAS Data Sets?
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.
SAS adds the default libref Work, so this DATA statement is equivalent toSAS adds the default libref Work, so this DATA statement is equivalent toSAS adds the default libref Work, so this DATA statement is equivalent to:
To create your permanent dataset, create your libref using a LIBNAME statement and use that libref in the two-level SAS data set name.
How to assign Library in SAS?
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.
How to find the physical Path of the SAS Library?
There are several options available that you can use to determine the physical path information of any SAS library.
Using the PATHNAME function
IIf you know a libref or fileref, you can use the PATHNAME function 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 coThe 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;
The Proc option procedure lists the current settings of SAS system options in the SAS log.
PROC OPTIONS OPTION=WORK; RUN;
Both of the above statements return some helpful 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. For example, when you specify the keyword ALL in the PROC CONTENTS statement, the step displays a list of all the SAS files in the specified SAS library.
PROC CONTENTS DATA=work.ALL NODS; RUN;
How to pre-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.
You can also automate assigning libname in SAS.
How to automatically assign libraries in SAS Enterprise Guide?
From the top Menu bar: Tools -> Options ->SAS Programs.
Check the box next to Submit SAS code when the server is connected.
Go to Edit next to Submit SAS code when the server is connected and finally, add libname statement libname lib ‘directory’;
How to automatically create and pre-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 compelling and can be used to generate a library reference for permanent SAS datasets.
However, using the libname function with the libref as the only argument 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, you can also use the following forms of the LIBNAME statement, where libref is the libref of the data library that you want to clear:
LIBNAME libref <clear>;
We hope this article helped you to create permanent SAS Libraries. You may also want to see our article on how to import data in SAS.