Search code examples
variablesstatadummy-variable

Ignore missing values when creating dummy variable


How can I create a dummy variable in Stata that takes the value of 1 when the variable pax is above 100 and 0 otherwise? Missing values should be labelled as 0.

My code is the following:

generate type = 0
replace type = 1 if pax > 100

The problem is that Stata labels all missing values as 1 instead of keeping them as 0.


Solution

  • This occurs because Stata views missing values as large positive values. As such, your variable type is set equal to 1 when you request this for all values of pax > 100 (which includes missings).

    You can avoid this by explicitly indicating that you do not want missing values replaced as 1:

    generate type = 0
    replace type = 1 if pax > 100 & pax != .