Announcement

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

  • Count repeated values

    I am interested in generating two variable counting how many times a given value is repeated and how many times it is repeated consecutively.

    For example, in the above dataset, I need to count how many times 1 is repeated and repeated consecutively in the variable X, and these countings are placed in the new variables n and rep, respectively.

    id X n rep
    1 0 0 0
    2 1 1 1
    3 0 0 0
    4 1 2 1
    5 0 0 0
    6 1 3 2
    7 1 3 2
    8 0 0 0
    9 1 4 2
    10 1 4 2


    Any ideas or suggestions?
    Huge thanks!

  • #2
    atobias:
    do you mean something along the following lines (please note: the code does not consider missing values)?
    Code:
    . g wanted_1=sum(X) if X==1
    
    
    . replace wanted_1=0 if wanted_1==.
    
    
    . list
    
         +-----------------------------+
         | id   X   n   rep   wanted_1 |
         |-----------------------------|
      1. |  1   0   0     0          0 |
      2. |  2   1   1     1          1 |
      3. |  3   0   0     0          0 |
      4. |  4   1   2     1          2 |
      5. |  5   0   0     0          0 |
         |-----------------------------|
      6. |  6   1   3     2          3 |
      7. |  7   1   3     2          4 |
      8. |  8   0   0     0          0 |
      9. |  9   1   4     2          5 |
     10. | 10   1   4     2          6 |
         +-----------------------------+
    
    . egen wanted_2=group(id X) if X[_n]==1 & X[_n+1]==1
    
    . replace wanted_2=0 if wanted_2==.
    
    . list
    
         +----------------------------------------+
         | id   X   n   rep   wanted_1   wanted_2 |
         |----------------------------------------|
      1. |  1   0   0     0          0          0 |
      2. |  2   1   1     1          1          0 |
      3. |  3   0   0     0          0          0 |
      4. |  4   1   2     1          2          0 |
      5. |  5   0   0     0          0          0 |
         |----------------------------------------|
      6. |  6   1   3     2          3          1 |
      7. |  7   1   3     2          4          0 |
      8. |  8   0   0     0          0          0 |
      9. |  9   1   4     2          5          2 |
     10. | 10   1   4     2          6          0 |
         +----------------------------------------+
    
    .
    Kind regards,
    Carlo
    (Stata 19.0)

    Comment


    • #3
      tsspell from SSC may help.

      Code:
      clear 
      input id X n rep
      1 0 0 0
      2 1 1 1
      3 0 0 0
      4 1 2 1
      5 0 0 0
      6 1 3 2
      7 1 3 2
      8 0 0 0
      9 1 4 2
      10 1 4 2
      end 
      
      tsset id 
      tsspell, cond(X == 1)
      egen REP = max(_seq), by(_spell)
      order id X n _spell rep REP 
      
      list, sepby(_spell)
      
           +-----------------------------------------------+
           | id   X   n   _spell   rep   REP   _seq   _end |
           |-----------------------------------------------|
        1. |  1   0   0        0     0     0      0      0 |
           |-----------------------------------------------|
        2. |  2   1   1        1     1     1      1      1 |
           |-----------------------------------------------|
        3. |  3   0   0        0     0     0      0      0 |
           |-----------------------------------------------|
        4. |  4   1   2        2     1     1      1      1 |
           |-----------------------------------------------|
        5. |  5   0   0        0     0     0      0      0 |
           |-----------------------------------------------|
        6. |  6   1   3        3     2     2      1      0 |
        7. |  7   1   3        3     2     2      2      1 |
           |-----------------------------------------------|
        8. |  8   0   0        0     0     0      0      0 |
           |-----------------------------------------------|
        9. |  9   1   4        4     2     2      1      0 |
       10. | 10   1   4        4     2     2      2      1 |
           +-----------------------------------------------+

      Comment

      Working...
      X