Hello!

I am Nithya. This is my first post, and i've tried to follow the advice on posting. But please excuse if I've missed something.

I have a string variable childgender with values=male,female. I am trying to create two separate variables countmale and countfemale with the number of male and female children in each household (hhid). I am The code i'm using is:

Code:
levelsof childgender, local(gender)
foreach sex of local gender{
    bysort hhid:egen count`sex'=count(childgender) if childgender=="`sex'"
    ta count`sex'
    egen n`sex'=max(count`sex'),by(hhid)
replace n`sex' =0 ==mi(n`sex')
}
The problem i'm having is with the line
Code:
egen n`sex'=max(count`sex'),by(hhid)
It works fine ifi use a proper variable name, but if I use max(count`sex'), instead of replacing empty cells of n`sex' with the maximum value, it replaces it with 1. Could someone please help?