Announcement

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

  • How to create a variable representing the max length of consecutively same numbers in a row?

    I am trying to find a way to caculate the max length of consectively same numbers in each row . For example, let's look at the data below

    Code:
    clear all
    input str1(id) byte(a1) byte(b2) byte(c1) byte(f4) byte(m1) byte(m2) byte(t3)
    a 2 2 2 2 3 3 3
    b 5 5 1 2 3 4 5
    c 1 1 1 1 1 1 1
    d 5 4 5 6 4 5 3
    e 1 1 2 2 2 2 1
    f 7 7 7 1 1 1 1
    g 2 2 2 2 2 2 1
    h 3 3 3 1 1 1 2
    end
    I would like to generate a variable called 'maxlength'. Idealy, it would be like the following.

    Code:
    id    a1    b2    c1    f4    m1    m2    t3    maxlength
    a    2    2    2    2    3    3    3    4
    b    5    5    1    2    3    4    5    2
    c    1    1    1    1    1    1    1    7
    d    5    4    5    6    4    5    3    1
    e    1    1    2    2    2    2    1    4
    f    7    7    7    1    1    1    1    4
    g    2    2    2    2    2    2    1    6
    h    3    3    3    1    1    1    2    3
    Could we do so in Stata?

  • #2
    This kind of calculation is easier after a reshape long. See also trickery for working with spells or runs at https://www.stata-journal.com/articl...article=dm0029

    Code:
    clear all
    input str1(id) byte(a1) byte(b2) byte(c1) byte(f4) byte(m1) byte(m2) byte(t3)
    a 2 2 2 2 3 3 3
    b 5 5 1 2 3 4 5
    c 1 1 1 1 1 1 1
    d 5 4 5 6 4 5 3
    e 1 1 2 2 2 2 1
    f 7 7 7 1 1 1 1
    g 2 2 2 2 2 2 1
    h 3 3 3 1 1 1 2
    end
    
    unab labels : a1-t3 
    rename (a1-t3) (y#), addnumber
    reshape long y, i(id) j(t)
    
    su t, meanonly 
    tokenize "`labels'"
    forval j = 1/`r(max)' { 
        label define t `j' "``j''", add 
    }
    
    label val t t 
    
    list, sepby(id)
    
    bysort id (t) : gen spell = sum(y != y[_n-1])
    bysort id spell (t) : gen seq = _n 
    egen max = max(seq), by(id)
    
    list, sepby(id)

    Code:
         +---------------------------------+
         | id    t   y   spell   seq   max |
         |---------------------------------|
      1. |  a   a1   2       1     1     4 |
      2. |  a   b2   2       1     2     4 |
      3. |  a   c1   2       1     3     4 |
      4. |  a   f4   2       1     4     4 |
      5. |  a   m1   3       2     1     4 |
      6. |  a   m2   3       2     2     4 |
      7. |  a   t3   3       2     3     4 |
         |---------------------------------|
      8. |  b   a1   5       1     1     2 |
      9. |  b   b2   5       1     2     2 |
     10. |  b   c1   1       2     1     2 |
     11. |  b   f4   2       3     1     2 |
     12. |  b   m1   3       4     1     2 |
     13. |  b   m2   4       5     1     2 |
     14. |  b   t3   5       6     1     2 |
         |---------------------------------|
     15. |  c   a1   1       1     1     7 |
     16. |  c   b2   1       1     2     7 |
     17. |  c   c1   1       1     3     7 |
     18. |  c   f4   1       1     4     7 |
     19. |  c   m1   1       1     5     7 |
     20. |  c   m2   1       1     6     7 |
     21. |  c   t3   1       1     7     7 |
         |---------------------------------|
     22. |  d   a1   5       1     1     1 |
     23. |  d   b2   4       2     1     1 |
     24. |  d   c1   5       3     1     1 |
     25. |  d   f4   6       4     1     1 |
     26. |  d   m1   4       5     1     1 |
     27. |  d   m2   5       6     1     1 |
     28. |  d   t3   3       7     1     1 |
         |---------------------------------|
     29. |  e   a1   1       1     1     4 |
     30. |  e   b2   1       1     2     4 |
     31. |  e   c1   2       2     1     4 |
     32. |  e   f4   2       2     2     4 |
     33. |  e   m1   2       2     3     4 |
     34. |  e   m2   2       2     4     4 |
     35. |  e   t3   1       3     1     4 |
         |---------------------------------|
     36. |  f   a1   7       1     1     4 |
     37. |  f   b2   7       1     2     4 |
     38. |  f   c1   7       1     3     4 |
     39. |  f   f4   1       2     1     4 |
     40. |  f   m1   1       2     2     4 |
     41. |  f   m2   1       2     3     4 |
     42. |  f   t3   1       2     4     4 |
         |---------------------------------|
     43. |  g   a1   2       1     1     6 |
     44. |  g   b2   2       1     2     6 |
     45. |  g   c1   2       1     3     6 |
     46. |  g   f4   2       1     4     6 |
     47. |  g   m1   2       1     5     6 |
     48. |  g   m2   2       1     6     6 |
     49. |  g   t3   1       2     1     6 |
         |---------------------------------|
     50. |  h   a1   3       1     1     3 |
     51. |  h   b2   3       1     2     3 |
     52. |  h   c1   3       1     3     3 |
     53. |  h   f4   1       2     1     3 |
     54. |  h   m1   1       2     2     3 |
     55. |  h   m2   1       2     3     3 |
     56. |  h   t3   2       3     1     3 |
         +---------------------------------+

    Comment

    Working...
    X