Assigning a SAS
© library can be done in several ways: via a
LIBNAME statement, a
LIBNAME function, the 'New Library' window in SAS
© Studio, an administrative interface, or an environment variable. The persistence of the assignment beyond the current
session depends on the chosen method.
A '
libref' is an abbreviated name or nickname for referencing the physical location of data. It must adhere to strict naming rules (maximum 8 characters, start with an English letter or an underscore, subsequent characters can be letters, numbers, or underscores) and must not use the reserved names Sashelp, Sasuser, or
Work. Librefs are valid only for the current
session unless persisted. They can be de-assigned and are referenced as the first element in a two-level name (for example, 'mylib.myfile').
The library engine is a SAS
© component that allows access to SAS
© files or files formatted by other applications. Although optional, it is recommended to specify it. The default engine is 'V9' (alias 'BASE'). SAS
© can sometimes automatically assign the correct engine, but explicit specification is a best practice, especially with files from different engines.
The physical location of the library is a mandatory element in the assignment. It is the path where data is created or accessed, enclosed in quotes. A relative path refers to the current working directory. SAS
© can, in some cases, automatically create the directory if the
DLCREATEDIR option is set.
Library options may be necessary depending on the engine and environment, particularly for database management systems (DBMS) or cloud storage systems.
LIBNAME statement options take precedence over system options, and dataset options take precedence over
LIBNAME options.
Using a
libref offers several advantages: convenience (shortcut for long paths), the ability to specify options for the entire library, ease of managing changes in data location, and grouping related files.
In some cases, a library assignment is not necessary: if no
libref is specified, SAS
© uses the temporary
Work library (unless a User library is assigned). It is also possible to reference a file directly by its physical location. However, direct access by location is not supported by all SAS
© functionalities (engines other than V9,
PROC COPY,
PROC DATASETS,
PROC SQL, certain dataset options, SAS
© views, stored compiled
DATA step programs, SAS
© catalogs, MDDB and FDB references). For unstructured data, a fileref is used instead of a
libref.