How to recode a string to a numeric value in Stata?

42 Views Asked by At

I am using the replace command to recode a string variable countrycode to a numeric variable ccode.

But the replace command has no effect. Stata gives no error messages, bt just says "0 changes made" after each line.

countrycode is a 3-character string.

This is the syntax:

gen ccode=-9
replace ccode=371 if countrycode=="arm"
replace ccode=900 if countrycode=="aus"
replace ccode=339 if countrycode=="alb"
replace ccode=160 if countrycode=="arg"

How can I convert the data to numeric?

1

There are 1 best solutions below

0
DBart2022 On

The code was correct, but the string variable was all caps, and I had used lower-case characters. Thanks for the tip!