Lesson 5: Getting SAS Data's Metadata¶
The SAS code for the CONTENTS procedure (PROC CONTENTS) below generates a report containing the following information.
- Dataset name: SASHELP.HEART
- Number of observations
- Number of variables
- Engine used to create the dataset
- Variable list showing:
- Name (e.g., AgeAtStart, Weight, Smoking)
- Type (Numeric or Character)
- Length
- Label
- Position in the dataset
title;
Proc contents data=sashelp.heart;
Run;
In the code snippet below, the POSITION option requests that the variables be listed in the order they appear in the dataset (their physical position), rather than alphabetically.
"ods select variables;" tells SAS to display only the output object named Variables. This suppresses the other default PROC CONTENTS output, such as dataset attributes and engine information.
The SAS code below uses PROC CONTENTS to display information about the variables in the SASHELP.CLASS dataset and limits the output to the Position table. VARNUM tells SAS to display variables in the order they appear in the dataset (their variable number), instead of the default alphabetical order.
Proc contents data=sashelp.class varnum;
ods select position;
Run;
With the SHORT option in the CONTENTS procedure below, variable names are listed in a row-by-row format;
proc contents data=sashelp.heart short;
Run;
The CONTENTS statement in the DATASETS procedure below also does the same output that PROC CONTENTS generates.
proc datasets;
contents data=sashelp.class;
quit;
How to write the PROC CONTENTS output to a file and print it?¶
- Add the OUT= filename option to the CONTENTS procedure to create a SAS data set where each observation is a variable from the original data set.
- Add the NOPRINT option to suppress the listing of the output data set.
- Sort the SAS data set by NAME and get the listing.
- Generate the listing using PROC PRINT
proc contents data = sashelp.heart
out = varsdata (keep= name type length label) noprint;
run;
proc sort data=varsdata; by name; run;
proc print data= varsdata noobs;
run;
| NAME | TYPE | LENGTH | LABEL |
|---|---|---|---|
| AgeAtDeath | 1 | 8 | Age at Death |
| AgeAtStart | 1 | 8 | Age at Start |
| AgeCHDdiag | 1 | 8 | Age CHD Diagnosed |
| BP_Status | 2 | 7 | Blood Pressure Status |
| Chol_Status | 2 | 10 | Cholesterol Status |
| Cholesterol | 1 | 8 | |
| DeathCause | 2 | 26 | Cause of Death |
| Diastolic | 1 | 8 | |
| Height | 1 | 8 | |
| MRW | 1 | 8 | Metropolitan Relative Weight |
| Sex | 2 | 6 | |
| Smoking | 1 | 8 | |
| Smoking_Status | 2 | 17 | Smoking Status |
| Status | 2 | 5 | |
| Systolic | 1 | 8 | |
| Weight | 1 | 8 | |
| Weight_Status | 2 | 11 | Weight Status |
How to get the names of SAS datasets that are stored in the SASHELP Library?¶
ods listing close;
ods output attributes=sashelp_attout;
proc contents data=mydir._all_;
run;
ods rtf file="C:\Explore\SAS\Lesson1data\sashelp_contents.rtf";
proc print data=sashelp_attout;
run;
ods rtf close;
ods listing;
ods select none;
proc contents data=sashelp._all_;
ods output members=m;
run;
ods select all;
proc print data=m noobs;
where memtype = 'DATA';
run;