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, statalist.org is already up and running.


[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 <sams.james@gmail.com>
To   statalist@hsphsun2.harvard.edu
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?


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


-- 
James Sams
sams.james@gmail.com
*
*   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–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index