title: How to correctly use egen mean function for computing average differences of the time series?
body: <p>I have the panel data in the format:
      | transaction_id | city_id | week | tr_cw |
  , where tr_cw is some metric computed for each city for each week.</p>
  
  <p>I'm trying to compute the mean growth of the metric for each city using egen:</p>
  
  <pre><code>egen tag_cw = tag(city_id week)
  sort tag_cw city_id week
  by tag_cw city_id: egen tr_speed = mean(tr_cw[_n]-(tr_cw[_n-1])) if tag_cw
  </code></pre>
  
  <p>However, the result is different from a bit more complicated computation (which numerically looks more valid):</p>
  
  <pre><code>by tag_cw city_id: g _tr_n = tr_cw[_n] if tag_cw
  by tag_cw city_id: g _tr_n1 = tr_cw[_n-1] if tag_cw
  by tag_cw city_id: egen tr_speed2 = mean(_tr_n-_tr_n1)
  </code></pre>
  
  <p>What am I missing in using egen? Is there a good tutorial to find out about the correct usage?</p>