Introduction to SAS - Part 02: LIBNAME, SAS Libraries, and Permanent Datasets
Overview
Part 01 introduced SAS as a system for data management and analysis and explained the concepts of observations, variables, DATA steps, and PROC steps.
This lesson introduces SAS libraries — the mechanism that connects a short reference name (a libref) to a physical folder on disk — and explains the difference between temporary and permanent SAS datasets.
Understanding libraries is essential for any SAS program that reads from or writes to files that must persist beyond the current session.
What Is a SAS Library?
A SAS library is an association between a short nickname (called a library reference or libref) and a physical folder on your computer or network.
Once defined, you can refer to datasets in that folder using the two-level name format: libref.dataset_name (e.g., MYLIB.SUBJECTS).
SAS has one built-in library that always exists: WORK. WORK is a temporary folder that SAS creates automatically at the start of each session. Any dataset in WORK is deleted when the session ends.
All other libraries must be explicitly defined using the LIBNAME statement before they can be used.
The LIBNAME Statement
The LIBNAME statement assigns a libref to a folder path. SAS will use that folder for reading and writing datasets until the libref is cleared or the session ends.
Syntax: LIBNAME libref "path\to\folder";
The libref must be 1 to 8 characters long, start with a letter or underscore, and contain only letters, digits, and underscores.
The folder path must exist before the LIBNAME statement runs — SAS does not create the folder automatically.
SAS Log
After running LIBNAME mydata, you can read and write datasets in C:\SAS_data\project1 using the two-level name mydata.datasetname.
LIBNAME _ALL_ LIST displays all active librefs, their paths, and their engine in the SAS log — useful for confirming a library was defined correctly.
If the path does not exist, SAS will write an ERROR to the log: ERROR: Physical file does not exist. Always create the folder on disk before defining the LIBNAME.
Look in the log after running the code — you should see a line confirming the library was successfully assigned.
Temporary vs Permanent Datasets
A temporary dataset uses a one-level name (e.g., subjects, work.subjects). It lives in the WORK library and is deleted when the SAS session ends.
A permanent dataset uses a two-level name pointing to a library other than WORK (e.g., mydata.subjects). It is saved to the physical folder on disk and persists after the session closes.
One-level names without a libref always refer to WORK — writing DATA subjects; is identical to DATA work.subjects;.
Use WORK for intermediate processing datasets. Use a permanent library for datasets that need to be available in future sessions, shared with colleagues, or delivered as output.
SAS Log
The permanent data step is commented out to avoid errors if the mydata library is not available in your environment — uncomment it after defining the library.
WORK.TEMP_SUBJECTS will be visible in the SAS explorer under the WORK library during this session. After closing SAS it will no longer exist.
MYDATA.PERM_SUBJECTS will persist as a .sas7bdat file in the target folder and can be read in any future SAS session that defines the same LIBNAME.
Dataset View
Viewing Datasets in a Library with PROC DATASETS
PROC DATASETS lists all datasets in a specified library. This is useful for confirming what exists in a permanent library before reading or overwriting files.
The CONTENTS statement within PROC DATASETS shows the structure (variable names, types, lengths) of a specific dataset.
SAS Log
LIBRARY=WORK specifies which library to inspect. Replace with a permanent libref to inspect that library.
MEMTYPE=DATA restricts the listing to data tables only (excludes catalogs, views, etc.).
The output shows all datasets currently in WORK — verify that WORK.TEMP_SUBJECTS appears in the list after running the earlier DATA step.
Clearing a Library with LIBNAME CLEAR
LIBNAME libref CLEAR removes the association between the libref and the folder. It does not delete the folder or the datasets in it — it simply disconnects the nickname.
After clearing, the libref is no longer recognised, and any code that references it will produce an ERROR until a new LIBNAME statement is run.
Clearing a library is useful in programs that use temporary librefs for intermediate processing and want to clean up at the end.
SAS Log
The LIBNAME CLEAR is commented out so it does not error if the library was never defined — uncomment it when using in a real program.
The physical folder and files remain on disk after CLEAR — only the SAS session's reference to them is removed.
Multiple Libraries in One Session
A SAS session can have many librefs active simultaneously — for example, a raw data library, a derived data library, and an output library.
Define all required LIBNAME statements at the top of your program so that every subsequent step can reference them without redefining them.
SAS Log
Defining all librefs at the top of the program means the paths are centralised — moving the project to a different server only requires updating the LIBNAME statements, not every DATA step or PROC step.
This pattern is standard in clinical programming where raw, derived, and output folders are clearly separated.
Key Points
A SAS library is a named reference (libref) pointing to a physical folder — defined with LIBNAME libref "path".
WORK is the built-in temporary library; all one-level dataset names refer to WORK. WORK is deleted when the session ends.
Permanent datasets use a two-level name (libref.dataset) and are saved as .sas7bdat files in the target folder — they persist across sessions.
LIBNAME _ALL_ LIST shows all active librefs in the log; PROC DATASETS lists the contents of a library.
LIBNAME libref CLEAR disconnects the reference without deleting the physical files.
Define all LIBNAME statements at the top of every program to keep paths centralised and easy to maintain.