Monday, November 7, 2016

Hack 3.25 Updating SAS Data Set Metadata with PROC DATASETS

SAS Programming Professionals,

Did you know that you do not have to re-create an entire SAS data set in order to specify formats, informats, labels, or to rename variables? 

Here is an example of what a novice SAS programmer might code to add formats to price and date, add an informat to date, add labels for product and date, and to rename the variable Holiday to existing SAS data set SGFLIB.SNACKS:

data sgflib.snacks;
set  sgflib.snacks;

      format price dollar6.2
             date  worddate.
             ;
      informat date mmddyy10.;
      label    product = "Snack Name"
               date    = "Sale Date"
               ;
      rename Holiday = Holiday_Sale;

run;

This DATA step causes SAS to read every observation in SGFLIB.SNACKS and make a completely new version of it.  The only difference between the original and the new SAS data set is the change to the metadata of the four specified variables.  This is a waste of computer resources; especially if the data set being re-created is very large.

A much more efficient way to accomplish these changes is to use the DATASETS Procedure.  PROC DATASETS reads the descriptor portion of the SAS data set—stored in the first physical page on disk—makes the changes to the metadata, and then writes the page back to the disk.  So, instead of hundreds or thousands or millions of observations being read, SAS simply reads a single data set page.

This PROC DATASETS code is equivalent to the DATA step, above:

proc datasets library=sgflib;
modify snacks;
      format price dollar6.2
             date  worddate.
             ;
      informat date mmddyy10.;
      label product = "Snack Name"
            date    = "Sale Date"
            ;
      rename Holiday = Holiday_Sale;

run;
quit;

The PROC DATASETS code is clean, efficient, and much more professional!

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.

No comments:

Post a Comment