title: Stata: Running sum with missing values
body: <p>I would like to get the running sum over <code>j</code>, e.g. for row <code>_n</code> and <code>j=3</code> I would like to get <code>x[_n] + x[_n+1] + x[_n+2]</code>.</p>
  
  <p>I thought to do the following trick:</p>
  
  <pre><code>    by grouping: gen foo = sum(log(x))
      by grouping: replace foo = foo - foo[_n - `y'] if _n &gt; `y'
  </code></pre>
  
  <p>However, this is not robust to missing values. <code>sum()</code> generates <code>0</code> for a missing value, while I need the cumulative sums to be <code>.</code> if at least one of the values to be summed over is <code>.</code>. How would I achieve that?</p>
  
  <p>I need it to be working for large <code>j</code> too, so manually adding these up is not feasible.</p>