Skip to content
AgileLogics
5 min read

Libraries and Data Sets

Prerequisites: DATA steps and PROC steps

SAS stores tables as data sets — files with the extension .sas7bdat. A library is a name (a libref, up to 8 characters) that points to a folder of data sets.

Two-level names

Every data set has a two-level name, libref.name:

  • sashelp.class — the class data set in the SASHELP library.
  • work.pets — the pets data set in the WORK library.
  • pets — a one-level name always means work.pets.

WORK is temporary: SAS deletes it when your session ends. That's fine for intermediate results, but anything you want to keep belongs in a permanent library.

Creating a permanent library

/* illustrative: use your own home folder path */
libname mydata '/home/u12345678/data';

data mydata.pets;
   set work.pets;
run;

Log (abridged):

NOTE: Libref MYDATA was successfully assigned as follows:
      Engine:        V9
      Physical Name: /home/u12345678/data
NOTE: There were 3 observations read from the data set WORK.PETS.
NOTE: The data set MYDATA.PETS has 3 observations and 3 variables.
  • libname assigns the libref for this session — put it at the top of your programs. The folder must already exist.
  • set reads an existing data set, one row per iteration. The file pets.sas7bdat now exists in that folder and survives the session.

Looking inside a data set

PROC CONTENTS describes a data set's variables:

proc contents data=sashelp.class;
run;

Output (abridged):

Alphabetic List of Variables and Attributes

#    Variable    Type    Len

3    Age         Num       8
4    Height      Num       8
1    Name        Char      8
2    Sex         Char      1
5    Weight      Num       8

The full output also shows the number of observations (19), when the data set was created, and more. # is the variable's position.

Names

Data set and variable names can be up to 32 characters, must start with a letter or underscore, and can contain letters, digits, and underscores. They're case-insensitive: Height and HEIGHT are the same variable (SAS remembers the case you first used, for display).

Practice

Run PROC CONTENTS on SASHELP.CARS. How many observations does it have, and which variables are character?

Show solution
proc contents data=sashelp.cars;
run;

The output reports 428 observations. The character variables are Make, Model, Type, Origin, and DriveTrain; the rest (such as MSRP, Horsepower, and MPG_City) are numeric.