Announcement

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

  • cumulative sum betwee 1 and 2?

    Dear All, I find this question here (in Chinese). The data set is:
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input float(A B C)
    0 0  0
    1 2  2
    0 4  6
    0 5 11
    0 3 14
    2 0  0
    0 0  0
    0 0  0
    1 3  3
    0 6  9
    0 7 16
    2 0  0
    end
    A and B are the raw data, and C is the desired result. The purpose is to calculate the cumulative sum of B, starting from A=1 to A=2 for each segment. Any suggestions are highly appreciated.
    Ho-Chuan (River) Huang
    Stata 17.0, MP(4)

  • #2
    Code:
    clear
    input byte(A B C)
    0 0  0
    1 2  2
    0 4  6
    0 5 11
    0 3 14
    2 0  0
    0 0  0
    0 0  0
    1 3  3
    0 6  9
    0 7 16
    2 0  0
    end
    
    gen order = _n
    gen id = sum(A==1) if sum(A==1) != sum(A==2)
    bys id (order) : gen wanted = sum(B) if !missing(id)
    sort order
    list, sepby(id)
    ---------------------------------
    Maarten L. Buis
    University of Konstanz
    Department of history and sociology
    box 40
    78457 Konstanz
    Germany
    http://www.maartenbuis.nl
    ---------------------------------

    Comment


    • #3
      Here's another way to think about it, which will extend fairly painlessly if #1 is really a stalking-horse for a question on panel data.. tsspell is from SSC.

      Code:
      clear
      input byte(A B C)
      0 0  0
      1 2  2
      0 4  6
      0 5 11
      0 3 14
      2 0  0
      0 0  0
      0 0  0
      1 3  3
      0 6  9
      0 7 16
      2 0  0
      end
      
      gen t = _n 
      tsset t 
      
      tsspell, fcond(A > 0) 
      
      bysort _spell (_seq) : gen wanted = sum(B) if A[1] == 1 
      
      sort t 
      
      list, sepby(_spell)  
      
           +-------------------------------------------------+
           | A   B    C    t   _spell   _seq   _end   wanted |
           |-------------------------------------------------|
        1. | 0   0    0    1        0      0      0        . |
           |-------------------------------------------------|
        2. | 1   2    2    2        1      1      0        2 |
        3. | 0   4    6    3        1      2      0        6 |
        4. | 0   5   11    4        1      3      0       11 |
        5. | 0   3   14    5        1      4      1       14 |
           |-------------------------------------------------|
        6. | 2   0    0    6        2      1      0        . |
        7. | 0   0    0    7        2      2      0        . |
        8. | 0   0    0    8        2      3      1        . |
           |-------------------------------------------------|
        9. | 1   3    3    9        3      1      0        3 |
       10. | 0   6    9   10        3      2      0        9 |
       11. | 0   7   16   11        3      3      1       16 |
           |-------------------------------------------------|
       12. | 2   0    0   12        4      1      1        . |
           +-------------------------------------------------+

      Comment


      • #4
        Dear Maarten, Thanks for this helpful suggestion.
        Ho-Chuan (River) Huang
        Stata 17.0, MP(4)

        Comment


        • #5
          Dear Nick, Thanks for this helpful suggestion.
          Ho-Chuan (River) Huang
          Stata 17.0, MP(4)

          Comment

          Working...
          X