Dear Statalist Community,

due to my thesis I am allowed to work with stata. My dataset comes from a Panelsurvey over several time periods, with round about 30000 individuals. It is not balanced. I got an issue where I am confronted with gender variance within the individual.
The problem here is not transsexuality, it proabably comes most of the times from misstakes. I think so, because the gender changes once in the middle of the individual survey period.

My nearest approach to solve my problem is the following:

bysort pid: gen syear_count = _N

gen gender_incons = 0
bysort pid (syear): replace gender_incons = 1 if gender != gender[_n-1] & gender != gender[_n+1] & syear_count >= 3


Here I at least detected those individuals, that change their gender several times. I have no clue how to tell stata how to fix this..

I think it needs to be something like:

bysort pid (syear): replace gender = gender[_n-1] if syear_count == 2

But here stata says "weights are not allowed".

I would also be very thankful about a maximum relative frequency command within the individuals gender. Something like replace gender = the maximum frequent gender value within the individual.

Can someone help me with my problem? I am getting more and more desperate.

Kind regards,
Moritz