Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down on April 23, and its replacement, is already up and running.

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

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

From   Charles Vellutini <>
To   "" <>
Subject   st: RE: Week handling broken in Stata, how to customize week handling
Date   Thu, 23 Feb 2012 00:49:37 -0800

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

-----Message d'origine-----
De : [] De la part de James Sams
Envoyé : jeudi 23 février 2012 05:51
À :
Objet : st: Week handling broken in Stata, how to customize week handling

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:

*   For searches and help try:

© Copyright 1996–2016 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index