Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: st: egen nth highest or lowest value
From
Nick Cox <[email protected]>
To
"[email protected]" <[email protected]>
Subject
Re: st: egen nth highest or lowest value
Date
Thu, 11 Apr 2013 09:31:02 +0100
This is clearly programmable with your own program (e.g. defining an
-egen- function), but here is a more systematic way of approaching it,
just with canned code.
. sysuse auto, clear
(1978 Automobile Data)
. egen rank_lo = rank(mpg) , by(rep78) unique
. egen rank_hi = rank(-mpg) , by(rep78) unique
. egen lo2 = total(mpg / (rank_lo == 2)), by(rep78)
. egen hi2 = total(mpg / (rank_hi == 2)), by(rep78)
. egen min = min(mpg), by(rep78)
. egen max = max(mpg), by(rep78)
. tabdisp rep78, c(min lo2 hi2 max)
----------------------------------------------------------
Repair |
Record |
1978 | min lo2 hi2 max
----------+-----------------------------------------------
1 | 18 24 18 24
2 | 14 16 24 24
3 | 12 12 28 29
4 | 14 14 28 30
5 | 17 17 35 41
. | 14 19 26 26
----------------------------------------------------------
Note that the results for -rep78 == 1- are correct. There are just two
values, so the second largest is the minimum and the second smallest
is the maximum.
Nick
[email protected]
On 11 April 2013 08:42, "Bartus Tamás" <[email protected]> wrote:
> The code for, say, the lowest 5th value of variable x could be
>
> sort x
> gen tempvar = x in 5
> egen newvar = mean(tempvar)
>
> The code for the highest 5th value is
>
> gsort - x
> gen tempvar = x in 5
> egen newvar = mean(tempvar)
On 13/04/11, [email protected] wrote:
>> is that possible to generate a new variable containing the nth lowest or nth highest value of another variable?
*
* For searches and help try:
* http://www.stata.com/help.cgi?search
* http://www.stata.com/support/faqs/resources/statalist-faq/
* http://www.ats.ucla.edu/stat/stata/