title: Creating a "period" variable from a date variable in Stata
body: <p>I have panel data (unbalanced) with id and date to determine the two dimensions. Ids are integer numbers from 1-449. Dates are quarters from 2004Q1 to 2012Q4. Is it possible to create a new variable t which is conditioned to the date-vector? </p>
  
  <p>For example:</p>
  
  <p><strong>2004Q1 => 1</strong></p>
  
  <p><strong>2004Q2 => 2</strong></p>
  
  <p>and so on.</p>
  
  <p>For a balanced panel that is no problem, but how can i make it for an unbalanced panel?</p>
  
  <p>I have tried:</p>
  
  <pre><code>sort company_id date 
  by company_id: gen t = _n 
  </code></pre>
  
  <p>which actually counts the observations. Problem is, for example, <code>company_id = 4</code> does not start in 2004Q1 but in 2005Q1, so the corresonding number should not be 1 but 5.</p>