NicolasAnd
NicolasAnd

Reputation: 3

Create Custom Definition of Week

I have daily data and want to convert them to weekly, using the following definition. Every Monday denotes the beginning of week i, and Sunday denotes the end of week i.

My date variable is called day and is already has %td format. I have a feeling that I should use the dow() function, combined with egen, group() but I struggle to get it quite right.

Upvotes: 0

Views: 360

Answers (1)

Nick Cox
Nick Cox

Reputation: 37208

If your data are once a week and you have data for Mondays only, then your date variable is fine and all you need to do is declare delta(7) if you use tsset or xtset.

If your data are for two or more days a week and you wish to collapse or contract to weekly data, then you can convert to a suitable time basis like this:

* Example generated by -dataex-. To install: ssc install dataex
clear
input float date
22067
22068
22069
22070
22071
22072
22073
22074
22075
22076
22077
22078
22079
22080
end
format %td date


gen wdate = cond(dow(date) == 1, date, cond(dow(date) == 0, date - 6, date - dow(date) + 1))

format wdate %td
gen dow = dow(date) 
list, sepby(wdate)

     +-----------------------------+
     |      date   dow       wdate |
     |-----------------------------|
  1. | 01jun2020     1   01jun2020 |
  2. | 02jun2020     2   01jun2020 |
  3. | 03jun2020     3   01jun2020 |
  4. | 04jun2020     4   01jun2020 |
  5. | 05jun2020     5   01jun2020 |
  6. | 06jun2020     6   01jun2020 |
  7. | 07jun2020     0   01jun2020 |
     |-----------------------------|
  8. | 08jun2020     1   08jun2020 |
  9. | 09jun2020     2   08jun2020 |
 10. | 10jun2020     3   08jun2020 |
 11. | 11jun2020     4   08jun2020 |
 12. | 12jun2020     5   08jun2020 |
 13. | 13jun2020     6   08jun2020 |
 14. | 14jun2020     0   08jun2020 |
     +-----------------------------+

In short, index weeks by the Mondays that start them. Now collapse or contract your dataset. Naturally if you have panel or longitudinal data some identifier may be involved too. delta(7) remains essential for anything depending on tsset or xtset.

There is no harm in using egen to map to successive integers, but no advantage in that either.

A theme underlying this is that Stata's own weeks are idiosyncratic, always starting week 1 on 1 January and always having 8 or 9 days in week 52. For more on weeks in Stata, see the papers here and here, which include the advice given in this answer, and much more.

Upvotes: 1

Related Questions