title: How to efficiently create lag variable using Stata
body: <p>I have panel data (<strong>time:</strong> date, <strong>name:</strong> ticker). I want to create 10 lags for variables <code>x</code> and <code>y</code>. Now I create each lag variable one by one using the following code:</p>
  
  <pre><code>by ticker: gen lag1 = x[_n-1]
  </code></pre>
  
  <p>However, this looks messy. </p>
  
  <p>Can anyone tell me how can I create lag variables more efficiently, please? </p>
  
  <p>Shall I use a loop or does Stata have a more efficient way of handling this kind of problem?</p>