Hi all,

I am currently using Stata Version 14. In the sample data attached below, I have information on the gender, rank, area of residence and also the year of residence for individuals. Data is also sorted by the rank i.e. rank 1 being the top rank, followed by rank 2 and so on.

In the data attached, if variable male=0, then the individual is a female and if male=1, then the person is a male. What I would like to do is, identify the number of males and females above their rank order (i.e. rank 1 is above/better than rank 2 and so on), only for females. For ex, in area B, person of rank 2 is a female. Number of males above this individual's rank is 1 (as the person of rank 1 is a male) and the number of females above their rank is zero. Likewise, for area B again, person of rank 5 is a female. Number of males above their rank order is 3 and number of females above their rank is 1.

Any suggestions on how to execute this using code would be helpful.

Thanks.


Code:
* Example generated by -dataex-. To install: ssc install dataex
clear
input str29 area float(year rank male males_above females_above female)
"A" 1978 1 1 . . 0
"A" 1978 2 1 . . 0
"A" 1978 3 0 2 0 1
"A" 1978 4 1 . . 0
"A" 1978 5 0 3 1 1
"B" 1983 1 1 . . 0
"B" 1983 2 0 1 0 1
"B" 1983 3 1 . . 0
"B" 1983 4 1 . . 0
"B" 1983 5 0 3 1 1
end