*Copyright @ www.mycsg.in;
What does PROC CONTENTS do
`proc contents` reports metadata about a SAS dataset such as variable names, types, lengths, labels, sort information, and number of observations
It is very useful when you need to understand an unfamiliar dataset or generate metadata-driven outputs
With the `out=` option the metadata can be captured into another SAS dataset for further filtering and analysis
Get the list of variables in a dataset into another dataset
Get the list of variables present in males dataset of WORK library into a dataset named males_cont01
`proc contents` reads the metadata of `males` not the data values themselves
`out=males_cont01` stores the metadata rows in a SAS dataset
`varnum` preserves the original variable order from the dataset
proc contents data=males out=males_cont01 varnum; run;
Copy Code
View Log
SAS Log
proc contents data=males out=males_cont01 varnum; run; NOTE: The data set WORK.MALES_CONT01 has 5 observations and 41 variables. NOTE: PROCEDURE CONTENTS used (Total process time): real time 0.01 seconds cpu time 0.00 seconds
Inspect `males_cont01` and review columns such as `name`, `type`, `length`, and `varnum`
This output dataset can be used later for documentation or automated programming tasks
View Data
Dataset View
Get the list of variables present in females dataset of WORK library into a dataset named females_cont01
This example repeats the same technique for another dataset
Comparing `males_cont01` and `females_cont01` is a simple way to check whether two datasets share the same variable structure
proc contents data=females out=females_cont01 varnum; run;
Copy Code
View Log
SAS Log
proc contents data=females out=females_cont01 varnum; run; NOTE: The data set WORK.FEMALES_CONT01 has 5 observations and 41 variables. NOTE: PROCEDURE CONTENTS used (Total process time): real time 0.01 seconds cpu time 0.01 seconds
View Data
Dataset View
Get the list of variables present in cars dataset of WORK library into a dataset named cars_cont01
We first copy `sashelp.cars` into a WORK dataset named `cars`
Then we capture its metadata using `proc contents`
data cars; set sashelp.cars; run; proc contents data=cars out=cars_cont01 varnum; run;
Copy Code
View Log
SAS Log
data cars; set sashelp.cars; run; NOTE: There were 428 observations read from the data set SASHELP.CARS. NOTE: The data set WORK.CARS has 428 observations and 15 variables. NOTE: DATA statement used (Total process time): real time 0.01 seconds cpu time 0.01 seconds proc contents data=cars out=cars_cont01 varnum; run; NOTE: The data set WORK.CARS_CONT01 has 15 observations and 41 variables. NOTE: PROCEDURE CONTENTS used (Total process time): real time 0.01 seconds cpu time 0.01 seconds
The metadata dataset for `cars` is much larger and useful for practicing metadata filtering
View Data
Dataset View
Get the list of variables present in datasets named males, females, and class of WORK library into a dataset named cont01
`work._all_` tells `proc contents` to generate metadata for all datasets in the WORK library
We then subset the metadata rows to keep only the datasets of interest
This is useful when you need a combined metadata inventory for several datasets
proc contents data=work._all_ out=cont01_pre varnum; run; data cont01; set cont01_pre; where memname in ("MALES", "FEMALES", "CLASS"); run;
Copy Code
View Log
SAS Log
proc contents data=work._all_ out=cont01_pre varnum; run; NOTE: The data set WORK.CONT01_PRE has 159 observations and 41 variables. NOTE: PROCEDURE CONTENTS used (Total process time): real time 0.03 seconds cpu time 0.01 seconds data cont01; set cont01_pre; where memname in ("MALES", "FEMALES", "CLASS"); run; NOTE: There were 15 observations read from the data set WORK.CONT01_PRE. WHERE memname in ('CLASS', 'FEMALES', 'MALES'); NOTE: The data set WORK.CONT01 has 15 observations and 41 variables. NOTE: DATA statement used (Total process time): real time 0.00 seconds cpu time 0.01 seconds
Inspect `cont01` and confirm that it contains metadata rows only for the chosen dataset names
View Data
Dataset View
Get the number of observations present in each dataset of SASHELP library into a dataset named numobs
The metadata output from `proc contents` includes the variable `nobs`
Because one metadata row is produced per variable we use `proc sort nodupkey` to reduce that to one row per dataset
proc contents data=sashelp._all_ out=cont01_pre varnum; run; proc sort data=cont01_pre out=numobs(keep=libname memname typemem nobs) nodupkey; by libname memname typemem nobs; run;
Copy Code
View Log
SAS Log
proc contents data=sashelp._all_ out=cont01_pre varnum; run; NOTE: The data set WORK.CONT01_PRE has 17124 observations and 41 variables. NOTE: PROCEDURE CONTENTS used (Total process time): real time 1.28 seconds cpu time 0.39 seconds proc sort data=cont01_pre out=numobs(keep=libname memname typemem nobs) nodupkey; by libname memname typemem nobs; run; NOTE: There were 17124 observations read from the data set WORK.CONT01_PRE. NOTE: 16908 observations with duplicate key values were deleted. NOTE: The data set WORK.NUMOBS has 216 observations and 4 variables. NOTE: PROCEDURE SORT used (Total process time): real time 0.03 seconds cpu time 0.01 seconds
`numobs` is now a dataset-level summary of observation counts across SASHELP members
This pattern is useful when reviewing large libraries quickly
View Data
Dataset View
Get the list of numeric variables present in class dataset of SASHELP library into a dataset named cont01_num
In PROC CONTENTS output the variable `type` identifies whether a variable is numeric or character
Numeric variables usually have `type=1` and character variables have `type=2`
We use that rule to subset only numeric variable metadata rows
data class; set sashelp.class; run; proc contents data=sashelp.class out=cont01_pre; run; data cont01_num; set cont01_pre; where type=1; keep libname memname name type; run;
Copy Code
View Log
SAS Log
data class; set sashelp.class; run; NOTE: There were 19 observations read from the data set SASHELP.CLASS. NOTE: The data set WORK.CLASS has 19 observations and 5 variables. NOTE: DATA statement used (Total process time): real time 0.00 seconds cpu time 0.00 seconds proc contents data=sashelp.class out=cont01_pre; run; NOTE: The data set WORK.CONT01_PRE has 5 observations and 41 variables. NOTE: PROCEDURE CONTENTS used (Total process time): real time 0.01 seconds cpu time 0.01 seconds data cont01_num; set cont01_pre; where type=1; keep libname memname name type; run; NOTE: There were 3 observations read from the data set WORK.CONT01_PRE. WHERE type=1; NOTE: The data set WORK.CONT01_NUM has 3 observations and 4 variables. NOTE: DATA statement used (Total process time): real time 0.00 seconds cpu time 0.01 seconds
Inspect `cont01_num` and verify that variables such as `age`, `height`, and `weight` are included while character variables are excluded
View Data
Dataset View
Get the first variable in datasets named class and cars of SASHELP library into a dataset named first_vars
When `varnum=1` the row represents the first variable in that dataset's variable order
This example shows how PROC CONTENTS metadata can be filtered for structural questions about datasets
proc contents data=sashelp._all_ out=cont01_pre; run; data cont01; set cont01_pre; where memname in ("CLASS" "CARS"); run; data first_vars; set cont01; where varnum=1; keep libname memname name varnum; run;
Copy Code
View Log
SAS Log
proc contents data=sashelp._all_ out=cont01_pre; run; NOTE: The data set WORK.CONT01_PRE has 17124 observations and 41 variables. NOTE: PROCEDURE CONTENTS used (Total process time): real time 0.20 seconds cpu time 0.17 seconds data cont01; set cont01_pre; where memname in ("CLASS" "CARS"); run; NOTE: There were 20 observations read from the data set WORK.CONT01_PRE. WHERE memname in ('CARS', 'CLASS'); NOTE: The data set WORK.CONT01 has 20 observations and 41 variables. NOTE: DATA statement used (Total process time): real time 0.00 seconds cpu time 0.00 seconds data first_vars; set cont01; where varnum=1; keep libname memname name varnum; run; NOTE: There were 2 observations read from the data set WORK.CONT01. WHERE varnum=1; NOTE: The data set WORK.FIRST_VARS has 2 observations and 4 variables. NOTE: DATA statement used (Total process time): real time 0.01 seconds cpu time 0.00 seconds
Review `first_vars` and confirm which variable appears first in each selected dataset
View Data
Dataset View
Identify the variables in sashelp.cars that have labels and those that do not
Variable labels are stored in the metadata column `label`
Missing labels can be separated from populated labels using normal DATA step logic on the contents output dataset
This is useful for documentation review and data quality checks
proc contents data=sashelp.cars out=cont01; run; data withlabels withoutlabels; set cont01; if missing(label) then output withoutlabels; else output withlabels; keep libname memname name label; run;
Copy Code
View Log
SAS Log
proc contents data=sashelp.cars out=cont01; run; NOTE: The data set WORK.CONT01 has 15 observations and 41 variables. NOTE: PROCEDURE CONTENTS used (Total process time): real time 0.01 seconds cpu time 0.01 seconds data withlabels withoutlabels; set cont01; if missing(label) then output withoutlabels; else output withlabels; keep libname memname name label; run; NOTE: There were 15 observations read from the data set WORK.CONT01. NOTE: The data set WORK.WITHLABELS has 6 observations and 4 variables. NOTE: The data set WORK.WITHOUTLABELS has 9 observations and 4 variables. NOTE: DATA statement used (Total process time): real time 0.01 seconds cpu time 0.01 seconds
Compare `withlabels` and `withoutlabels` to see which variables have user-friendly labels already defined
View Data
Dataset View
Key points to remember
`proc contents` is a metadata inspection procedure
The `out=` option lets metadata be saved and processed like any other SAS dataset
`varnum` preserves original variable order
Metadata filtering can answer practical questions about types, labels, positions, and observation counts