Announcement

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

  • streak of consecutive numbers

    Hi everyone,
    I am new to stata and have a basic question. I have the following data about days at which a patient visited the hospital.
    Patient 1: days 4, 5, 6, 8, 9, 11, 12, 13,14, 15,
    Patient 2: days 8, 9, 10, 14, 15, 16, 17, 19, 20

    I want to generate a new column with longest streak of consecutive days for each patient. How could I go about doing this?
    Thank you in advance!


  • #2
    Please use dataex to give an example (FAQ Advice #12). Your example is helpful but I had to retype it to do anything useful.

    There is a Stata FAQ that shows some relevant technique.

    https://www.stata.com/support/faqs/d...-observations/

    but here I just use general technique on spells as discussed in https://www.stata-journal.com/sjpdf....iclenum=dm0029

    The start of a spell (streak) is marked by a difference of dates more than 1. The works at the beginning of patient record too as the difference is turned as missing. For more discussion see (and please cite!) the article just mentioned. The new variable (not column!) is then within reach.

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input float(patient days)
    1  4
    1  5
    1  6
    1  8
    1  9
    1 11
    1 12
    1 13
    1 14
    1 15
    2  8
    2  9
    2 10
    2 14
    2 15
    2 16
    2 17
    2 19
    2 20
    end
    
    bysort patient : gen spell = sum((days - days[_n-1]) > 1)
    bysort patient spell : gen length = _N
    egen longest = max(length), by(patient)
    
    list , sepby(patient spell)
    
         +-------------------------------------------+
         | patient   days   spell   length   longest |
         |-------------------------------------------|
      1. |       1      4       1        3         5 |
      2. |       1      5       1        3         5 |
      3. |       1      6       1        3         5 |
         |-------------------------------------------|
      4. |       1      8       2        2         5 |
      5. |       1      9       2        2         5 |
         |-------------------------------------------|
      6. |       1     11       3        5         5 |
      7. |       1     12       3        5         5 |
      8. |       1     13       3        5         5 |
      9. |       1     14       3        5         5 |
     10. |       1     15       3        5         5 |
         |-------------------------------------------|
     11. |       2      8       1        3         4 |
     12. |       2      9       1        3         4 |
     13. |       2     10       1        3         4 |
         |-------------------------------------------|
     14. |       2     14       2        4         4 |
     15. |       2     15       2        4         4 |
     16. |       2     16       2        4         4 |
     17. |       2     17       2        4         4 |
         |-------------------------------------------|
     18. |       2     19       3        2         4 |
     19. |       2     20       3        2         4 |
         +-------------------------------------------+

    Comment


    • #3
      You'll probably want to first make your dataset in "long" format as opposed to its current "wide" format.

      Regardless, typing
      Code:
      help spell
      at the command line in Stata brings up one or two Stata Journal articles that are likely to be helpful,as well as a user-written command spell installable directly in Stata from SSC, and which could prove useful.

      Comment


      • #4
        Thanks for Joseph for the mention. tsspell (SSC) is regarded by its author as superseding spell.

        Comment

        Working...
        X