I would like to get the running sum over j, e.g. for row _n and j=3 I would like to get x[_n] + x[_n+1] + x[_n+2].
I thought to do the following trick:
by grouping: gen foo = sum(log(x))
by grouping: replace foo = foo - foo[_n - `y'] if _n > `y'
However, this is not robust to missing values. sum() generates 0 for a missing value, while I need the cumulative sums to be . if at least one of the values to be summed over is .. How would I achieve that?
I need it to be working for large j too, so manually adding these up is not feasible.
sum()ignores missing values. Its result will be non-zero if the non-missing values have cumulative sum non-zero. - Nick Cox