title: Adding observations to panel in Stata
body: <p>I have a panel data from year <code>t1</code> to <code>t2</code>. Some individuals enter the sample after <code>t1</code> and/or exit the sample before <code>t2</code>. For efficiency (large sample), <strong>the dataset only contains rows for years when individuals are observed.</strong></p>
  
  <p>I want to add a new observation per individual, containing the <strong>year after</strong> an individual left the sample. So, if someone left in, say 2003, I want the new observation to contain the individual's id and the value <code>2004</code> in the year variable. Every other variable in that observation should be missing.</p>
  
  <p>This is my approach, using a sample dataset:</p>
  
  <pre><code>webuse nlswork, clear
  
  * Here goes plenty of lines of codes modifying the dataset ... for generality *
  
  timer on 1
  
  preserve
  keep id year
  bysort id (year) : keep if _n == _N
  replace year = year + 1
  save temp.dta, replace
  restore
  
  append using temp.dta
  sort id year
  erase temp.dta
  
  timer off 1
  timer list 
  </code></pre>
  
  <p>I think this might be a bit inefficient, as it includes a preserve/restore, saving/deleting an additional database, and an append, all relatively time-consuming actions. Something like <code>tsfill, last</code> would be amazing, but that option doesn't exist. Is anyone aware of a <strong>more efficient</strong> method? The code above includes timer, so anyone can benchmark it against another method.</p>