Skip to content
Tutorials Center
My learning

Topics

Lesson 5 of 37 ยท Environment and Data Steps

SAS Libraries and Data Sets: WORK, SASHELP and LIBNAME

Understand SAS libraries and data sets: two-level names, the temporary WORK library, SASHELP sample data, the LIBNAME statement, PROC CONTENTS and data set options.

SAS keeps data in data sets, and data sets live in libraries. If you understand these two ideas you understand where every table in a SAS program comes from and where it goes. This lesson covers library names, the built-in libraries, the LIBNAME statement, how to inspect a data set and the options that fine-tune how a data set is read or written.

Two-level names

Every data set has a two-level name: library.dataset.

SAS
sashelp.class        /* library SASHELP, data set CLASS */
work.sales           /* library WORK,    data set SALES */
mydata.customers     /* library MYDATA,  data set CUSTOMERS */

A library is really a nickname (a libref) for a place where SAS stores files: a folder on disk, a database schema, or a cloud location. The libref is at most 8 characters long: it starts with a letter or underscore and contains letters, digits and underscores.

If you write only sales with one level, SAS assumes the library WORK. So data sales; and data work.sales; mean the same thing.

The built-in libraries

Library What it holds Lifetime
WORK Temporary data sets you create Deleted when the SAS session ends
SASHELP Sample data sets shipped with SAS (such as class, cars, heart) Permanent, read-only for normal users
SASUSER Personal settings and profiles in some setups Permanent

sashelp.class is the classic practice table: 19 students with Name, Sex, Age, Height and Weight. This course uses it often because it is always installed.

Creating a library with LIBNAME

To read or write permanent data, assign a libref to a folder:

SAS
libname proj "C:\training\project";   /* Windows path example */

data proj.students;
   set sashelp.class;
run;

On a Linux server the path would look like "/home/user/project". The folder must already exist; SAS does not create it. After the DATA step, the folder contains a file named students.sas7bdat. That file extension is how SAS stores data sets on disk, and the file name is the data set name in lowercase.

A LIBNAME statement is global. It lasts until the session ends or you clear it:

SAS
libname proj clear;

Next session you must run the libname statement again, because the libref is not saved with the data. Putting it at the top of every program is standard practice.

Other kinds of libraries

The same statement can connect to other storage using an engine. With SAS/ACCESS licensed, libname db oracle user=... path=...; makes database tables look like SAS data sets. You will see xlsx and json engines too. The rest of your code does not change, which is the beauty of the library idea.

Looking inside a data set

PROC CONTENTS shows the descriptor portion of a data set: its size, creation date and the list of variables with their type and length.

SAS
proc contents data=sashelp.class;
run;

The report lists the number of observations, the number of variables and a table of variables with the columns Variable, Type (Num or Char), Len and others. To see only the variable list in the order they are stored, add varnum. PROC PRINT shows the data itself:

SAS
proc print data=sashelp.class (obs=5);
run;

Notice (obs=5). This is a data set option.

Data set options

Options in parentheses after a data set name change how SAS reads or writes that one data set. The most useful:

Option Effect
keep=a b c / drop=a b c Keep or drop variables
rename=(old=new) Rename a variable
where=(age > 13) Read only rows meeting a condition
obs=n Stop after observation n
firstobs=n Start at observation n
label="text" Give the data set a label
SAS
data teens;
   set sashelp.class (keep=name age height
                      rename=(height=ht)
                      where=(age >= 13));
run;

Here SAS keeps three variables, renames height to ht and reads only observations with age >= 13. As an input option this is efficient because SAS filters while reading. As an output option (on the data statement) it controls what is written: data teens (drop=age);.

Variables: type, length and label

Each variable in the descriptor has:

  • Type: numeric (always stored as 8-byte floating point by default) or character.
  • Length: bytes of storage. Character variables can be 1 to 32,767 bytes; numerics 3 to 8.
  • Label: an optional description up to 256 characters used in reports.
  • Format and informat: display and input rules (see Informats and Formats).

A variable's length for character data is set the first time SAS sees it, which matters in Conditional Logic.

A model of name resolution

This Python stand-in (not SAS) shows how one-level and two-level names are resolved, which is the rule behind many "file does not exist" errors.

Python Python stand-in: resolving data set names

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The message "Libref ... is not assigned." is a real SAS error, and it usually means a libname statement was not run in this session.

Practical habits

  • Put a libname at the top of the program rather than relying on one set interactively.
  • Write results you want to keep to a permanent library and everything intermediate to WORK.
  • Never write into SASHELP; copy to WORK first.
  • Use PROC CONTENTS whenever you receive an unfamiliar data set.

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What library does the data step 'data sales;' write to?

    With a one-level name SAS uses the temporary WORK library.
  2. 2. What happens to WORK data sets when the SAS session ends?

    WORK is temporary and is cleaned when the session ends.
  3. 3. Which data set option reads only rows where age is above 13?

    The where= option filters observations as they are read.
  4. 4. Which procedure lists a data set's variables, types and lengths?

    PROC CONTENTS shows the descriptor portion of a data set.

Summary

  • A data set name has two levels: libref.dataset; one level means WORK.
  • WORK is temporary, SASHELP is a read-only sample library, permanent libraries are defined with libname.
  • PROC CONTENTS shows variables and attributes; PROC PRINT shows rows.
  • Data set options (keep=, drop=, rename=, where=, obs=, firstobs=) tune a single read or write.

Next: DATA Step Basics: SET, Assignments and KEEP/DROP