Tuesday 30 November 2010

NOTE: Whither the WEEK Format?

My friend Eli recently wrote to tell me of a very successful 9.1 to 9.2 upgrade that was marred by just one issue. His colleague Sarah discovered that the WEEK format disappeared between 9.1 and 9.2. Sarah had been using the WEEK format to produce week numbers from dates.

With some research, Sarah managed to find an explanation in SAS usage note 34978. It tells an apparently odd tale:
For many years SAS did not have a format called WEEK. Customers often created user-defined formats with PROC FORMAT and used the format name of WEEK.

Beginning in SAS 9.1, a SAS WEEKw. format and informat were implemented but mistakenly not documented. If a customer had created a user-defined format called WEEK and attempted to use it in a program, SAS used its format called WEEK rather than the user-defined format by the same name.

Since the WEEK format and informat weren't documented anyway, the decision was made to remove them from the software beginning in SAS 9.2. This decision allows legacy code, that includes a user-defined format called WEEK, to run as expected. Now, when customers create their own format called WEEK, they will use it when they reference the WEEK format in code.

If you simply want to produce the week number for a SAS date, here is some sample code.

/* PROC FORMAT with the PICTURE statement creates a custom format. The %U directive writes the week number of the year (Sunday as the first day of the week) with no leading zero.
Today's date is written out using the WEEK format */

proc format;
  picture week '%U'(Datatype=Date);

data a;
  put x week.;
The tale seems odd because SAS took the format away completely; one might have expected them to keep it, or to offer it with a different name. However, further research shows that SAS includes several other facilities for managing week number, including new formats:

  • The WEEK function - returns the week number value in varying styles
  • WEEKU, WEEKV, and WEEKW formats and informats - reads and writes week numbers in varying styles

So, they took it away, but they didn't really! One might speculate that the WEEK format was added by a SAS staffer who wasn't aware of the other formats (and function). Anyway, if you're still on SAS V9.1 and you use the undocumented WEEK format or informat, you have been warned!