Stata - egen, bysort, collapse

28 Views Asked by At

I have this problem: Find the university with the largest number of students aged 12 years old. What is the number of 12 years old students in this university?

and I am using this code:

keep if age==12
by id_uni, sort: gen new_var = _N
gsort -new_var

Is there a better way of doing this in Stata?

1

There are 1 best solutions below

0
Nick Cox On BEST ANSWER

Here is another way to do it that is not destructive.

bysort id_uni : egen count = total(age == 12)
sort count 

di id_uni[_N] " " count[_N] 

In principle you should check that the highest value occurs for only one university.

I am alarmed at the thought of any university with students 12 years old.