Search code examples
stringstatarecode

How to recode strings in Stata?


I would like to change the values in my Nationality variable. For some countries, there is a space after the name of the country ("Germany" and "Germany "), so that they don't appear to be the same country when looking at frequencies. I tried using "encode" to make the values numeric, so that I could use "recode" to change the values, but that gave me the error "unknown el Germany in rule". I attached a screenshot. Screenshot of data


Solution

  • recode is for numeric variables only, you can use replace.

    replace Nationality = "Belgium" if Nationality == "Belgium "
    

    However, a more convenient way for the problem at hand is strtrim(), which removes leading and trailing spaces.

    replace Nationality = strtrim(Nationality)