Monday, October 3, 2016

Hack 3.21 Saving the Output of PROC CONTENTS to a SAS Data Set

SAS Programming Professionals,

Did you know that you can save the output of PROC CONTENTS to a SAS data set?

When you do so, it creates a SAS data set with one observation for each variable found in the original data set.  Each observation has specific information about one particular variable, as well as some redundant information about the original SAS data library.  For example:

proc contents data=sashelp.prdsale out=work.prdsale_contents;
run;

Creates a new SAS data set with one ten rows; one each for variables ACTUAL, COUNTRY, DIVISION, MONTH, PREDICT, PRODTYPE, PRODUCT, QUARTER, REGION, and YEAR.  A PROC CONTENTS of the new data set (work.prdsale_contents) yields, in part:

Alphabetic List of Variables and Attributes

     #    Variable    Type    Len    Format         Label

    32    CHARSET     Char      8                   Host Character Set
    33    COLLATE     Char      8                   Collating Sequence
    28    COMPRESS    Char      8                   Compression Routine
    20    CRDATE      Num       8    DATETIME16.    Create Date
    22    DELOBS      Num       8                   Deleted Observations in Data Set
    36    ENCRYPT     Char      8                   Encryption Routine
    19    ENGINE      Char      8                   Engine Name
    27    FLAGS       Char      3                   Update Flags (Protect Contribute Add)
    10    FORMAT      Char     32                   Variable Format
    12    FORMATD     Num       8                   Number of Format Decimals
    11    FORMATL     Num       8                   Format Length
    38    GENMAX      Num       8                   Maximum Number of Generations
    40    GENNEXT     Num       8                   Next Generation Number
    39    GENNUM      Num       8                   Generation Number
    25    IDXCOUNT    Num       8                   Number of Indexes for Data Set
    23    IDXUSAGE    Char      9                   Use of Variable in Indexes
    13    INFORMAT    Char     32                   Variable Informat
    15    INFORMD     Num       8                   Number of Informat Decimals
    14    INFORML     Num       8                   Informat Length
    16    JUST        Num       8                   Justification
     7    LENGTH      Num       8                   Variable Length
     1    LIBNAME     Char      8                   Library Name
     3    MEMLABEL    Char    256                   Data Set Label
     2    MEMNAME     Char     32                   Library Member Name
    24    MEMTYPE     Char      8                   Library Member Type
    21    MODATE      Num       8    DATETIME16.    Last Modified Date
     5   NAME       Char    32                Variable Name
    18    NOBS        Num       8                   Observations in Data Set
    34    NODUPKEY    Char      3                   Sort Option: No Duplicate Keys
    35    NODUPREC    Char      3                   Sort Option: No Duplicate Records
    17    NPOS        Num       8                   Position in Buffer
    37    POINTOBS    Char      3                   Point to Observations
    26    PROTECT     Char      3                   Password Protection (Read Write Alter)
    29    REUSE       Char      3                   Reuse Space
    30    SORTED      Num       8                   Sorted and/or Validated
    31    SORTEDBY    Num       8                   Position of Variable in Sortedby Clause
     6    TYPE        Num       8                   Variable Type
     4    TYPEMEM     Char      8                   Special Data Set Type (From TYPE=)
     8    VARNUM      Num       8                   Variable Number

I color coded the rows in the output above to better differentiate them.  Those colored blue specify information about the original SAS data set.  Those colored red specify information about the particular variable (ACTUAL, COUNTRY, DIVISION, etc.).  Variable names are stored in the NAME variable of this new data set.  So, in observation 1, NAME would have the value of “Actual”, in observation #2 NAME would have the value “Predict”, and so on.

There are dozens of uses for a PROC CONTENTS SAS data set, including documenting the specifics of the original SAS data set, programmatically querying a variable’s characteristics (Format, Informat, Length, whether it is used in an index, etc.), and programmatically querying the original data set’s attributes (whether it is compressed or indexed, the data set’s label, etc.).  So, add being able to create a PROC CONTENTS data set to your already burgeoning SAS programming techniques toolkit.

Best of luck in all your SAS endeavors!

----MMMMIIIIKKKKEEEE
(aka Michael A. Raithel)

Excerpt from the book:  Did You Know That?  Essential Hacks for Clever SAS Programmers

I plan to post each and every one of the hacks in the book to social media on a weekly basis.  Please pass them along to colleagues who you know would benefit.