Before investigating other methods for reading data into a SAS data set, let's digress for a few minutes to learn how to use the CONTENTS procedure to view the contents of SAS libraries.
The CONTENTS Procedure
The CONTENTS procedure allows us to create SAS output that describes either the contents of a SAS library or the descriptor information for an individual SAS data set. In order to view the contents of a SAS library, we can use the following general form of the procedure:
PROC CONTENTS data = libref._ALL_ NODS; RUN;
- libref is the libref that you assigned to the library
- the _ALL_ option requests a listing of all of the SAS files in the library
- the NODS option (which stands for "no details") suppresses the printing of detailed information about each file when you specify the _ALL_ option.
- You need to connect the _ALL_ option to the libref with a period (.)
- You can specify the NODS option only when you specify the _ALL_ option. Let's take a look at an example
Example 3.1 Section
The following SAS code requests a listing of the contents of the library called sashelp:
PROC CONTENTS data = sashelp._ALL_ nods; RUN;
First, what are SAS Libraries all about? Let's have you launch your SAS application. If the Explorer Window is not active, then click on the Explorer tab that appears among the bottom tabs.
Double click Libraries, which contains all available libraries. Then you should see a listing of your libraries, that is, something that looks like this:
You should see, at least, three libraries that SAS defines for you by default:
- As you know, Work is a temporary library for files that do not need to be saved from session to session.
- Sashelp is a permanent library that contains more than 200 sample data and other files that control how SAS works at your site. You can download these sample data for practicing or illustrating purpose.
- Sasuser is a permanent library that contains SAS files in the Profile catalog that store your personal settings. You may also opt to store your own files there. (Personally, I don't.)
Now, let's have you run the SAS code. Upon doing so, you should see output that looks something like this:
The CONTENTS Procedure
|Physical Name||C:\Program Files\SASHome\SASFoundation\9.4\nls\en\SASCFG|
|Physical Name||C:\Program Files\SASHome\SASFoundation\9.4\core\sashelp|
|Physical Name||C:\Program Files\SASHome\SASFoundation\9.4\aacomp\sashelp|
|Physical Name||C:\Program Files\SASHome\SASFoundation\9.4\af\sashelp|
... partial listing below ...
|#||Name||Member Type||Level||File Size||Last Modified|
Now, to view the descriptor information for any of the files that appear in the sashelp library, we need to modify our CONTENTS procedure a little bit.
Example 3.2 Section
The following SAS code tells SAS to display the descriptor information for the class data set that appears in the sashelp library:
PROC CONTENTS data = sashelp.class; RUN;
Launch and run the SAS code. (If, by chance, the class data set is not stored in your sashelp library, modify the code so that it references a data set that is stored in the library.) Review the resulting output to convince yourself that SAS does indeed display the descriptor information. Note in particular that, by default, SAS displays the variable information in alphabetical order:
Alphabetic List of Variables and Attributes
If we instead preferred to view a listing of the variables in the order in which they appear in the data set, we again need to modify the CONTENTS procedure a bit.
Example 3.3 Section
The following SAS code again tells SAS to display the descriptor information for the class data set that appears in the sashelp library. The varnum option requests that SAS display the listing of the variables in the order in which they appear in the data set:
PROC CONTENTS data = sashelp.class varnum; RUN;
Launch and run the SAS code. (Again, if the deskact data set is not stored in your sasuser library, modify the code so that it references a data set that is stored in the library.) Review the resulting output to convince yourself that SAS displays the descriptor information, and in particular, the variable information in the order in which the variables appear in the data set:
Variables in Creation Order