Search code examples
conditional-statementsstata

Generating dummy variable


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?


Solution

  • I am surprised at the implication that your code was accepted as legal.

    It is utterly illegal:

    1. You need generate or any abbreviation thereof.

    2. 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)