In SAS, you can access data using a hardcoded file path in your program. This file path has two pieces of information, namely, the file location and the name and type of the data. For example:
proc print data="c:/myfiles/sasdata/my_sales.sas7bdat"; run;
Here the location of the data is c:/myfiles/sasdata, and the name and type of the data is my_sales.sas7bdat.
However, if you have a longer program or the location of your data changes, it would be tedious to provide or change repeatedly the full path. For this reason, SAS provides libraries.
In this article, I discuss the 10 most asked questions about libraries in SAS.
1. What is a SAS Library?
You can see a SAS library as a collection of files that are in the same location and are of the same type. A library provides you an efficient way to specify the location and file type.
2. How to Create a SAS Library?
You create (or assign) a SAS library with the LIBNAME statement. The statement starts with the LIBNAME keyword, followed by a library name (i.e., libref), the engine, and finally, the location of your files. Because the LIBNAME statement is a global statement, it doesn’t require a RUN statement.
LIBNAME libref <engine> "path";
- libref: The libref is the name of the library. A libref is at most 8 characters long, starts with a letter or an underscore, and can only include letters, numbers, or underscores.
- engine: The engine provides SAS with instructions on how to read the data. The default engine is BASE for SAS tables. Other engines exist for Excel, Teradata, Hadoop, etc.
- path: The location of the data you want to read. This can be a physical location to a folder or options to connect to a database.
So, you could use the following code to assign the library my_lib. This LIBNAME statement uses the BASE engine to read all SAS tables from c:/myfiles/sasdata/.
libname my_lib base "c:/myfiles/sasdata/";
libname my_lib "c:/myfiles/sasdata/";
3. How to Use a SAS Library?
You use a library in your SAS program to access your data by providing the libref, a period, and the name of the dataset. It isn’t necessary to provide the file extensions because you already provided the engine when you submitted the LIBNAME statement.
So, for example, instead of:
proc print data="c:/myfiles/sasdata/my_sales.sas7bdat"; run;
You could use:
libname my_lib base "c:/myfiles/sasdata/"; proc print data=my_lib.my_sales; run;
Another advantage of libraries is that, if the location of the file(s) changes, you only have to update the LIBNAME statement.
Alternatively, you can use SAS libraries also to import and export data in different formats. For example, Excel, XML, or SPSS.
4. How to Delete a SAS Library?
You delete a SAS library automatically when you close your SAS session or with a LIBNAME statement. The statement starts with the LIBNAME keyword, the libref you want to delete, and the CLEAR keyword.
libname my_lib clear;
Remember that a libref is just a pointer (or shortcut) to a location with data. When you delete the libref, you don’t delete the data. You only delete the connection between SAS and the data. For this reason, many SAS programs start with a couple of LIBNAME statements.
Do you know? How to Delete a Macro Variable
5. What is an Automatic SAS Library?
Automatic SAS libraries are libraries that are created automatically when you start your SAS session. Two libraries that SAS assigns automatically are:
- work: The work library is a temporary library because it’s always empty when you start your SAS session. Normally, you use this library to store your working files that can be deleted when you close your SAS session.
- sashelp: The sashelp library is a permanent library that contains sample data. You can use these datasets for testing.
Besides the work and sashelp library there might be other automatic libraries that are established by the SAS administrator.
6. How to Create a SAS Library for Excel Files?
You can also use the LIBNAME statement to create a library to store an Excel file. A single library contains all Excel sheets from one Excel file. In other words, you can consider each sheet as a separate table.
For example, with the code below, we create a library with all sheets of the Excel fine my_excel_file.xlsx.
options validvarname=v7; libname my_excel xlsx "c:/myfiles/sasdata/my_excel_file.xslx";
You use the VALIDVARNAME=V7 option to convert Excel column names that don’t comply with the SAS naming convention. For example, if you use this option, SAS removes whitespace in the column names.
See also How to Export an Excel File
7. How to Delete all Datasets in a SAS Library?
In SAS, you can delete a dataset with three different procedures, namely PROC DATASETS, PROC DELETE, or PROC SQL.
You use PROC DATASETS to delete one or more datasets from the same library. If you use the nolist option, SAS doesn’t create a report with the removed datasets.
proc datasets library=libref <nolist>; delete data-set-name-1 <data-set-name-2, ..., data-set-name-n>; run;
You can use PROC SQL and PROC DELETE to remove one or more tables from different libraries.
proc sql; drop table libref.data-set-name-1 <, libref.data-set-name-2, ..., libref.data-set-name-n>; quit;
proc delete data=libref.data-set-name-1 <libref.data-set-name-2 ... libref.data-set-name-n>; run;
You use the PROC DATASETS in combination with the kill keyword to remove all datasets from a library. SAS will delete all datasets without warning. So, make sure that you specify the library correctly.
proc datasets library=libref kill <nolist>; quit;
Examples? See this article
8. How to Show Library Content?
You can use PROC CONTENT to show all content of a library. With this procedure, SAS creates a report of all datasets, views, indexes, etc in the specified library.
proc contents data=libref._all_; run;
If your library contains many objects (dataset, view, etc), it will take a while to run this code.
Moreover, if you only want to see information about a specified dataset, you need to replace the _all_ keyword with the table name.
9. How to Find the Path of a Library?
Although you normally define your libraries with a LIBNAME statement, some libraries are assigned automatically when you start a SAS session. In this case, your SAS program doesn’t contain a LIBNAME statement with the physical path of the library. So, how do you find the path of a library?
You can find the path of a library with the PATHNAME function. To show the path to the directory, you create a SAS macro variable with the path and print it to the log.
libname my_lib "c:/myfiles/sasdata/"; %let path_my_lib = %sysfunc(pathname(my_lib)); %put Path to library: &path_my_lib;
10. How to Create a Library in SAS University Edition?
If you use SAS University Edition, it can be complicated to create a library because of the virtual machine it uses. I think this video does a great job explaining how to assign a library in SAS University Edition.
5 thoughts on “How to Create a SAS Library (+ 9 Other Questions about Libraries)”
Comments are closed.