title: How to do an X-month rolling sum while accounting for missing values
body: <p>I have monthly returns data, <code>Return</code>, a date variable, <code>Price_Date</code>, and a firm identification variable, <code>Firm_ID</code>.</p>
  
  <p>There are several years of returns data for multiple firms.</p>
  
  <p>Some months are missing entirely (no values for any variables including the date).</p>
  
  <p>Alternatively for some of the months that do exist (i.e. the date variable exists) the <code>return</code> variable and/or the <code>Firm_ID</code> variable have missing values (i.e. <code>.</code>).</p>
  
  <p>I want to calculate a new variable that is the rolling X-month (e.g. 3 months, 12 months) sum of the returns (consecutive months).</p>
  
  <p>The new variable, Rolling_Sum, should equal the current month's return plus the previous (X-1) months returns (for a total of X months of returns).</p>
  
  <p>However I also want to take into account missing observations such that when any of the returns in any particular month's rolling sum are missing then the rolling sum for that month should return a missing value also.</p>
  
  <p>How can I achieve this?</p>
  
  <p>For example if I am finding the 3 month rolling return then the data for the variables, Date, Firm_ID, Returns, and Rolling_Sum might look as follows. </p>
  
  <pre><code>31JAN2014 FirmA 10 .
  28Feb2014 FirmA 11 .
  31Mar2014 FirmA 12 33
  30Apr2014 FirmA . .
  31May2013 FirmA 10 .
  30Jun2014 FirmA 10 .
  31Aug2014 FirmA 12 .
  30Sep2014 FirmA 11 .
  31Oct2014 FirmA 50 73
  31Jan2014 FirmB 20 .
  28Feb2014 FirmB 70 .
  31Mar2014 FirmB 40 130
  </code></pre>
  
  <p>Notice that if a value is missing within the 3 month sum then the Rolling_Sum returns as missing (i.e ".") also (e.g. lines 1 &amp; 2, and 4, 5 &amp; 6 of the data). In addition, if an observation is missing then a consecutive 3 month rolling sum cannot be obtained so I would get a missing value as output also (e.g. lines 7 and 8 since July is missing). 
  That is the kind of ideal output I am after. </p>
  
  <p>Edit: All returns are already calculated as returns for the whole month.</p>
  
  <p>Clarification: I am after the rolling sum for consecutive months not consecutive observations. </p>