How do I convert string variables to numeric variables in Stata?
Asked Answered
G

2

6

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.

Gwen answered 26/7, 2013 at 12:15 Comment(1)
It always help to show exactly what code you tried. Note that real() is a function in Stata, not a command.Leviathan
A
7

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

Or,

encode var, generate(var1)
Auckland answered 26/7, 2013 at 12:30 Comment(1)
Thank you so much, i realised it was string because there were some 'NA' in the dataset, i deleted them and it was fine. !Gwen
U
0
destring varname, replace ignore(xyz)      

xyz is any non-numeric character in the value of the variable

Ugric answered 15/7, 2022 at 0:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.