Working with SAS libraries

SAS files are stored in a SAS librarySAS files include 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 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

data test

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:

libref.data-set-name

The part of the name before the period is called a libref (short for library reference), which 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.

Files are stored permanently until you delete them. For example, the test dataset below is held under the mydata Permanent SAS Library when creating a SAS file.

data mydata.test;

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. 

Creating a library in SAS
Creating a library in SAS

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 file is stored in the temporary SAS library if you don’t specify a libref when creating a file (or specify Work).

The temporary library and its contents are deleted when you end the session. Therefore, to store files permanently in a SAS library, you must assign it a libref other than the default Work.

How to Create Permanent SAS Data Sets?

To create your permanent dataset, create your libref using a LIBNAME statement and use that libref in the two-level SAS data set name.

data test; 

SAS adds the default libref Work, so this DATA statement is equivalent to SAS adding the default libref Work.

data work.test;
creating permanent sas dataset

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.

Working with SAS libraries
Two-Level Temporary SAS Library Name

Alternatively, you can use a one-level name to reference a file in a temporary SAS library.

Working with SAS libraries
One-Level Temporary SAS Library Name

Referencing a SAReferencing 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 several options available that you can use to determine the physical path information of any SAS library.

Using the PATHNAME function

If 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.

/tmp/SAS_work891A000009A3_localhost.localdomain/SAS_work5B7D000009A3_localhost.localdomain

Using libname or Proc Options statements

The SAS Work library contains temporary files 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;
Proc Contents
                                                                  The output of the Proc Content Procedure

How do 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.
Working with SAS libraries
  • Once the New Library windows pop out, enter the Library Name, and path and check the “Re-create this library at start-up” option.
pre-assign a permanent Library in SAS Studio
pre-assign a permanent Library in SAS Studio

Another method to create a permanent library is to edit the autoexec file.

  • Select Options > Autoexec file.
Working with SAS libraries
Edit 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.
Working with SAS libraries

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.

LIBNAME MYLIB;

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.

Subscribe to our mailing list for weekly updates if you like this article. You can also find us on Instagram and Facebook.

Every week we'll send you SAS tips and in-depth tutorials

JOIN OUR COMMUNITY OF SAS Programmers!

Subhro Kar is an Analyst with over five years of experience. As a programmer specializing in SAS (Statistical Analysis System), Subhro also offers tutorials and guides on how to approach the coding language. His website, 9to5sas, offers students and new programmers useful easy-to-grasp resources to help them understand the fundamentals of SAS. Through this website, he shares his passion for programming while giving back to up-and-coming programmers in the field. Subhro’s mission is to offer quality tips, tricks, and lessons that give SAS beginners the skills they need to succeed.