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.
Last updated: August 2026

2.3 Investigating Libraries with PROC CONTENTS & PROC DATASETS

Quick Answer: PROC CONTENTS is a read-only reporting procedure that prints the descriptor portion of a SAS data set — variable names, types, lengths, positions, formats, and counts. PROC DATASETS is 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, and PROC CONTENTS is 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:

  1. 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.
  2. Alphabetic list of variables and attributes — one row per variable with # (position in the PDV), Variable, Type, Len, and, where assigned, Format, Informat, and Label.

Exam Trap: the variable table is titled Alphabetic List of Variables for a reason. PROC CONTENTS sorts variables alphabetically by default, not in the order they physically occur. The # column still shows the true position, and the VARNUM option 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.

SyntaxResult
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_ nodsPrints 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: NODS is only legal together with _ALL_. Coding proc 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 include MEMNAME, NAME, TYPE (1 = numeric, 2 = character), LENGTH, VARNUM, FORMAT, and LABEL. This is how programs make decisions about variables they cannot hardcode.
  • NOPRINT — suppresses the printed report, normally paired with OUT=.
  • 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 / optionEffect
LIBRARY= (or LIB=)Names the target library; defaults to WORK if omitted
NOLISTSuppresses the automatic library directory listing
CHANGE old=newRenames a data set
DELETE name-listDeletes the named members
KILLDeletes every member in the library — irreversible
MODIFY nameOpens 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=
CONTENTSRuns the equivalent of PROC CONTENTS from inside PROC DATASETS

Exam Trap: PROC DATASETS ends with QUIT;, not RUN;. PROC SORT, PROC PRINT, PROC MEANS, and PROC FREQ end with RUN;. PROC DATASETS, PROC SQL, and PROC FORMAT with run-groups belong to the family that stays resident until QUIT; is submitted.

CONTENTS versus DATASETS: When to Use Which

NeedProcedure
Inspect variables, types, lengths, formatsPROC CONTENTS
List every member of a libraryPROC CONTENTS ... _ALL_ NODS or PROC DATASETS directory
Capture metadata into a data setPROC CONTENTS ... OUT= NOPRINT
Rename, delete, or append membersPROC DATASETS
Change a label, format, or variable name without rewriting rowsPROC DATASETS ... MODIFY

PROC CONTENTS never changes anything, which is exactly why it is the safe first step when you inherit an unfamiliar library.

Test Your Knowledge

A programmer submits proc contents data=finance._all_ nods; run;. What does this produce?

A
B
C
D
Test Your Knowledge

By default, in what order does PROC CONTENTS list the variables of a data set, and which option changes that order?

A
B
C
D
Test Your Knowledge

Which PROC DATASETS statement renames a data set without copying its observations, and how must the step be terminated?

A
B
C
D
Test Your Knowledge

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?

A
B
C
D