Date: Tue, 22 Nov 2005 13:23:54 -0600 Rob Rohrbough "SAS(r) Discussion" Rob Rohrbough MMWR Week Computation text/plain; charset="us-ascii"

I have a client who has asked for a way to compute the "MMWR week number". I found a posting a couple of years old from The Macro Maven at the CDC, which appears to compute the Morbidity and Mortality Weekly Report (http://www.cdc.gov/mmwr/) issue number. His post may be found with a SAS-L archive search on "MMWR". However, I don't believe that is what is required here. If I understand it correctly, the computation is as follows:

If the first day of the year falls on a Wednesday, or earlier in the week, the week number for the whole week is "1" - including the days of the past year that fall in that week. If the first day of a year falls on a Thursday or later, then the whole week is the last week of the prior year, computed from its starting week.

The SAS code and log, below (long), I believe, illustrate the correct computation. However, I am looking to you vast array of experts to challenge it and offer, perhaps, a better solution - especially if you happen to be familiar with the MMWR week number.

Any takers?

TIA,

Rob Rohrbough Omaha, NE

Log, beginning with code:

1 %macro MMWRweek (pDate, pWeek); %* Returns pWeek, a numeric variable, 2 for pDate, a date variable, 3 in a data step. 4 pWeek must be large enough to hold a 5 date variable temporarily; 6 7 if (&pDate > mdy(12,30,year(&pDate))) and (weekday(&pDate) < 4) then do; 8 &pWeek = 1; 9 end; else if (&pDate > mdy(12,29,year(&pDate))) and (weekday(&pDate) < 3) then do; 10 &pWeek = 1; 11 end; else if (&pDate > mdy(12,28,year(&pDate))) and (weekday(&pDate) < 2) then do; 12 &pWeek = 1; 13 end; else do; 14 if weekday(mdy(1,1,year(&pDate))) < 2 then 15 &pWeek = week(&pDate, 'u'); 16 else if weekday(mdy(1,1,year(&pDate))) < 5 then 17 &pWeek = week(&pDate, 'u') + 1; 18 else &pWeek = week(&pDate, 'u'); 19 if &pWeek = 0 then do; 20 &pWeek = mdy(12,31,year(&pDate)-1); 21 if weekday(mdy(1,1,year(&pWeek))) < 2 then 22 &pWeek = week(&pWeek, 'u'); 23 else if weekday(mdy(1,1,year(&pWeek))) < 5 then 24 &pWeek = week(&pWeek, 'u') + 1; 25 else &pWeek = week(&pWeek, 'u'); 26 end; * if week zero; 27 end; * if last three days in year; 28 %mend MMWRweek; 29 30 31 data datetest; 32 format d weekdate29.; 33 input d date9.; 34 w = week(d); 35 wu = week(d, 'u'); 36 wv = week(d, 'v'); 37 ww = week(d, 'w'); 38 wd = weekday(d); 39 40 %MMWRweek(d, wmmwr); 41 42 put d= @32 w= @40 wu= @48 wv= @55 ww= @65 wd= @72 wmmwr=; 43 if mod(_n_, 7) = 0 then put; 44 45 cards;

d=Thursday, December 29, 1994 w=52 wu=52 wv=52 ww=52 wd=5 wmmwr=52 d=Friday, December 30, 1994 w=52 wu=52 wv=52 ww=52 wd=6 wmmwr=52 d=Saturday, December 31, 1994 w=52 wu=52 wv=52 ww=52 wd=7 wmmwr=52 d=Sunday, January 1, 1995 w=1 wu=1 wv=52 ww=0 wd=1 wmmwr=1 d=Monday, January 2, 1995 w=1 wu=1 wv=1 ww=1 wd=2 wmmwr=1 d=Tuesday, January 3, 1995 w=1 wu=1 wv=1 ww=1 wd=3 wmmwr=1 d=Wednesday, January 4, 1995 w=1 wu=1 wv=1 ww=1 wd=4 wmmwr=1

d=Friday, December 29, 2000 w=52 wu=52 wv=52 ww=52 wd=6 wmmwr=52 d=Saturday, December 30, 2000 w=52 wu=52 wv=52 ww=52 wd=7 wmmwr=52 d=Sunday, December 31, 2000 w=53 wu=53 wv=52 ww=52 wd=1 wmmwr=1 d=Monday, January 1, 2001 w=0 wu=0 wv=1 ww=1 wd=2 wmmwr=1 d=Tuesday, January 2, 2001 w=0 wu=0 wv=1 ww=1 wd=3 wmmwr=1 d=Wednesday, January 3, 2001 w=0 wu=0 wv=1 ww=1 wd=4 wmmwr=1 d=Thursday, January 4, 2001 w=0 wu=0 wv=1 ww=1 wd=5 wmmwr=1

d=Saturday, December 29, 2001 w=51 wu=51 wv=52 ww=52 wd=7 wmmwr=52 d=Sunday, December 30, 2001 w=52 wu=52 wv=52 ww=52 wd=1 wmmwr=1 d=Monday, December 31, 2001 w=52 wu=52 wv=1 ww=53 wd=2 wmmwr=1 d=Tuesday, January 1, 2002 w=0 wu=0 wv=1 ww=0 wd=3 wmmwr=1 d=Wednesday, January 2, 2002 w=0 wu=0 wv=1 ww=0 wd=4 wmmwr=1 d=Thursday, January 3, 2002 w=0 wu=0 wv=1 ww=0 wd=5 wmmwr=1 d=Friday, January 4, 2002 w=0 wu=0 wv=1 ww=0 wd=6 wmmwr=1

d=Sunday, December 29, 2002 w=52 wu=52 wv=52 ww=51 wd=1 wmmwr=1 d=Monday, December 30, 2002 w=52 wu=52 wv=1 ww=52 wd=2 wmmwr=1 d=Tuesday, December 31, 2002 w=52 wu=52 wv=1 ww=52 wd=3 wmmwr=1 d=Wednesday, January 1, 2003 w=0 wu=0 wv=1 ww=0 wd=4 wmmwr=1 d=Thursday, January 2, 2003 w=0 wu=0 wv=1 ww=0 wd=5 wmmwr=1 d=Friday, January 3, 2003 w=0 wu=0 wv=1 ww=0 wd=6 wmmwr=1 d=Saturday, January 4, 2003 w=0 wu=0 wv=1 ww=0 wd=7 wmmwr=1

d=Monday, December 29, 2003 w=52 wu=52 wv=1 ww=52 wd=2 wmmwr=53 d=Tuesday, December 30, 2003 w=52 wu=52 wv=1 ww=52 wd=3 wmmwr=53 d=Wednesday, December 31, 2003 w=52 wu=52 wv=1 ww=52 wd=4 wmmwr=53 d=Thursday, January 1, 2004 w=0 wu=0 wv=1 ww=0 wd=5 wmmwr=53 d=Friday, January 2, 2004 w=0 wu=0 wv=1 ww=0 wd=6 wmmwr=53 d=Saturday, January 3, 2004 w=0 wu=0 wv=1 ww=0 wd=7 wmmwr=53 d=Sunday, January 4, 2004 w=1 wu=1 wv=1 ww=0 wd=1 wmmwr=1

d=Tuesday, December 29, 1998 w=52 wu=52 wv=53 ww=52 wd=3 wmmwr=52 d=Wednesday, December 30, 1998 w=52 wu=52 wv=53 ww=52 wd=4 wmmwr=52 d=Thursday, December 31, 1998 w=52 wu=52 wv=53 ww=52 wd=5 wmmwr=52 d=Friday, January 1, 1999 w=0 wu=0 wv=53 ww=0 wd=6 wmmwr=52 d=Saturday, January 2, 1999 w=0 wu=0 wv=53 ww=0 wd=7 wmmwr=52 d=Sunday, January 3, 1999 w=1 wu=1 wv=53 ww=0 wd=1 wmmwr=1 d=Monday, January 4, 1999 w=1 wu=1 wv=1 ww=1 wd=2 wmmwr=1

d=Wednesday, December 29, 2004 w=52 wu=52 wv=53 ww=52 wd=4 wmmwr=52 d=Thursday, December 30, 2004 w=52 wu=52 wv=53 ww=52 wd=5 wmmwr=52 d=Friday, December 31, 2004 w=52 wu=52 wv=53 ww=52 wd=6 wmmwr=52 d=Saturday, January 1, 2005 w=0 wu=0 wv=53 ww=0 wd=7 wmmwr=52 d=Sunday, January 2, 2005 w=1 wu=1 wv=53 ww=0 wd=1 wmmwr=1 d=Monday, January 3, 2005 w=1 wu=1 wv=1 ww=1 wd=2 wmmwr=1 d=Tuesday, January 4, 2005 w=1 wu=1 wv=1 ww=1 wd=3 wmmwr=1

d=Thursday, December 29, 2005 w=52 wu=52 wv=52 ww=52 wd=5 wmmwr=52 d=Friday, December 30, 2005 w=52 wu=52 wv=52 ww=52 wd=6 wmmwr=52 d=Saturday, December 31, 2005 w=52 wu=52 wv=52 ww=52 wd=7 wmmwr=52 d=Sunday, January 1, 2006 w=1 wu=1 wv=52 ww=0 wd=1 wmmwr=1 d=Monday, January 2, 2006 w=1 wu=1 wv=1 ww=1 wd=2 wmmwr=1 d=Tuesday, January 3, 2006 w=1 wu=1 wv=1 ww=1 wd=3 wmmwr=1 d=Wednesday, January 4, 2006 w=1 wu=1 wv=1 ww=1 wd=4 wmmwr=1

d=Sunday, November 13, 2005 w=46 wu=46 wv=45 ww=45 wd=1 wmmwr=46 d=Monday, November 14, 2005 w=46 wu=46 wv=46 ww=46 wd=2 wmmwr=46 d=Tuesday, November 15, 2005 w=46 wu=46 wv=46 ww=46 wd=3 wmmwr=46 d=Wednesday, November 16, 2005 w=46 wu=46 wv=46 ww=46 wd=4 wmmwr=46 d=Thursday, November 17, 2005 w=46 wu=46 wv=46 ww=46 wd=5 wmmwr=46 d=Friday, November 18, 2005 w=46 wu=46 wv=46 ww=46 wd=6 wmmwr=46 d=Saturday, November 19, 2005 w=46 wu=46 wv=46 ww=46 wd=7 wmmwr=46 NOTE: The data set WORK.DATETEST has 63 observations and 7 variables. NOTE: DATA statement used (Total process time): real time 0.14 seconds cpu time 0.05 seconds

109 run;

FYI, the input data (enclosed in CARDS; and RUN; statements) are:

cards; 29dec1994 30dec1994 31dec1994 01jan1995 02jan1995 03jan1995 04jan1995 29dec2000 30dec2000 31dec2000 01jan2001 02jan2001 03jan2001 04jan2001 29dec2001 30dec2001 31dec2001 01jan2002 02jan2002 03jan2002 04jan2002 29dec2002 30dec2002 31dec2002 01jan2003 02jan2003 03jan2003 04jan2003 29dec2003 30dec2003 31dec2003 01jan2004 02jan2004 03jan2004 04jan2004 29dec1998 30dec1998 31dec1998 01jan1999 02jan1999 03jan1999 04jan1999 29dec2004 30dec2004 31dec2004 01jan2005 02jan2005 03jan2005 04jan2005 29dec2005 30dec2005 31dec2005 01jan2006 02jan2006 03jan2006 04jan2006 13nov2005 14nov2005 15nov2005 16nov2005 17nov2005 18nov2005 19nov2005 run;

Back to: Top of message | Previous page | Main SAS-L page