title: Assigning rank to a variable based on 2 other variables in stata
body: <p>I have a longitudinal data with some variables as: <code>firm id</code>, <code>event date</code>, <code>abnormal return</code>, where <code>event date</code> is acquisition announcement date by each firm and <code>abnormal return</code> is the event-announcement return for each firm over a window of 5 days (-2,+2). Here each sample firm engages in multiple acquisitions over a pre-defined period. </p>
  
  <p>For e.g., I have 200 firms, each having multiple abnormal return-observations for the period 1999-2011. Therefore, each firm has at least 2 such observations during this period, computed over 2 such event dates.</p>
  
  <p>I need to assign a rank to the abnormal returns for each firm so as to rank the subsequent deals (after the first acquisition by a firm) in order of its event dates. So, I want to create a new <code>rank</code> variable and use it for further analysis of a firm’s increase/decrease in each subsequent abnormal return after the first one.</p>
  
  <p>Please suggest how can I perform this in Stata or what codes to use?
  I tried with: </p>
  
  <pre><code>bysort firm_id event_date: egen rank = rank(abnormal_return), 
  </code></pre>
  
  <p>but I’m not getting what I want.</p>
  
  <p>N.B. – a) In my sample, there are few firms which perform more than 1 deal (say, 2) on the same event date, so they have exactly same abnormal returns for those 2 deals;
  b) Abnormal returns are either positive or negative and in 6/7 places of decimal, for e.g. a negative abnormal return looks like, -0.0365089; a positive one, 0.0416888.</p>
  
  <p>I'm sorry I've already posted this query in statalist, which I ought to have mentioned in the post, but somehow in a hurry to get feedback, I missed it - sincere apologies.</p>
  
  <p>(UPDATE)
  I've finally managed to share the dropbox link of a snapshot of my dta. file.</p>
  
  <p><a href="https://www.dropbox.com/s/w501upimdgwvzyz/Rank.dta?dl=0" rel="nofollow">https://www.dropbox.com/s/w501upimdgwvzyz/Rank.dta?dl=0</a></p>
  
  <p>The query i have now (which hasn't been answered at statalist yet &amp; i'm trying to do an analysis on the basis of this, hence re-posting here):</p>
  
  <p>If I am looking at a window of 3 years, i.e., I need to re-rank the abnormal returns on the basis that the subsequent deals had been made in a span of 3 years from the first deal. For e.g., a firm in my dta. file with id = 13 has 4 deals in years 2000, 2001, 2005 and 2006, respectively. The first deal is in year 2000. The 3rd acquisition in year 2005, which is more than 3 years from the focal deal in year 2000, hence shouldn’t be counted or ranked from the 1st deal in year 2000, rather year 2005 will be the new 1st deal for this firm for another span of 3 years. So, in this case, I shall have to rank the deals by firm_id, 13 as 0 (2000), 1(2001); 0 (2005), 1 (2006).</p>
  
  <p>Can anybody please help with the re-coding here?</p>