SAS libraries

Working with SAS libraries

  • Post author:
  • Post category:Base SAS
  • Post comments:0 Comments
  • Reading time:16 mins read

SAS files are stored in a SAS library. SAS 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 library.

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 have 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

To create a library, you need to submit a LIBNAME statement only once during a SAS session. The libref remains in effect for the duration of the session.

A LIBNAME statement consists of

  • the keyword LIBNAME.
  • the libref name that you want to use.
  • the pathname that represents the physical location of the library.
  • a semicolon.

The following LIBNAME statement assigns the mylib libref to the specified folder. The directory that you associate with the libref must already exist before you can assign it to the libref.

libname mylib '/home/subhroster20070/9to5sas';
 NOTE: Libref MYLIB refers to the same physical library as MYPUBLIC.
 NOTE: Libref MYLIB was successfully assigned as follows: 
       Engine:        V9 
       Physical Name: /home/subhroster20070/9to5sas

The note in the log indicates that libref mylib 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 you 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.

automatically assign libraries in SAS Enterprise Guide
automatically assign libraries in SAS Enterprise Guide

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

Subhro provides valuable and informative content on SAS, offering a comprehensive understanding of SAS concepts. We have been creating SAS tutorials since 2019, and 9to5sas has become one of the leading free SAS resources available on the internet.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.