Announcement

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

  • Number of repeats in each block

    Dear helpers
    I couldn't get a good code to count the number of continuous 1s for each ID; something like
    0 0 4 4 4 4 0 1 in 2013-2020 for the 1st id
    2 2 0 0 0 2 2 in 2014-2020 for the 2nd id
    Please help.

    ----------------------- copy starting from the next line -----------------------
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input int id float(yr x)
    1001 2013 0
    1001 2014 0
    1001 2015 1
    1001 2016 1
    1001 2017 1
    1001 2018 1
    1001 2019 0
    1001 2020 1
    1002 2014 1
    1002 2015 1
    1002 2016 0
    1002 2017 0
    1002 2018 0
    1002 2019 1
    1002 2020 1
    end
    ------------------ copy up to and including the previous line ------------------

  • #2
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input int id float(yr x)
    1001 2013 0
    1001 2014 0
    1001 2015 1
    1001 2016 1
    1001 2017 1
    1001 2018 1
    1001 2019 0
    1001 2020 1
    1002 2014 1
    1002 2015 1
    1002 2016 0
    1002 2017 0
    1002 2018 0
    1002 2019 1
    1002 2020 1
    end
    
    bysort id: egen count = total(x)
    
    list
    Code:
    . list
    
         +-------------------------+
         |   id     yr   x   count |
         |-------------------------|
      1. | 1001   2013   0       5 |
      2. | 1001   2014   0       5 |
      3. | 1001   2015   1       5 |
      4. | 1001   2016   1       5 |
      5. | 1001   2017   1       5 |
         |-------------------------|
      6. | 1001   2018   1       5 |
      7. | 1001   2019   0       5 |
      8. | 1001   2020   1       5 |
      9. | 1002   2014   1       4 |
     10. | 1002   2015   1       4 |
         |-------------------------|
     11. | 1002   2016   0       4 |
     12. | 1002   2017   0       4 |
     13. | 1002   2018   0       4 |
     14. | 1002   2019   1       4 |
     15. | 1002   2020   1       4 |
         +-------------------------+
    Edit: I'm sorry, that's not what you were asking for. I'll think about it, and I will respond with the correct answer unless someone else responds faster.
    Last edited by Daniel Schaefer; 07 Sep 2023, 20:14.

    Comment


    • #3
      Thanks, Daniel, but that's not what I wanted.
      I wanted
      id yr x y
      1001 2013 0 0
      1001 2014 0 0
      1001 2015 1 4
      1001 2016 1 4
      1001 2017 1 4
      1001 2018 1 4
      1001 2019 0 0
      1001 2020 1 1

      Comment


      • #4
        Okay, I think this works:

        Code:
        * Example generated by -dataex-. For more info, type help dataex
        clear
        input int id float(yr x)
        1001 2013 0
        1001 2014 0
        1001 2015 1
        1001 2016 1
        1001 2017 1
        1001 2018 1
        1001 2019 0
        1001 2020 1
        1002 2014 1
        1002 2015 1
        1002 2016 0
        1002 2017 0
        1002 2018 0
        1002 2019 1
        1002 2020 1
        end
        
        gen indicator = x[_n -1] == 0 & x == 1
        gen sum = sum(indicator)
        egen group = group(id sum) if x != 0
        bysort group: gen wanted = _N if !missing(group)
        replace wanted = 0 if missing(wanted)
        sort id yr
        drop indicator sum group
        
        list
        Code:
        . list
        
             +--------------------------+
             |   id     yr   x   wanted |
             |--------------------------|
          1. | 1001   2013   0        0 |
          2. | 1001   2014   0        0 |
          3. | 1001   2015   1        4 |
          4. | 1001   2016   1        4 |
          5. | 1001   2017   1        4 |
             |--------------------------|
          6. | 1001   2018   1        4 |
          7. | 1001   2019   0        0 |
          8. | 1001   2020   1        1 |
          9. | 1002   2014   1        2 |
         10. | 1002   2015   1        2 |
             |--------------------------|
         11. | 1002   2016   0        0 |
         12. | 1002   2017   0        0 |
         13. | 1002   2018   0        0 |
         14. | 1002   2019   1        2 |
         15. | 1002   2020   1        2 |
             +--------------------------+

        Comment


        • #5
          Hi Daniel, great! That's exactly what I wanted! Short & nice!
          Thank you so much for the help!

          Comment


          • #6
            Here's another way to do it using tsspell from SSC.

            For the principles, see https://journals.sagepub.com/doi/pdf...867X0700700209

            For examples of the practice, tsspell is an otherwise unpredictable search term for this forum.

            Code:
            * Example generated by -dataex-. For more info, type help dataex
            clear
            input int id float(yr x)
            1001 2013 0
            1001 2014 0
            1001 2015 1
            1001 2016 1
            1001 2017 1
            1001 2018 1
            1001 2019 0
            1001 2020 1
            1002 2014 1
            1002 2015 1
            1002 2016 0
            1002 2017 0
            1002 2018 0
            1002 2019 1
            1002 2020 1
            end
            
            tsset id yr 
            
            * ssc install tsspell 
            tsspell, cond(x == 1)
            
            egen length = max(_seq), by(id _spell) 
            
            list, sepby(id _spell)
            
               +-------------------------------------------------+
                 |   id     yr   x   _seq   _spell   _end   length |
                 |-------------------------------------------------|
              1. | 1001   2013   0      0        0      0        0 |
              2. | 1001   2014   0      0        0      0        0 |
                 |-------------------------------------------------|
              3. | 1001   2015   1      1        1      0        4 |
              4. | 1001   2016   1      2        1      0        4 |
              5. | 1001   2017   1      3        1      0        4 |
              6. | 1001   2018   1      4        1      1        4 |
                 |-------------------------------------------------|
              7. | 1001   2019   0      0        0      0        0 |
                 |-------------------------------------------------|
              8. | 1001   2020   1      1        2      1        1 |
                 |-------------------------------------------------|
              9. | 1002   2014   1      1        1      0        2 |
             10. | 1002   2015   1      2        1      1        2 |
                 |-------------------------------------------------|
             11. | 1002   2016   0      0        0      0        0 |
             12. | 1002   2017   0      0        0      0        0 |
             13. | 1002   2018   0      0        0      0        0 |
                 |-------------------------------------------------|
             14. | 1002   2019   1      1        2      0        2 |
             15. | 1002   2020   1      2        2      1        2 |
                 +-------------------------------------------------+

            Comment

            Working...
            X