Monday, March 21, 2016

Hack 2.11 Stripping the Formats Off of Every Variable in a SAS® Data Set


SAS Programming Professionals,

Did you know that you can quickly and easily strip the formats off of every variable in a SAS data set?

This is handy in situations where you have a SAS data set without the associated format catalog.  In such instances, it is difficult to open the SAS data set in DATA steps and procedures, and it is difficult to view it in SAS Explorer.  Often, you may simply choose to specify OPTIONS NOFMTERR to preserve the format associations and still be able to work with the data set.  However, if you determine that this is too much trouble and that you really do not want the format associations, you can permanently remove them with the DATASETS Procedure.  

Here is an example:

      /*Allocate SAS libraries*/
      libname ctemp "c:\temp";

      /* Create PRDSAL2 in CTEMP library */
      data ctemp.prdsal2;
      set sashelp.prdsal2;
      run;

/* PROC CONTENTS to show the formats */
proc contents data=ctemp.prdsal2 details;
run;

/* Strip all formats from PRDSAL2 data set*/
proc datasets library=ctemp nolist;
modify prdsal2;
format _all_;
run;

/* PROC CONTENTS to show all formats are gone baby gone!*/
proc contents data=ctemp.prdsal2 details;
run;

The first part of the example is simply setup.  The real action takes place in the DATASETS Procedure.  There, we specify the relevant SAS data library and SAS data set, CTEMP and PRDSAL2, respectively.  Then, the FORMAT statement with _ALL_ strips the formats off of all of the variables in the SAS data set.

I would bet that some of you are wondering what you need to do to strip off the formats of individual variables… right?  That is easy enough:

      proc datasets library=ctemp nolist;
      modify prdsal2;
      format actual country;
      run;

 …will relieve variables ACTUAL and COUNTRY of their formats.  It’s that simple!

Best of luck in all of 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