Skip to main content

SAS Tutorials: SAS Libraries

This SAS software tutorial describes what SAS libraries are used for, and how to create and access your own libraries.

SAS Libraries

A SAS library is a folder located on a user's disk drive or on the internet that is specially designated for use by SAS. SAS libraries allow users to safely store things like 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.

If the data you want to work with is already a SAS dataset stored somewhere on your computer, then all you need to do to start working with your data is assign a library name to the location of the dataset. If it is stored as a file other than SAS (Excel, SPSS, text, etc.) then you’ll need to help SAS read it with one of the methods described in the next sections of this tutorial. Alternatively, you can create your own data in SAS, which is also covered below.

One or many datasets can be assigned to the same library. A library is assigned a location with a LIBNAME statement in SAS. A LIBNAME statement is one of those global statements that we mentioned in Section 1 that does not occur in a data step or a proc step. The format of the LIBNAME statement is:

    LIBNAME libref 'Folder path name here';

Here the first word (LIBNAME) is the SAS keyword that tells it to create a library. The second word (libref) is what you name the library. It must be eight or less characters and start with a letter. Finally, the text in quotes is what path name you tell SAS to assign the library to. (This folder must already exist on your computer.) Here is an example LIBNAME statement that creates the library named practice and assigns it to a folder in the directory ‘C:\Users\Research\Thesis.’

    LIBNAME practice 'C:\Users\Research\Thesis';

Most SAS users put their LIBNAME statements at the beginning of the program, but you can put a LIBNAME statement anywhere in the program as long as it comes before you reference a file in that location. You will get an error message if you try to refer to a dataset that has not yet been pointed to in a LIBNAME statement. Once a library has been assigned to a location with a SAS dataset, the dataset can be referred to in statements using two parts:

libref.SAS-dataset-name

The first part of the name (before the period) is the dataset’s library assignment. The second part of the name (after the period) is the dataset’s name. For example, if we have a dataset named sample stored in the folder ‘C:\Users\Research\Thesis’, which we’ve told SAS is the location of our library called practice, then we refer to that dataset as

practice.sample

When you end your SAS session, any libraries that you’ve defined will be lost. This just means that when you restart SAS, you will need reload the library again in order to access its contents.

The Work Library

SAS has a built-in temporary library called Work. The Work library is a place to store data you are working on in your current session. Because the Work library is temporary, you will lose any datasets you created and stored in the Work library when you close out of your SAS session.

You do not have to declare the Work library with a LIBNAME statement because it is automatically created when you open SAS. Furthermore, you do not have to include it as the libref when you reference a dataset – a dataset named without a libref is assumed to be in the Work library.

A data step that starts with this line:

DATA work.StatsRule;

is the same as a data step that starts with this line:

DATA StatsRule;

Both will create a dataset called StatsRule in the work library. The Work library can be extremely useful when you want to manipulate a dataset with multiple data steps and you don’t necessarily want to save the datasets resulting from the interim steps.

The Work library can also be helpful if you want to work on your stored dataset, but you are nervous about manipulating the original file – just copy the dataset into the Work library and you can manipulate it there. Copying a dataset into the Work library is easy:

DATA test;
    SET practice.sample;
RUN;

The program above copies the dataset sample (which is located in the SAS library called practice) into a temporary dataset called test (which is located in the Work library). Now you can try out some manipulations on your temporary dataset called test without altering your original dataset. Once you are sure you’ve got it right, you can save the dataset back into your permanent library.

Using Libraries in SAS University Edition or SAS Studio

If you are using SAS University Edition and want to use SAS libraries to read and write permanent data files, the file path you use must refer to the shared folder: “/folders/myfolders”. In fact, if you have ever tried to specify a SAS library in another directory (such as "C:/Users/myname/Documents" or "/Users/myname/Documents"), you may have received an error message. Why is this?

Because SAS University Edition runs using virtualization software, it may only access files located in folders that have been marked as "shared" with SAS University Edition. (The "sharing" process is done in VirtualBox, and is part of the installation and setup for SAS University Edition.) The material that you store in this shared folder will remain in this folder between sessions of using SAS University Edition.

Fortunately, no matter where your "/folders/myfolders" directory is on your computer, in SAS University Edition, you will always use the file path "/folders/myfolders/" to refer to the shared folder. This true for users running SAS University Edition on Windows or Mac computers.

In SAS University Edition, you can use specify this shared folder with the following LIBNAME statement using this syntax:

LIBNAME practice '/folders/myfolders/';

This assigns the library name "practice" to the "/folders/myfolders/" directory.

Alternatively, you can also do this via the Libraries navigation pane. To create a library, click on the small filing cabinet icon under “Libraries”.

Screenshot of SAS Studio showing where to create library

This will open the New Library window:

Screenshot of SAS Studio showing what areas to include information: name, path, and option to re-create library at start-up

A Name: Create a name for the new library.

B Path: Similar to the LIBNAME statement, you will assign the location to be: /folders/myfolders/

C Re-create this Library at start-up: Optional setting for SAS to "remember" this library definition every time you use SAS University Edition. (This means that you wouldn't have to re-run the LIBNAME syntax or re-do the New Library steps the next time you launch SAS University Edition.)