[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: st: Ranking observations within groups

From   "Nick Cox" <>
To   <>
Subject   RE: st: Ranking observations within groups
Date   Thu, 11 Dec 2008 19:28:02 -0000

Despite your positive reply, I don't believe that is what you want. 

webuse machine 
bysort machine (output) : gen rank = output != output[_n-1]
by machine : replace rank = sum(rank)

seems to be what you are asking for. 


James Cross

Great, thanks!

2008/12/11 David Airey <>:

> Seems the unique option does what you describe you need.
> clear
> webuse machine.dta
> drop operator
> bysort machine: egen rank = rank(output)
> bysort machine: egen rankf = rank(output), field
> bysort machine: egen rankt = rank(output), track
> bysort machine: egen ranku = rank(output), unique
> sort machine output
> list, clean

> On Dec 11, 2008, at 9:04 AM, James Cross wrote:

>> I have a variable that represents different actors positions on
>> different issues (groups). So I have around 30 positions between 0
>> 100 for each individual issue within the variable with quite a few
>> ties. I want to create a variable that ranks these observations in
>> ascending order so that the lowest observation(s) will have a value
>> 1 in the new variable, the second lowest observation(s) will have a
>> value of 2 and so on.
>> I have tried -by issue_var: egen newvar = rank(oldvar), track- but
>> this returns a newvar in which the lowest variable is given 1st
>> position, which is fine, but if there are say 3 ties for position 1,
>> the second lowest oldvar is ranked in 4th position rather than 2nd
>> position which is what I need. Any ideas as to how I could create a
>> newvar where ties do not change the rank position of following
>> observations within each group?

*   For searches and help try:

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