This is not a pressing matter as I easily found a work-around, I was just curious if anyone could tell me how to fix a syntax error I encountered. I was collapsing a dataset so that we kept only one observation per one variable and we needed the kept observation to be the one with the fewest missing values. I generated a variable that was the count of the non missing variables called "MissCount" and then tried to run this code:

by IDvar: gen KeepObs = 1 if MissCount == max(MissCount)
keep if KeepObs == 1

I also tried this with egen, neither worked.

I worked around this easily with:

by IDvar: egen KeepObs = max(MissCount)
keep if KeepObs == MissCount

But I was curious if someone could explain why my first attempt doesn't work? Again, not a pressing matter, but thank you!