I have a problem with Stata.
I have two variables: country
& country_birthplace
for example:
country | country_birthplace | dummy variable |
---|---|---|
US | US | 0 |
India | India | 1 |
India | US | 1 |
US | (missing value) | . (missing value) |
I want to create a dummy variable that is 0 if both country
and country_birthplace
are "US"
; 1 in all other cases; except where there is a missing value in country_birthplace
, when I want a missing value.
I tried this code:
experience_abroad = cond(missing(country_birthplace), ., (country == "US" & country_birthplace == "US" ? 0 : 1))
But this gives me the dummy variable the other way around. So I get 1 for US
& US
and 0 in all other cases.
How can I get the output I want?
I am surprised at the implication that your code was accepted as legal.
It is utterly illegal:
You need generate
or any abbreviation thereof.
The x ? a : b
syntax is allowed in Mata but not in Stata.
gen experience_abroad = cond(missing(country_birthplace), ., !(country == "US" & country_birthplace == "US")
should work, as should
gen experience_abroad = !(country == "US" & country_birthplace == "US") if !missing(country_birthplace)
or
gen experience_abroad = cond(country == "US" & country_birthplace == "US", 0, 1) if !missing(country_birthplace)