Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I convert string variables to numeric variables in Stata?

I have two variables in Stata, both numeric variables that have somehow been recorded as string variables. I need them converted to numeric variables so that I can generate a new variable with them.

When I tried, it said type mismatch I have tried the real and the encode commands, none of which are working.

like image 957
Exodia16 Avatar asked Jul 26 '13 12:07

Exodia16


People also ask

How do you convert a string variable to a numeric variable?

destring; tostringdestring/tostring, replace or destring/tostring, generate() converts string variables to numeric variables and vice versa.

How do you change variable type in Stata?

The storage type of variables can be changed with the help of the recast command. Here, numeric variables can be changed to a different numeric type, and string variables to a different string type (i.e. different string length). will ask Stata to change the storage type of variable income to "long".

Can you recode a string variable in Stata?

To recode variables in Stata, use the recode command. To use recode , you must provide a list of variables to be recoded and the rules associated with that change.

Why is Stata reading numbers as string?

One common reason for this problem is that the data have been imported from a spreadsheet or something similar. Some users of Excel or similar programs get in the habit of putting several lines of header material before the body of their data.


1 Answers

Try destring var, replace in Stata if you want to convert var into numeric

Or,

encode var, generate(var1)
like image 120
Metrics Avatar answered Sep 22 '22 11:09

Metrics