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: Identify observations by occupation
From
Nick Cox <[email protected]>
To
[email protected]
Subject
Re: st: Identify observations by occupation
Date
Tue, 17 Apr 2012 10:05:44 +0100
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
<[email protected]> 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/