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: Identify observations by occupation

 From Nick Cox To statalist@hsphsun2.harvard.edu Subject Re: st: Identify observations by occupation Date Tue, 17 Apr 2012 10:23:09 +0100

```I said

> bysort total_occupation occupation : gen rank = _n == 1
>
> is also
>
> egen rank = tag(total_occupation occupation)

That's a red herring here. Please ignore that bit.

On Tue, Apr 17, 2012 at 10:05 AM, Nick Cox <njcoxstata@gmail.com> wrote:
> You want largest to have rank 1, but need to take account of ties.
> This is a bit shorter. I don't know what -total_je_beruf- is in your
> code.
>
> bysort occupation: egen total_occupation = total(n)
> bysort total_occupation occupation : gen rank = _n == 1
> replace rank = sum(rank)
> replace rank = rank[_N] - rank + 1
>
> bysort total_occupation occupation : gen rank = _n == 1
>
> is also
>
> egen rank = tag(total_occupation occupation)
>
> Nick
>
> On Tue, Apr 17, 2012 at 9:45 AM, Oliver Jones
> <ojones@wiwi.uni-bielefeld.de> wrote:
>
>> I have a dataset containing the number of employed people
>> for 180 cities and 300 occupations.
>>
>> Now I want to generate a variable that shows the ranking
>> of the occupations by the total number of people working
>> in that occupation.
>>
>> I have managed to create this variable. But I bet there is
>> a more elegant and easier way to do it and I hope some one
>> here can show me how or tell me the appropriate command(s).
>>
>> Here is what I have done ...
>>
>> The data looks like this:
>>
>> city  |  occupation  |  n
>> -----------------------------
>> 001   |  nurse       |  1222
>> 002   |  nurse       |   345
>> .     |   .          |     .
>> .     |   .          |     .
>> .     |   .          |     .
>> 180   |  nurse       |    94
>> .............................
>> 001   |  lawyer      |   308
>> 002   |  lawyer      |    55
>> .     |   .          |     .
>> .     |   .          |     .
>> .     |   .          |     .
>> 180   |  lawyer      |    15
>> .............................
>> 001   |  teacher     |   930
>> .     |   .          |     .
>> .     |   .          |     .
>> .     |   .          |     .
>>
>>
>> bysort occupation: egen total_occupation = total(n)
>> bysort total_occupation: gen index = (_n == 1)
>> sort index total_je_beruf
>> bysort index: gen top_occupation = _N + 1 - _n
>> replace top_occupation = . if index == 0
>> sort top_occupation
>>
>> * List the biggest 10 occupations
>> list occupation total_occupation top_occupation if top_occupation <= 10

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