Announcement

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

  • Counting weeks with different start date by group

    Hi. My data comprises doctor names and the consultation date. The start date for consultations is different across doctors and there are missing days in between. I want the number of consults per week for each doctor from their individual start date. That is, if doctor A started consults on Oct 1, then week 1 = Oct 1-7, week 2 = Oct 8-14; if doctor B started consults on Oct 20, then for them week 1 = Oct 20-26, week 2 = Oct 28-Nov 2. Thus, a week would comprise 7 consecutive days from each doctors individual start date (not necessarily a week starting Monday) irrespective of whether there are missing days in between.

    I know how to count weeks from fixed start date, but am having trouble figuring out how to count weeks when the start date varies across doctors. Any help here would be much appreciated. Thanks.


    Code:
    
    input str37 ConsultantName float date_n
    
    "A" 22952
    "A" 22952
    "A" 22952
    "A" 22959
    "A" 23001
    "A" 23001
    "A" 23001
    "A" 23001
    "A" 23044
    "A" 23059
    "B" 22922
    "B" 22922
    "B" 22929
    "B" 22929
    "B" 22936
    "B" 22943
    "B" 22950
    "B" 22957
    "B" 22957

  • #2
    Code:
    clear 
    input str37 ConsultantName float date_n
    "A" 22952
    "A" 22952
    "A" 22952
    "A" 22959
    "A" 23001
    "A" 23001
    "A" 23001
    "A" 23001
    "A" 23044
    "A" 23059
    "B" 22922
    "B" 22922
    "B" 22929
    "B" 22929
    "B" 22936
    "B" 22943
    "B" 22950
    "B" 22957
    "B" 22957
    end 
    
    bysort ConsultantName (date_n) : gen week = ceil((date_n - date_n[1]  + 1)/7) 
    
    list, sepby(ConsultantName week) 
    
         +--------------------------+
         | Consul~e   date_n   week |
         |--------------------------|
      1. |        A    22952      1 |
      2. |        A    22952      1 |
      3. |        A    22952      1 |
         |--------------------------|
      4. |        A    22959      2 |
         |--------------------------|
      5. |        A    23001      8 |
      6. |        A    23001      8 |
      7. |        A    23001      8 |
      8. |        A    23001      8 |
         |--------------------------|
      9. |        A    23044     14 |
         |--------------------------|
     10. |        A    23059     16 |
         |--------------------------|
     11. |        B    22922      1 |
     12. |        B    22922      1 |
         |--------------------------|
     13. |        B    22929      2 |
     14. |        B    22929      2 |
         |--------------------------|
     15. |        B    22936      3 |
         |--------------------------|
     16. |        B    22943      4 |
         |--------------------------|
     17. |        B    22950      5 |
         |--------------------------|
     18. |        B    22957      6 |
     19. |        B    22957      6 |
         +--------------------------+
    
    .

    Comment


    • #3
      Thank you, Nick

      Comment

      Working...
      X