Generating a string variable based on an existing string variable

510 views Asked by At

I am trying in Stata to generate a variable called region_code, based on another existing variable region,

Essentially, I want something that looks as follows: Country region region_code

X South Asia SA

Y Latin America & Caribbean LAC

And this is the code that I have tried:

generate region_codes  = .
replace  region_codes  = "SA" if region == "South Asia"
replace  region_codes  = "LAC" if region == "Latin America & Caribbean"
1

There are 1 answers

0
Nick Cox On BEST ANSWER

You need to initialise a string variable with an empty string "" not . -- which is numeric missing. Otherwise you will get an error message type mismatch.