Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Week indicator

    Hi,

    I was wondering if anyone could possibly help me with constructing a couple of week indicators.

    I have 2 date variables: date1 and date2. I would like to use date1 to construct a weekno1 which would tell me the week number of the year. The catch is that weekno1 should count from Sunday through Saturday.

    Then, I want to look at compare date1 and date2, and assign a new weekno2 a value of whatever the week number (weekno1) that date2 falls into. How do I do this?

    My time frame is 2 months in the middle of the year, so I wouldn't have to deal with dates over years.

    Thank you very much!

    Xuyang


  • #2
    If you

    Code:
    search week, sj
    you will get something like this (except that in Stata you will get clickable links)

    . search week, sj

    Search of official help files, FAQs, Examples, SJs, and STBs

    SJ-12-4 dm0065_1 . . . . . Stata tip 111: More on working with weeks, erratum
    . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . N. J. Cox
    Q4/12 SJ 12(4):765 (no commands)
    lists previously omitted key reference

    SJ-12-3 dm0065 . . . . . . . . . . Stata tip 111: More on working with weeks
    . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . N. J. Cox
    Q3/12 SJ 12(3):565--569 (no commands)
    discusses how to convert data presented in yearly and weekly
    form to daily dates and how to aggregate such data to months
    or longer intervals

    SJ-10-4 dm0052 . . . . . . . . . . . . . . . . Stata tip 68: Week assumptions
    . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . N. J. Cox
    Q4/10 SJ 10(4):682--685 (no commands)
    tip on Stata's solution for weeks and on how to set up
    your own alternatives given different definitions of the
    week


    None of these pieces requires subscription to the Stata Journal to be accessed.

    What you need (or the trickiest part) could be as little as one line.

    You want weeks to start on Sunday. Then the daily date minus the day of the week gives you today if it's a Sunday or the previous Sunday if it's not.


    Code:
    . clear
    
    . set obs 14
    number of observations (_N) was 0, now 14
    
    . gen ddate = mdy(2, 17, 2017)+ _n - 1
    
    . format ddate %td
    
    . gen sundays = ddate - dow(ddate)
    
    . egen weekno = group(sundays)
    
    . l, sepby(sundays)
    
         +------------------------------+
         |     ddate   sundays   weekno |
         |------------------------------|
      1. | 17feb2017     20862        1 |
      2. | 18feb2017     20862        1 |
         |------------------------------|
      3. | 19feb2017     20869        2 |
      4. | 20feb2017     20869        2 |
      5. | 21feb2017     20869        2 |
      6. | 22feb2017     20869        2 |
      7. | 23feb2017     20869        2 |
      8. | 24feb2017     20869        2 |
      9. | 25feb2017     20869        2 |
         |------------------------------|
     10. | 26feb2017     20876        3 |
     11. | 27feb2017     20876        3 |
     12. | 28feb2017     20876        3 |
     13. | 01mar2017     20876        3 |
     14. | 02mar2017     20876        3 |
         +------------------------------+
    Note that in many ways, it is easiest to define a week by the day that starts it. That allows comparability between datasets that have differing date ranges.

    The pieces cited above spell out some of the limitations of Stata's definition of a week.

    Comment


    • #3
      Thank you!

      Xuyang

      Comment

      Working...
      X