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: Week handling broken in Stata, how to customize week handling

From   James Sams <>
Subject   st: Week handling broken in Stata, how to customize week handling
Date   Wed, 22 Feb 2012 22:51:09 -0600

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


James Sams
*   For searches and help try:

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