-1

I'm trying to recode a byte variable in my Stata file to a string variable. But it is showing me an error (stating that it doesn't recognize the element). Stringing it isn't working either. Can I do this in Stata?

The data file I have includes the demographic information of continents. I recoded it based on life expectancy (lexp). The code I'm using is:

recode lexp (min/72 =1) (73/75 = 2) (76/max=3)

Now I need to recode 3 to a string value A.

Using recode lexp (3=A)

I get

unknown el A in rule

Nick Cox
  • 35,529
  • 6
  • 31
  • 47
dexter
  • 143
  • 1
  • 2
  • 5

1 Answers1

2

recode is meant to change the values of numeric variables to other numeric values; not to strings.

I think you want to label your values:

clear
set more off

input ///
byte bytevar
1
2
3
end

// add value labels
label define lblbyte 1 "A" 2 "B" 3 "C"
label values bytevar lblbyte

// list
list

// but they are really numeric values
list, nolabel

See help label.

You can also define the value labels directly within the recode command. Read help recode carefully.

Roberto Ferrer
  • 11,024
  • 1
  • 21
  • 23
  • Thanks! The labelling worked! Can we recode a byte variable to a string variable? – dexter Aug 07 '15 at 06:49
  • 1
    The `recode` command is not meant to do that. You can convert a numeric value to its corresponding string using the `string()` function. What is the purpose of converting the numeric value `3` into the string `A`? Is labeling not enough for your task? – Roberto Ferrer Aug 07 '15 at 06:54