Statalist


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

Re: st: Ranking observations within groups


From   "James Cross" <[email protected]>
To   [email protected]
Subject   Re: st: Ranking observations within groups
Date   Thu, 11 Dec 2008 21:48:20 +0000

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!

James

2008/12/11 Nick Cox <[email protected]>:
> 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.
>
> Nick
> [email protected]
>
> James Cross
>
> Great, thanks!
>
> 2008/12/11 David Airey <[email protected]>:
>
>> 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
> 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/
>
>



-- 
Best Regards,

James Cross
*
*   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