[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

RE: st: How to create a rank?

From   "Newson, Roger B" <>
To   <>
Subject   RE: st: How to create a rank?
Date   Tue, 4 Mar 2008 12:51:59 -0000

If you check the entry for -rank()- under -whelp egen-, then you will
find that -rank()- supports multiple definitions of ranks. The default

rank(Y_j) = 0.5 + 0.5*Sum_k(Y_k==Y_j) + Sum_k(Y_k<Y_j)

where Y_j is the value of the variable being ranked in the j'th
observation, rank(Y_j) is its rank, Y_k is the k'th observation, and
Sum_k is the sum over all k from 1 to N, where N is the number of
observations being ranked. This definition implies that tied Y_j values
are given the mean of the ranks that they would have had, if they had
been ranked randomly. This often implies fractional ranks. However,
there are other possibilities on offer. You can decide which one is
right for your purposes.

I hope this helps.


Roger B Newson
Lecturer in Medical Statistics
Respiratory Epidemiology and Public Health Group
National Heart and Lung Institute
Imperial College London
Royal Brompton campus
Room 33, Emmanuel Kaye Building
1B Manresa Road
London SW3 6LR
Tel: +44 (0)20 7352 8121 ext 3381
Fax: +44 (0)20 7351 8322
Web page:
Departmental Web page:

Opinions expressed are those of the author, not of the institution.

-----Original Message-----
[] On Behalf Of Song
Sent: 04 March 2008 03:58
Subject: Re: st: How to create a rank?

Thank you. I solved my problem. By the way, if there exist ties, egen
rank = 
rank(revenue) also creates a problem. In my case, the command produced 
***.5, etc. instead of a whole number.


----- Original Message ----- 
From: "Nick Cox" <>
To: <>
Sent: Monday, March 03, 2008 3:11 AM
Subject: RE: st: How to create a rank?

> As mentioned in my earlier posting, this fixes the reversal but does
> wrong thing in the presence of ties.
> For ranks, use -egen, rank()-. It's as simple as that.
> Nick
> Jeremy Miles
> How about :
> gen revenue_reversed = revenue * -1
> sort revenue_reversed
> egen rank=group(revenue_reversed)
> Song
>>  I am trying to create 'rank' based on total revenues. I used the
> following
>>  command:
>>  sort revenue
>>  egen rank=group(revenue)
>>  The result is that the smallest revenue is 1 and the highest revenue
> is 100,
>>  for example. How can I reverse the rank? I want the highest revenue
> to be
>>  number '1'.
> *
> *   For searches and help try:
> *
> *
> * 

*   For searches and help try:

*   For searches and help try:

© Copyright 1996–2019 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index