Announcement

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

  • Subgroup within id variable for panel data

    I have panel data with an id variable for each person, I want to generate a subgroup identifier based on consecutive number of days. Based on the dataex given at the end, I'd like to have a variable that gives:
    id drug day period
    1 1 1 1
    1 1 2 1
    1 1 3 1
    1 1 4 1
    1 1 5 1
    1 1 8 2
    1 1 9 2
    1 1 10 2
    1 1 13 3
    1 1 14 3

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input int id byte drug float days
    1 1 1
    1 1 2
    1 1 3
    1 1 4
    1 1 5
    1 1 8
    1 1 9
    1 1 10
    1 1 13
    1 1 14
    1 1 15
    1 1 16
    2 1 1
    2 1 2
    2 1 3
    2 1 4
    2 1 5
    2 1 6
    2 1 7
    2 1 8
    2 1 24
    2 1 25
    2 1 26
    2 1 31
    2 1 32
    2 1 33
    2 1 34
    2 1 41
    2 1 42
    2 1 44
    2 1 45
    2 1 46
    2 1 47
    2 1 48
    2 1 49
    2 1 50
    2 1 51
    2 1 52
    2 1 53
    2 1 54
    2 1 55
    2 1 56
    2 1 57
    2 1 60
    2 1 61
    2 1 62
    3 1 1
    3 1 2
    3 1 3
    3 1 4
    3 1 5
    3 1 6
    3 1 7
    3 1 8
    3 1 9
    3 1 10
    3 1 11
    3 1 12
    3 1 13
    3 1 14
    3 1 15
    3 1 16
    3 1 21
    3 1 22
    4 1 1
    4 1 2
    4 1 3
    4 1 4
    5 1 1
    7 1 1
    7 1 2
    7 1 3
    7 1 4
    7 1 5
    7 1 6
    7 1 7
    7 1 8
    7 1 9
    7 1 10
    7 1 45
    7 1 46
    7 1 47
    7 1 48
    7 1 49
    7 1 50
    7 1 51
    7 1 52
    7 1 53
    7 1 54
    7 1 65
    7 1 66
    7 1 71
    7 1 72
    7 1 73
    7 1 74
    7 1 75
    7 1 76
    7 1 77
    7 1 78
    9 1 1
    end

  • #2
    You have nice data examples here, but I don't understand the rules that map the data you have into what you want. In particular, the phrase "generate a subgroup identifier based on consecutive number of days" doesn't click for me; perhaps it does for someone else. "Consecutive number of days" for what? The "subgroup" would denote people who share what characteristic in common?
    And, I don't know what "period" is supposed to designate: The period during which *what* prevails? I think it would increase your chances of a quick answer if you could explain this differently.

    Comment


    • #3
      I think what you are looking for is an indicator of "spells" of consecutive days - each time there's a break in the sequence, or a new id (I'm not sure of the role of drug) you want to start a new sequence.

      There's a good discussion of this by Nick Cox in the Stata Journal paper (currently free to download) at https://www.stata-journal.com/articl...article=dm0079

      Starting from your example data, the code below does what I understand you to want. It's left as an exercise for the reader to combine the two generate commands into one; I chose the wordier version to make it more apparent what is happening.
      Code:
      . by id (days), sort: generate newspell = days != days[_n-1]+1
      
      . by id (days), sort: generate spell = sum(newspell)
      
      . list, noobs sepby(id spell)
      
        +-------------------------------------+
        | id   drug   days   newspell   spell |
        |-------------------------------------|
        |  1      1      1          1       1 |
        |  1      1      2          0       1 |
        |  1      1      3          0       1 |
        |  1      1      4          0       1 |
        |  1      1      5          0       1 |
        |-------------------------------------|
        |  1      1      8          1       2 |
        |  1      1      9          0       2 |
        |  1      1     10          0       2 |
        |-------------------------------------|
        |  1      1     13          1       3 |
        |  1      1     14          0       3 |
        |  1      1     15          0       3 |
        |  1      1     16          0       3 |
        |-------------------------------------|
        |  2      1      1          1       1 |
        |  2      1      2          0       1 |
        |  2      1      3          0       1 |
        |  2      1      4          0       1 |
        |  2      1      5          0       1 |
        |  2      1      6          0       1 |
        |  2      1      7          0       1 |
        |  2      1      8          0       1 |
        |-------------------------------------|
        |  2      1     24          1       2 |
        |  2      1     25          0       2 |
        |  2      1     26          0       2 |
        |-------------------------------------|
        |  2      1     31          1       3 |
        |  2      1     32          0       3 |
        |  2      1     33          0       3 |
        |  2      1     34          0       3 |
        |-------------------------------------|
        |  2      1     41          1       4 |
        |  2      1     42          0       4 |
        |-------------------------------------|
        |  2      1     44          1       5 |
        |  2      1     45          0       5 |
        |  2      1     46          0       5 |
        |  2      1     47          0       5 |
        |  2      1     48          0       5 |
        |  2      1     49          0       5 |
        |  2      1     50          0       5 |
        |  2      1     51          0       5 |
        |  2      1     52          0       5 |
        |  2      1     53          0       5 |
        |  2      1     54          0       5 |
        |  2      1     55          0       5 |
        |  2      1     56          0       5 |
        |  2      1     57          0       5 |
        |-------------------------------------|
        |  2      1     60          1       6 |
        |  2      1     61          0       6 |
        |  2      1     62          0       6 |
        |-------------------------------------|
        |  3      1      1          1       1 |
        |  3      1      2          0       1 |
        |  3      1      3          0       1 |
        |  3      1      4          0       1 |
        |  3      1      5          0       1 |
        |  3      1      6          0       1 |
        |  3      1      7          0       1 |
        |  3      1      8          0       1 |
        |  3      1      9          0       1 |
        |  3      1     10          0       1 |
        |  3      1     11          0       1 |
        |  3      1     12          0       1 |
        |  3      1     13          0       1 |
        |  3      1     14          0       1 |
        |  3      1     15          0       1 |
        |  3      1     16          0       1 |
        |-------------------------------------|
        |  3      1     21          1       2 |
        |  3      1     22          0       2 |
        |-------------------------------------|
        |  4      1      1          1       1 |
        |  4      1      2          0       1 |
        |  4      1      3          0       1 |
        |  4      1      4          0       1 |
        |-------------------------------------|
        |  5      1      1          1       1 |
        |-------------------------------------|
        |  7      1      1          1       1 |
        |  7      1      2          0       1 |
        |  7      1      3          0       1 |
        |  7      1      4          0       1 |
        |  7      1      5          0       1 |
        |  7      1      6          0       1 |
        |  7      1      7          0       1 |
        |  7      1      8          0       1 |
        |  7      1      9          0       1 |
        |  7      1     10          0       1 |
        |-------------------------------------|
        |  7      1     45          1       2 |
        |  7      1     46          0       2 |
        |  7      1     47          0       2 |
        |  7      1     48          0       2 |
        |  7      1     49          0       2 |
        |  7      1     50          0       2 |
        |  7      1     51          0       2 |
        |  7      1     52          0       2 |
        |  7      1     53          0       2 |
        |  7      1     54          0       2 |
        |-------------------------------------|
        |  7      1     65          1       3 |
        |  7      1     66          0       3 |
        |-------------------------------------|
        |  7      1     71          1       4 |
        |  7      1     72          0       4 |
        |  7      1     73          0       4 |
        |  7      1     74          0       4 |
        |  7      1     75          0       4 |
        |  7      1     76          0       4 |
        |  7      1     77          0       4 |
        |  7      1     78          0       4 |
        |-------------------------------------|
        |  9      1      1          1       1 |
        +-------------------------------------+

      Comment


      • #4
        Originally posted by William Lisowski View Post
        I think what you are looking for is an indicator of "spells" of consecutive days - each time there's a break in the sequence, or a new id (I'm not sure of the role of drug) you want to start a new sequence.

        There's a good discussion of this by Nick Cox in the Stata Journal paper (currently free to download) at https://www.stata-journal.com/articl...article=dm0079

        Starting from your example data, the code below does what I understand you to want. It's left as an exercise for the reader to combine the two generate commands into one; I chose the wordier version to make it more apparent what is happening.
        [CODE]
        . by id (days), sort: generate newspell = days != days[_n-1]+1

        . by id (days), sort: generate spell = sum(newspell)
        Thanks William, this is what I am looking for. I will also take a look at Nick's discussion.

        Comment

        Working...
        X