Announcement

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

  • nonconsecutive, cumulative sum?

    Dear All, Suppose that I have the following data:
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input float(id year x y)
    1 1968 1 1
    1 1969 1 2
    1 1970 0 0
    1 1971 1 1
    1 1972 1 2
    1 1973 1 3
    2 1968 0 0
    2 1969 1 1
    2 1970 1 2
    end
    For each id, I'd like to calculate the cumulative sum of `x' and call it variable `y'. The trick is, whenever we encounter x=0, the cumulative sum restarts from 1.
    Ho-Chuan (River) Huang
    Stata 17.0, MP(4)

  • #2
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input float(id year x y)
    1 1968 1 1
    1 1969 1 2
    1 1970 0 0
    1 1971 1 1
    1 1972 1 2
    1 1973 1 3
    2 1968 0 0
    2 1969 1 1
    2 1970 1 2
    end
    
    * method 1
    
    gen runsum = .
    bysort id (year) : replace runsum = cond(_n == 1, x, cond(x == 0, 0, runsum[_n-1] + x))
    
    
    * method 2
    
    tsset id year
    * ssc inst tsspell to use -tsspell-
    
    tsspell, fcond(missing(L.year) | x == 0)
    
    bysort id _spell (_seq) : gen runsum2 = sum(x)
    
    list, sepby(id)
    EDIT This version of method 1 may seem simpler:


    Code:
    gen runsum = x
    bysort id (year) : replace runsum = cond(x == 0, 0, runsum[_n-1] + x) if _n > 1
    Last edited by Nick Cox; 26 Jun 2017, 05:40.

    Comment


    • #3
      Nick, many thanks for the suggestions.

      Ho-Chuan (River) Huang
      Stata 17.0, MP(4)

      Comment

      Working...
      X