Announcement

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

  • Creating indicator for 3 or more consecutive months

    Hello,

    I want to create an indicator variable by id for any individual with 3 or more consecutive months of prescription, using the prescription dates.
    I came across the egen seq( )and tried it but it does not exactly do what I want.

    Example dataset:
    id date
    1 09aug2012
    1 08nov2012
    1 10jan2013
    -----------------------------------
    2 09may2001
    2 23may2001
    2 11jul2001
    2 01aug2001
    2 03oct2001
    2 19nov2001
    -----------------------------------
    3 09nov2004
    3 06dec2004
    3 01jan2005
    3 01mar2005
    -----------------------------------

    Many thanks for the assistance.

    Kwame

  • #2
    Please do read and act on FAQ Advice #12

    https://www.statalist.org/forums/help#stata

    as requested to provide an easily used data example. Dates in particular are a nuisance unless properly set up using dataex (SSC).

    This is a guess at what you want. At first sight it is sufficient to find an observation preceded by an observation in the previous month and followed by one in the next month. But a run of dates Jan, Jan, Feb, Feb, Mar, Mar wouldn't satisfy that. Hence I reduce the dataset temporarily to at most one observation for each patient and month.

    Code:
    clear 
    input id str9 sdate
    1 09aug2012
    1 08nov2012
    1 10jan2013
    2 09may2001
    2 23may2001
    2 11jul2001
    2 01aug2001
    2 03oct2001
    2 19nov2001
    3 09nov2004
    3 06dec2004
    3 01jan2005
    3 01mar2005
    end 
    
    gen date = daily(sdate, "DMY")
    format date %td 
    
    * where you start
    gen mdate = mofd(date) 
    format mdate %tm 
    
    save tosave, replace 
    keep id mdate 
    duplicates drop 
    bysort id (mdate): gen any = (mdate == mdate[_n-1] + 1) & (mdate + 1 == mdate[_n+1]) 
    egen hasany = max(any), by(id) 
    drop any 
    
    merge 1:m id mdate using tosave 
    sort id mdate 
    drop _merge 
    list, sepby(id) 
    
         +-----------------------------------------------+
         | id     mdate   hasany       sdate        date |
         |-----------------------------------------------|
      1. |  1    2012m8        0   09aug2012   09aug2012 |
      2. |  1   2012m11        0   08nov2012   08nov2012 |
      3. |  1    2013m1        0   10jan2013   10jan2013 |
         |-----------------------------------------------|
      4. |  2    2001m5        0   09may2001   09may2001 |
      5. |  2    2001m5        0   23may2001   23may2001 |
      6. |  2    2001m7        0   11jul2001   11jul2001 |
      7. |  2    2001m8        0   01aug2001   01aug2001 |
      8. |  2   2001m10        0   03oct2001   03oct2001 |
      9. |  2   2001m11        0   19nov2001   19nov2001 |
         |-----------------------------------------------|
     10. |  3   2004m11        1   09nov2004   09nov2004 |
     11. |  3   2004m12        1   06dec2004   06dec2004 |
     12. |  3    2005m1        1   01jan2005   01jan2005 |
     13. |  3    2005m3        1   01mar2005   01mar2005 |
         +-----------------------------------------------+

    Comment


    • #3
      Many thanks Nick.
      Got the desired results.

      Comment

      Working...
      X