Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to generate a string variable out of a labeled numeric variable (Stata)?

I have a variable state that takes integer values from 11 to 99. It is labeled.

How can I create a string variable stateString that would have string values without all those numeric values?

gen stateString = tostring(state)

doesn't do the trick.

like image 970
Buras Avatar asked Jun 23 '13 08:06

Buras


People also ask

How do I convert string to numeric in Stata?

Stata: Data Analysis and Statistical Software If you have a string variable and want to convert it to a numeric variable, you can use the encode command. If you have a string variable that has only numbers in it, then you can alternatively use the real() function.

How do I convert string variables to numeric ones?

One way is to use the number function with the compute command. To do this, you need to create a new variable using the compute command. To use the number function, you need to enclose the name of the string variable and a format for the new numeric variable. compute score1 = number(score, F2).

How do you string a variable?

String variables, simply speaking, are variables that contain not just numbers, but also other characters (possibly mixed with numbers). For instance, the European Social Survey stores information about the country where respondents were surveyed in variable cntry , which contains strings like "DE", "ES, "LT" etc.

How do you label variables in Stata?

Adding a value label to a variable in Stata is a two-step process. The first step is to use the . label define command to create a mapping between numeric values and the words or phrases used to describe those values. The second step is to associate a specific mapping with a particular variable using the .


2 Answers

tostring isn't a function; it's a command, and in Stata the two are quite distinct. Nothing but guesswork leads to the syntax you tried.

tostring state, gen(stateString) 

should work. But tostring is just a wrapper for the function string() and

gen  stateString = string(state) 

should also work to get string variables.

But the string values would be "11", ... "99" and that's the wrong approach. Given the value labels, you are fine with having this variable as numeric.

If you really want a string variable, you need decode, not tostring.

decode state, gen(stateString) 

EDIT: The syntax tostring() would only work if tostring() were a function, which is not. The original answer thus explained why the OP's code was wrong, as well as explaining how to do it correctly. I spelled out in this edit how to use decode.

EDIT 2021: The function string() still works and is documented as before, but the function name strofreal() is now given prominence.

like image 55
Nick Cox Avatar answered Oct 23 '22 19:10

Nick Cox


You have to install Roger Newson's command sdecode (ssc install sdecode) and then it is just:

sdecode state, gen(stateString)
like image 1
victoria Avatar answered Oct 23 '22 19:10

victoria