SAS (Base SAS)+
*==============================================================================;
* Step 1: create a small DM-like dataset ;
*==============================================================================;
data dm;
infile cards truncover dlm="|";
length domain $2 usubjid $5 sex $3;
input usubjid $ sex $ age;
domain = "DM";
label usubjid = "Unique Subject Identifier";
cards;
1001|M|72
1002|F|65
1003|F|45
1004|M|40
1005|F|35
1006|F|6
1007|F|5
;
run;
*==============================================================================;
* Step 2: run PROC CONTENTS and capture the metadata in a dataset ;
*==============================================================================;
proc contents data = dm out = meta01 noprint;
run;
*------------------------------------------------------------------------------;
* Step 3: print the metadata columns we care about ;
*------------------------------------------------------------------------------;
proc print data = meta01 noobs;
var memname name varnum type length nobs;
run;- To get a dataset-level metadata listing in SAS, we run
PROC CONTENTSon the target dataset. By default it writes a listing to the output window. - When we want the metadata as a SAS dataset (so we can filter or join it downstream), we add the
out=option — PROC CONTENTS then writes one row per variable to the named output dataset. - We add
noprintso PROC CONTENTS does not also dump the listing to the output window when all we want is themeta01dataset. - The key columns in the
out=dataset arememname(dataset name),name(variable name),varnum(position),type(1 = numeric, 2 = character),length(storage length in bytes) andnobs(number of observations in the parent dataset). - We then print the subset of columns we care about with
PROC PRINT, which gives us a clean tabular view — exactly the shape we aim to reproduce in R. - One gotcha: the
typecolumn inmeta01uses 1/2 encoding, not the words “numeric” and “character”. In the R versions below we emit readable class names (character,numeric) instead, which is friendlier for a listing.