2.3 Investigating Libraries with PROC CONTENTS & PROC DATASETS
Key Takeaways
- PROC CONTENTS prints the descriptor portion of a data set: variable names, types, lengths, positions, formats, informats, labels, and the observation and variable counts.
- DATA=libref._ALL_ lists every member of a library, and adding NODS suppresses the per-data-set variable detail to leave only the directory listing.
- VARNUM prints variables in creation (PDV) order instead of the default alphabetical order, which is how you confirm the physical column sequence.
- OUT= writes the descriptor metadata to a SAS data set so that program logic can react to variable names, types, and lengths.
- PROC DATASETS manages library members in place - DELETE, CHANGE, KILL, APPEND, MODIFY - without copying the data, and it ends with QUIT rather than RUN.
2.3 Investigating Libraries with PROC CONTENTS & PROC DATASETS
Quick Answer:
PROC CONTENTSis a read-only reporting procedure that prints the descriptor portion of a SAS data set — variable names, types, lengths, positions, formats, and counts.PROC DATASETSis a utility procedure that manages library members in place: deleting, renaming, appending, and modifying attributes without rewriting the data. The content guide names investigating libraries with base utility procedures as a main objective, andPROC CONTENTSis the procedure it calls out by name.
Every SAS data set has two parts. The data portion holds the observations. The descriptor portion holds the metadata: how many observations and variables exist, when the data set was created, which engine wrote it, and, for each variable, its name, type, length, position, format, informat, and label. PROC CONTENTS is how you read the descriptor portion without reading a single observation.
1. PROC CONTENTS on a Single Data Set
proc contents data=sashelp.class;
run;
The output arrives in two blocks:
- Data set attributes — member name, type, engine, creation and modification datetimes, observation count, variable count, observation length, whether the data set is sorted and by what, and compression status.
- Alphabetic list of variables and attributes — one row per variable with
#(position in the PDV),Variable,Type,Len, and, where assigned,Format,Informat, andLabel.
Exam Trap: the variable table is titled Alphabetic List of Variables for a reason.
PROC CONTENTSsorts variables alphabetically by default, not in the order they physically occur. The#column still shows the true position, and theVARNUMoption re-sorts the listing into that creation order.
/* Print variables in physical PDV order rather than alphabetically */
proc contents data=sashelp.class varnum;
run;
2. Investigating an Entire Library
Two special values turn PROC CONTENTS from a single-data-set report into a library survey.
| Syntax | Result |
|---|---|
data=libref._ALL_ | Reports on every member of the library, one full descriptor block per data set, preceded by a directory listing |
data=libref._ALL_ nods | Prints only the directory listing — member names, types, and sizes — and suppresses the per-data-set variable detail |
libname retail 'C:\SAS_Data\RetailStore';
/* Full descriptor for every member - can be very long */
proc contents data=retail._all_;
run;
/* Just the inventory of what lives in the library */
proc contents data=retail._all_ nods;
run;
Exam Trap:
NODSis only legal together with_ALL_. Codingproc contents data=retail.inventory nods;produces an error, because there is no directory to print for a single member. The mnemonic is "NO Data Set details".
Other Frequently Tested PROC CONTENTS Options
OUT=SAS-data-set— writes the descriptor metadata to a data set instead of (or in addition to) printing it. Columns includeMEMNAME,NAME,TYPE(1 = numeric, 2 = character),LENGTH,VARNUM,FORMAT, andLABEL. This is how programs make decisions about variables they cannot hardcode.NOPRINT— suppresses the printed report, normally paired withOUT=.SHORT— prints just a compact list of variable names with no attribute columns.DIRECTORY— prints the library directory along with the single-data-set report.
/* Capture metadata, then act on it: list every character variable */
proc contents data=work.customers out=work.meta(keep=name type length) noprint;
run;
proc print data=work.meta;
where type = 2; /* 2 = character, 1 = numeric */
title "Character variables in WORK.CUSTOMERS";
run;
3. PROC DATASETS: Managing Members in Place
PROC DATASETS performs library housekeeping without copying data, which makes it dramatically faster than a DATA step for the same task. It is a RUN-group procedure: statements execute at each RUN;, and the procedure stays open until it hits QUIT;.
proc datasets library=work nolist;
change old_sales = sales_2026; /* rename a data set */
delete temp1 temp2 scratch; /* delete specific members */
modify sales_2026; /* open one member for edits */
label Amount = 'Gross Amount (USD)';
format Amount dollar12.2;
rename Amt = Amount;
append base = master_sales
data = sales_2026; /* fast vertical append */
quit;
| Statement / option | Effect |
|---|---|
LIBRARY= (or LIB=) | Names the target library; defaults to WORK if omitted |
NOLIST | Suppresses the automatic library directory listing |
CHANGE old=new | Renames a data set |
DELETE name-list | Deletes the named members |
KILL | Deletes every member in the library — irreversible |
MODIFY name | Opens one member so LABEL, FORMAT, and RENAME can change attributes in place |
APPEND BASE= DATA= | Adds the observations of DATA= to the end of BASE= without rewriting BASE= |
CONTENTS | Runs the equivalent of PROC CONTENTS from inside PROC DATASETS |
Exam Trap:
PROC DATASETSends withQUIT;, notRUN;.PROC SORT,PROC PRINT,PROC MEANS, andPROC FREQend withRUN;.PROC DATASETS,PROC SQL, andPROC FORMATwith run-groups belong to the family that stays resident untilQUIT;is submitted.
CONTENTS versus DATASETS: When to Use Which
| Need | Procedure |
|---|---|
| Inspect variables, types, lengths, formats | PROC CONTENTS |
| List every member of a library | PROC CONTENTS ... _ALL_ NODS or PROC DATASETS directory |
| Capture metadata into a data set | PROC CONTENTS ... OUT= NOPRINT |
| Rename, delete, or append members | PROC DATASETS |
| Change a label, format, or variable name without rewriting rows | PROC DATASETS ... MODIFY |
PROC CONTENTS never changes anything, which is exactly why it is the safe first step when you inherit an unfamiliar library.
A programmer submits proc contents data=finance._all_ nods; run;. What does this produce?
By default, in what order does PROC CONTENTS list the variables of a data set, and which option changes that order?
Which PROC DATASETS statement renames a data set without copying its observations, and how must the step be terminated?
A program must branch based on whether a variable in an unfamiliar data set is numeric or character. Which PROC CONTENTS option makes that metadata available to program logic?