Stata treats missing values in numeric variables (".") as REALLY BIG numbers when evaluating Boolean logic statements. Say, for example, that age is missing in 4 people in your dataset. If you generate a new variable to indicate people older than 50, like this:
gen ageover50 = 0
replace ageover50 = 1 if age>50
Stata will evaluate the "if age>50" Boolean statement as "true" for observations where age = ".", and those 4 people with missing values will be coded as ageover50 = 1!
There is a reason Stata does this, but I'm not sure what it is...just BEWARE, and always check your missing values. This is NOT a problem with the recode command structured like this:
gen ageover50 = age
recode ageover50 min/49=0 50/max=1
In this case, Stata will get it right and leave the missing values as missing.
I often circumvent this problem with the following code:
gen ageover50 = 0
replace ageover50 = 1 if age>50
replage ageover50 = . if age=.
Does that work or is there problems with that coding strategy? Thanks.
Adding back the missing values to the ageover50 variable would probably deal with the issue (though perhaps less efficiently), but I think the larger point is that you have to keep track of your missing variables and not assume that they will remain missing when dealing with certain Boolean statements. We will deal more with the replace and recode commands next week in Lab 3.