title: Stata: Generate sum / total by specific date ranges and save them as a new variable
body: <p>I work with panel data which contain several companies (<code>id</code>) and cover the period from 1.1.2008 to 1.1.2013 (<code>date</code>, <code>year</code>). I want to generate new variable (<code>sum1</code>) which contains a sum of daily observation for <code>var1</code> for each company and specific time interval. If the interval was equal to each year I would use the function <code>total()</code>:</p>
  
  <pre><code> bysort id year: egen sum1=total(var1)
  </code></pre>
  
  <p>In my case however, the time interval  is determined as an interval between two events. I have a special variable called <code>event</code>, which takes value of 1 if the event has occurred on special date and missing otherwise. There are 5 to 10 events for each company. The intervals between events are not equal; hence the first interval can contain 60 observations, the next interval 360 observations. The intervals are also not equal for different companies. The starting date for the first interval for each company is 1.1.2008. The starting date for the second interval is the date of the first event + 1 day. Besides I would like to account for missing values, so if all values of <code>var1</code> for the company x are missing variable, <code>sum1</code> for company x and specific interval must contain missing values and not 0.</p>
  
  <p>My panel looks like this:</p>
  
  <pre><code>   id   date        year    var1    event  sum1(to gen)  event_id(to gen)
     1    1.1.2008    2008    25        .     95 (25+30+40)     1
     1    2.1.2008    2008    30        .     95 (25+30+40)     1
     ...........................................................1      
     1    31.4.2008   2008    40        1     95 (25+30+40)     1
     1    1.5.2008    2008    50        .     160 (50+50+60)    2
     1    2.5.2008    2008    50        .     160 (50+50+60)    2
     .........................................  ................2
     1   31.4.2009    2009    60        1     160 (50+50+60)    2 
     2    1.1.2008    2008    26        .     96 (26+30+40)     1 
     2    2.1.2008    2008    30        .     96 (26+30+40)     1
     ...........................................................1      
     2    31.6.2008   2008    40        1     96 (26+30+40)     1
     2    1.5.2008    2008    51        .     161 (51+50+60)    2
     2    2.5.2008    2008    50        .     161 (51+50+60)    2
     ...........................................................2
     2   31.6.2009    2009    60        1     161 (51+50+60)    2  
  </code></pre>
  
  <p>I tried to write different loops (<code>while</code>, <code>if</code>), but I failed to do it correctly. I cannot use <code>rolling</code> as my intervals are not the same. </p>
  
  <p>My other idea was to create the group identifier first (called <code>event_id</code>), which contains the <code>event_id</code> for each interval and each company. Then I could use <code>bysort id event_id: egen sum1=total(var1)</code>, but unfortunately I do not have any idea how to do that. So, the variables <code>event_id</code> and <code>sum1</code> in my panel do not exist and serve as an example for output I want to achieve. </p>