Announcement

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

  • Organize in groups with same ID

    Hi to you all

    I am new to this forum and to STATA overall but so far I've managed to get along by browsing forums and using google. Now I have however come across a problem I just can't crack.

    I have some data where ID is not unique as there can be several info of same ID. Let me be a bit more clear.
    ID 1 pay "the" center a visit on date (xx.xx.xx) and has one observation that day (1) and the day after (2). Later that year (yy.yy.yy) the same person pay the center another visit and has again two observations with a days space (8+9). How can I group visit 1 and visit 2 uniquely?

    ID date Group
    1 | 1 | 1
    1 | 2 | 1
    1 | 8 | 2
    1 | 9 | 2
    2 | 2 | 1
    2 | 3 | 1
    2 | 8 | 2
    2 | 7 | 2
    3 | 2 | 1
    3 | 4 | 1
    3 | 10 | 2
    3 | 11 | 2

    I've tried using bysort but dates are not unique so I and up with no grouping at all.

    In hope of some help

    /Martin

  • #2
    The problem is that your criteria for grouping are not specified. For example, it is not just visits on successive days as ID 3 dates 2 and 4 are grouped together.

    So, if you will specify precise rules, then someone can give you the Stata [not STATA!] code you need.

    Comment


    • #3
      I shall forever call it Stata, sorry for the misunderstanding.

      I want to group using these criterias:
      If the time period between dates are more than 48 hours then its a new group with all the following obs < 48 hours.

      Hope it makes sense

      Comment


      • #4
        So, a gap more than 2 days starts a new group.

        Code:
        clear
        input ID date Group
        1  1  1
        1  2  1
        1  8  2
        1  9  2
        2  2  1
        2  3  1
        2  8  2
        2  7  2
        3  2  1
        3  4  1
        3  10  2
        3  11  2
        end
        bysort ID (date): gen newGroup = sum((date - date[_n-1]) > 2)
        list, sepby(ID newG)
        
        
             +------------------------------+
             | ID   date   Group   newGroup |
             |------------------------------|
          1. |  1      1       1          1 |
          2. |  1      2       1          1 |
             |------------------------------|
          3. |  1      8       2          2 |
          4. |  1      9       2          2 |
             |------------------------------|
          5. |  2      2       1          1 |
          6. |  2      3       1          1 |
             |------------------------------|
          7. |  2      8       2          2 |
          8. |  2      7       2          2 |
             |------------------------------|
          9. |  3      2       1          1 |
         10. |  3      4       1          1 |
             |------------------------------|
         11. |  3     10       2          2 |
         12. |  3     11       2          2 |
             +------------------------------+
        See also tsspell (SSC) and

        SJ-7-2 dm0029 . . . . . . . . . . . . . . Speaking Stata: Identifying spells
        . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . N. J. Cox
        Q2/07 SJ 7(2):249--265 (no commands)
        shows how to handle spells with complete control over
        spell specification
        http://www.stata-journal.com/sjpdf.h...iclenum=dm0029

        Comment

        Working...
        X