Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down on April 23, and its replacement, statalist.org is already up and running.

# Re: st: Dataset of means from the three largest values of a group

 From Nick Cox To statalist@hsphsun2.harvard.edu Subject Re: st: Dataset of means from the three largest values of a group Date Mon, 28 Nov 2011 20:25:21 +0000

```I assume no missing values for -trade-. "3" here evidently means here "up to 3"

bysort country year (trade)  :  gen tag  = (_N - _n) < 3

by country year : egen meanhighest = mean(trade / tag)

On why division by zero can be useful, see

SJ-11-2 dm0055  . . . . . . . . . . . . . .  Speaking Stata: Compared with ...
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  N. J. Cox
Q2/11   SJ 11(2):305--314                                (no commands)
reviews techniques for relating values to values in other
observations

The second highest (silver medallist) is more robust-resistant to outliers:

Nick

On Mon, Nov 28, 2011 at 8:03 PM, Iulian Ihnatov <iulian.ihnatov@uaic.ro> wrote:

> I have the following dataset for the period of 1999 to 2010:
> AFG          1999    USA          12345
> AFG          1999    DEU          9875
> AFG          1999    FRA           25487
> ........................
> AFG          2000    USA           5454
> AFG          2000    HUN          5454
> ........................
> HUN         1999    DEU           58744
> ........................
>
> I need to create a dataset of means of the "trade" variable, grouped by
> country and year, but only for the three largest observations of each group.
> I may use - collapse (mean) trade, by(country year) -, but I don't know how
> to isolate the largest three values from each group (in some years, there
> are only 1 or 2 observations available, in others more than 10). Any help
> would be highly appreciated.

*
*   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/
```