Rodrigo
Rodrigo

Reputation: 69

Stata: Aggregating by week

I have a dataset that has a date variable with missing dates.

var1
15sep2014
15sep2014
17sep2014
18sep2014
22sep2014
22sep2014
22sep2014
29sep2014
06oct2014

I aggregated the data using this command.

gen week = week(var1)

and the results look like this

var 1      week
15sep2014   37
15sep2014   37
17sep2014   38
18sep2014   38
22sep2014   38

I was wondering whether it would be possible to get the month name and year in the week variable.

Upvotes: 0

Views: 2841

Answers (2)

Nick Cox
Nick Cox

Reputation: 37208

In general, week() is part of the solution if and only if you define your weeks according to Stata's rules for weeks. They are

  1. Week 1 of the year starts on January 1, regardless.
  2. Week 2 of the year starts on January 8, regardless.
  3. And so on, except that week 52 of the year includes 8 or 9 days, depending on whether the year is leap or not.

Do you use these rules? I guess not. Then the simplest practice is to define a week by whichever day starts the week. If your weeks start on Sundays, then use the rule (dailydate - dow(dailydate)). If your weeks start on Mondays, ..., Saturdays, adjust the definition.

. clear 

. input str9 svar1

     svar1
1. "15sep2014"
2. "15sep2014"
3. "17sep2014"
4. "18sep2014"
5. "22sep2014"
6. "22sep2014"
7. "22sep2014"
8. "29sep2014"
9. "06oct2014" 
10. end 

. gen var1 = daily(svar1, "DMY") 

. gen week = var1 - dow(var1) 

. format week var1 %td 

. list

    +-----------------------------------+
    |     svar1        var1        week |
    |-----------------------------------|
 1. | 15sep2014   15sep2014   14sep2014 |
 2. | 15sep2014   15sep2014   14sep2014 |
 3. | 17sep2014   17sep2014   14sep2014 |
 4. | 18sep2014   18sep2014   14sep2014 |
 5. | 22sep2014   22sep2014   21sep2014 |
    |-----------------------------------|
 6. | 22sep2014   22sep2014   21sep2014 |
 7. | 22sep2014   22sep2014   21sep2014 |
 8. | 29sep2014   29sep2014   28sep2014 |
 9. | 06oct2014   06oct2014   05oct2014 |
    +-----------------------------------+

Much more discussion here, here and here, although the first should be sufficient.

Upvotes: 3

Sam Larson
Sam Larson

Reputation: 91

Instead of using the week() function, I would probably use the wofd() function to transform your %td daily date into a %tw weekly date. Then you can just play with the datetime display formats to decide exactly how to format the date. For example:

gen date_weekly = wofd(var1)
format date_weekly %twww:_Mon_ccYY

That code should give you this:

var1        date_weekly
15sep2014   37: Sep 2014
15sep2014   37: Sep 2014
17sep2014   38: Sep 2014
18sep2014   38: Sep 2014
22sep2014   38: Sep 2014

This help file will be useful:

help datetime display formats

And if you want to brush up on the difference between %tw and %td dates, you might refresh yourself here:

help datetime

Upvotes: 0

Related Questions