Monday, March 28, 2016

Hack 2.13 Using the ATTRIB Statement to Specify Multiple Attributes


SAS Programming Professionals,

Did you know that you can specify the length, format, informat, and label for a variable all in the same statement?

The aptly named ATTRIB statement lets you specify the aforementioned attributes of a variable in a single statement, obviating the need for separate LENGTH, FORMAT, INFORMAT, and LABEL statements.  Here is an example:


data birthfile;
attrib birthdate length=6  label = "Birth Date" format=worddate.                  informat=mmddyy.;
attrib firstname length=$7 label = "First Name";
attrib lastname  length=$7 label = "Last Name";

input birthdate firstname lastname;

datalines;
042775 Michael Raithel
;
run;

proc print label noobs;
run;

In the example, we use the ATTRIB statement to specify LENGTH, FORMAT, INFORMAT, and LABEL for BIRTHDATE.  We don’t really need a FORMAT or INFORMAT for FIRSTNAME and LASTNAME, so we only specify the LENGTH and LABEL on the second two ATTRIB statements.  The resulting PROC PRINT looks like this:


                         First      Last
        Birth Date       Name       Name
       April 27, 1975    Michael    Raithel

Using an ATTRIB statement is a good alternative to using multiple LENGTH, FORMAT, INFORMAT, and LABEL statements because it specifies all of the attributes of a variable in one spot.  Good documentation!  See if it makes sense in your own SAS programs.

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