Weeks

September 3, 2010 at 4:32 am 2 comments

| Gabriel |

I use a lot of data that exists at the daily level (stored as a string like “9/3/2010”), but which I prefer to handle at the weekly level. In part this is about keeping the memory manageable but it also lets me bracket the epiphenomenal issues associated with the weekly work pattern of the music industry (e.g., records drop on Tuesdays). There are a few ways to turn a date into a week in Stata: the official way, the way I used to do it, and the way I do it now.

1. Beginning with Stata 10, there is a data type (%tw) which stores dates by the week, in contrast to %td (formerly just “%d”) which stores it as a date. This is good for some purposes but it gets messy if you’re trying to look at things that cross calendar years since the last week of the year can have a funny number of days. (Another reason I don’t use these functions is simply that I’ve been working on these scripts since Stata 9). However if you want to do it, it looks like this:

gen fp_w=wofd(date( firstplayed,"MDY"))
format fp_w %tw

2a. The way I prefer to do it is to store data as %td, but to force it to count by sevens, so effectively the %td data really stands for “the week ending in this date” or “this date give or take a few days.” Until a few days ago, I’d do this by dividing by seven and forcing it to be an integer, then multiplying again.

gen fpdate=date(firstplayed,"MDY")
gen int fp1=fpdate/7
gen fp_w=fp1*7
format fp_w %td
drop fp1

This is really ugly code and I’m not proud of it. First, note that this is a lot of code considering how little it does. I could have done this more efficiently by using the “mod(x,y)” function to subtract the remainder. Second, this only works if you’re interested in rounding off to the closest Friday and not some other day of the week.

2b. My new approach still stores as “%td” but is both more flexible and slightly simpler. In particular, it lets me define “week ending on X” where X is any day of the week I choose, here specified as the local `dow’ so I can define the end of the week once in the header and have it apply throughout several places that I do something like this. Note that Stata treats Sunday as 0, Monday as 1, etc. What I do is subtract the actual day of the week, then add the target day of the week so it substantively means “this thing happened on or a few days before this date.”

gen fp_w=date(firstplayed,"MDY")-dow(date(firstplayed,"MDY"))+`dow'
format fp_w %td

Entry filed under: Uncategorized. Tags: , .

(A lot of) Misc Links How to Review a Literature

2 Comments

  • 1. Nick Cox  |  September 4, 2010 at 5:51 am

    Various picky comments first:

    1. Some sloppiness over terminology here.

    In Stata terminology something like %tw is a display format and definitely not a data type indicating how data are stored. An easy way to see this is to understand that the same data can be displayed in different ways, for example numbers with fractional parts can be displayed rounded differently. But changing the display format does not affect what is stored.

    Note that although you could store dates as strings, meaning string variables. the formats you cite apply only to dates stored as numeric variables.

    2. It might help some readers to know that date in Stata is at its simplest “daily date”.

    3. For rounding to integer results the -floor()- and -ceil()- functions are worth knowing well. To round down in sevens, use 7 * floor(dailydate/7)-; to round up use -ceil()- instead. (-ceil()- is short for “ceiling”.)

    On the larger issue, it is easy to agree that weeks are awkward because, whatever one’s idea of the natural start and end of the week, they don’t nest within years.

    In
    http://www.stata.com/statalist/archive/2010-08/msg01180.html
    there is this one-liner for weeks classified according to Monday starts

    gen mondays = cond(dow(day) == 0, day – 6, day – dow(day) + 1)

    and there is a corresponding one-liner for any other pairing of day of the week and start or finish.

  • 2. Nick Cox  |  January 5, 2011 at 2:45 pm

    See now also

    Cox, N.J. 2010.
    Stata tip 68: Week assumptions
    The Stata Journal 10(4): 682–685.

    This includes a grateful nod to the prime mover of this blog.


The Culture Geeks


%d bloggers like this: