Working with SAS libraries

Working with SAS libraries

Every SAS file is stored in a SAS library, which is a collection of SAS files such as SAS data sets and catalogs. 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.

The LIBNAME Statement

When you write a DATA statement such as

data test;

SAS creates a temporary SAS data set called Test. When you close your SAS session, this data set disappears.

SAS data set names actually 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), 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.

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

When you end the session, the temporary library and its contents are deleted. To store files permanently in a SAS library, you have to assign it a libref other than the default Work.

data test; 

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

data work.test;

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.

Referencing SAS Files

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.

Working with SAS libraries 1
Two-Level Temporary SAS Library Name

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

Working with SAS libraries 3
One-Level Temporary SAS Library Name

Referencing a SAS file in any library except Work indicates that the SAS file is stored permanently.

Defining SAS Libraries

There are SAS statements to assign library names.

  1. Assign a name (libref) to the SAS library in which the file is stored
  2. Use the libref as the first part of the file’s two-level name (libref.filename) to reference the file within the library.

The LIBNAME statement starts with the LIBNAME keyword and then specifies the name of the library (called a libref), followed by the directory or folder where you want to store your permanent SAS data sets.

The libref you use must not be more than 8 characters in length and must be a valid SAS name.

LIBNAME MYLIB '/folders/myfolders';
 data mylib.class;
     set sashelp.class;
run;

SAS Libraries

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.

/tmp/SAS_work891A000009A3_localhost.localdomain/SAS_work5B7D000009A3_localhost.localdomain

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;
Proc Contents
The output of the Proc Content Procedure

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 METAAUTORESOURCES option 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.
Working with SAS libraries 5
  • Once, the New Library windows pop out, enter the Library Name, path and check the “Re-create this library at start-up” option.
Working with SAS libraries 7

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

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

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.

LIBNAME MYLIB;

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

About the Author - Subhro Kar

Been in the realm with the professionals of the IT industry. I am passionate about Coding, Blogging, Web Designing and deliver creative and useful content for a wide array of audience.

Leave a Comment

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