Statalist


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

RE: st: Ranking observations within groups


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   RE: st: Ranking observations within groups
Date   Thu, 11 Dec 2008 22:37:06 -0000

Thanks. In fact the code is too long. 

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

Nick 
[email protected] 

James Cross

I realised this afterwards, I got around the problem by ranking then
deleting the lowest observation from the variable in question and
continuing until all observations were ranked. It seems your solution
is much more elegant however, thank you!

Nick Cox

> 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!

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

James Cross 

>>> I have a variable that represents different actors positions on
>>> different issues (groups). So I have around 30 positions between 0
> and
>>> 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
> of
>>> 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:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



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