Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

st: RE: RE: Week handling broken in Stata, how to customize week handling


From   Nick Cox <[email protected]>
To   "'[email protected]'" <[email protected]>
Subject   st: RE: RE: Week handling broken in Stata, how to customize week handling
Date   Thu, 23 Feb 2012 09:47:58 +0000

In addition, note that -week()- is documented at [D] p.245 

week(ed)
Domain ed: %td dates 01jan0100 to 31dec9999 (integers 􀀀679,350 to 2,936,549)
Range: integers 1 to 52 and missing
Description: returns the numeric week of the year corresponding to date ed
(the first week of a year is the first 7-day period of the year).

I readily agree that this is quite likely to be not what you want, but Stata's implementation of -week()- is not broken. What it does is deliberate. 

In truth, whatever you do about weeks is quite wrong for many purposes, especially for an international and interdisciplinary user community. So, typically you need to knit your own. 

Nick 
[email protected] 

Charles Vellutini

See Nick's thorough answers to a very similar question I asked a little while back:

http://www.stata.com/statalist/archive/2012-02/msg00714.html

James Sams

Stata seems confused about the definition of a week. My understanding is that a week is a 7 day period starting either on Sunday or Monday, with ISO-8601 declaring Monday. The first week of the year is the week containing January 4th, leaving the possibility with 53 week years that extend into the new year. 
I believe I have found violations of all these invariants, though unfortunately, I have not documented all of them. The most critical is that it seems that after 2008, weeks begin to start on Tuesday instead of Monday. See the code below for an illustration of an 8 day week in an up-to-date Stata 12, with the following weeks beginning on Tuesday. Having a consistent 7 day week is of course important for aggregating sub-weekly data to the week level. In the past I thought I had seen 3-4 day weeks around the end/beginning of a year, but I cannot replicate that right now.

Which brings me to my next problem.

I have a sub-weekly dataset that I want to align to a weekly dataset. However, this latter dataset uses a Sunday-Saturday week, giving me the date of the Saturday that concludes each week. Is there a way I can force Stata to use a Sunday-Saturday week so that my aggregation of the former dataset does not have an off-by-one error compared to the latter dataset?


clear
input str12 datestr
"2007-12-23"
"2007-12-24"
"2007-12-25"
"2007-12-26"
"2007-12-27"
"2007-12-28"
"2007-12-29"
"2007-12-30"
"2007-12-31"
"2008-01-01"
"2008-01-02"
"2008-01-03"
"2008-01-04"
"2008-01-05"
"2008-01-06"
"2008-01-07"
"2008-01-08"
"2008-01-09"
"2008-01-10"
"2008-01-11"
"2008-01-12"
"2008-01-13"
"2008-01-14"
"2008-01-15"
end

gen int date = date(datestr, "YMD")
format date %tdCCYY-NN-DD_Day
gen int week = wofd(date)
format week %twCCYY-WW

list

collapse (count) day_count=date, by(week) format day_count %9.0f

list


*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index