The LIBNAME statement: SAS language libraries


SAS language programs can read information from and write information to a variety of different file types, including SAS-language datasets and other files, database tables, IBM z/OS VSAM files, text files (including comma-separated values files) and so on.

The LIBNAME global statement can be used to specify a SAS language library. A library can contain datasets, catalogs, DATA step views and other files that can be used by SAS language programs. Libraries provide a single location for files and can be used by any SAS language program executed in the session in which the library is defined.

LIBNAME can also be used to access tables in databases and worksheets in Microsoft Excel workbooks. For information on how to do this, see The LIBNAME statement: Specifying a database server or workbook.

Note: You can also access sections in structured files such as XML or JSON. These use library engines. These are not discussed in this article, but the access method is the same as for accessing databases.

This forum article only describes how to use the LIBNAME global statement to specify the library in which datasets and other SAS-language files are stored.

The contents of a library are specified in a SAS-language program using the following format:

libref.member

where libref is a library reference (sometimes called a libref) and member is a file in that library. This format is known as a two-level name.

To specify a library reference for a location, the LIBNAME statement has the format:

LIBNAME libref location;

where libref is the name of the library reference that will be used in SAS language programs and location is an operating system specific pathname. For example, on Windows, the pathname might be:

LIBNAME myref 'c:\temp';

You could then access datasets in the folder c:\temp using the two-level name; for example:

DATA myref.outds;
  SET myref.inds;
  WHERE x GT 2;
RUN;

This reads the dataset inds.wpd in the folder c:\temp, and then writes observations where x is greater than 2 to the dataset outds.wpd in the same folder.

You can use this library reference wherever you can specify a dataset in the SAS language. For example:

PROC SORT DATA=myref.outds;
  BY x;
RUN;

In this example, the input dataset specified using the DATA option is the dataset outds.wpd in the folder c:\temp.

Note: You can specify engines to the LIBNAME statement, which ensure that files are correctly represented in a SAS language program. In the examples in this article, we have made use of the default WPD engine. There are many other engines available that enable you read other types of file, such as the .sas7bdat files. For information on engines, see the description of the LIBNAME global statement in the Altair SLC Reference for Language Elements.