Bookmark and Share

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

[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 <>
To   "''" <>
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 

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. 


Charles Vellutini

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

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?

input str12 datestr

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


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


*   For searches and help try:

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