NC STATE University
SAS Consulting, Department of Statistics

SAS FAQ: What is a LIBNAME statement used for?
SAS version 6.12 and 7

If you are not sure what a SAS data set is, read this first. What is a SAS data set?

A LIBNAME statement is used when you want SAS to store/retrieve SAS data sets from a directory other than the default, saswork, directory. This is the method used to save SAS data sets between SAS sessions.

The LIBNAME statement assigns an alias, libref, to a specific directory . The libref is used in subsequent programs in place of the directory name. Once a data set is stored in the desired directory, the associated libref must be used to retieve the data set. Without a libref SAS would look in the default, saswork, directory for the data set.

Background:
SAS stores data sets in the saswork directory unless another directory is requested by the program. The saswork directory is erased at the end of each SAS session. For this reason, data sets stored in saswork are only available during the current SAS session. Data sets stored in saswork are commonly referred to as "temporary". Data sets stored in any other directory are refered to as "permanent."

In summary:
  • The default storage location for SAS data sets is the saswork directory.
  • Data sets stored in the saswork directory are erased at the end of the SAS session.
  • Libname statements assign an alias, libref, to a directory.
  • Once Assigned, librefs are used to indicate the directory location for saving or accessing SAS data sets.

    Here are some examples.
    Program Statements Description
    data class1; No LIBNAME statement and no libref. The data set "class1" will be stored is the saswork directory. It will be erased at the end of the SAS session, so it is a "temporary" SAS data set.
    libname mylib 'research';
    data mylib.exp1;
    The LIBNAME statement defines the libref, "mylib", as the "research" subdirectory. The SAS data set "exp1" will be written in the "research" subdirectory, because the libref "mylib" was used on the data statement.
    libname inlib '.';
    data inlib.chem;
    The dot inside the parenthesis on the LIBNAME statement means "current working directory". The SAS data set "chem" will be written in the "current working directory", because the libref, "inlib", was used on the data statement.
    libname lib1 'c:\mysas';
    data a; set lib1.exp;
    Data set "a" will be a copy of "exp1". "a" will be written in the saswork directory, data set "exp1" will be read from c:\mysas.

    For some reason this is a difficult subject to teach. If you have suggestions for this document, please let us know. Thanks.



    Home   |   FAQ   |   SAS Class   |   Sample Programs   |   Tutorials   |   NC State Dept. of Statistics
    Maintained by: Sandy Donaghy and Joy Smith
    Last Modified: July 12, 1999