user2331149
user2331149

Reputation: 1

How can I rank observations within groups in Stata in descending order?

I have district level data grouped within town level data with voter turnout. I want to regroup or rank the towns with highest voter turnout in each district.

Upvotes: 0

Views: 20207

Answers (1)

Nick Cox
Nick Cox

Reputation: 37208

To rank in descending order, just negate the variable (or more generally the expression) on which you are ranking.

 sysuse auto 
 egen rank = rank(-mpg) 

Stata's default is to rank in ascending order. But a minus sign is sufficient to reverse that.

In your case, it sounds like

 egen rank = rank(-turnout), by(district) 

Upvotes: 7

Related Questions