No announcement yet.
  • Filter
  • Time
  • Show
Clear All
new posts

  • Assign label for string values of a variable

    I am trying to define a label for non-numeric values of a variable.
    For example, I have a variable X with values 1, 2, 3a
    I want to define a label for this variable
    label define x1 1 "complete" 2 "incomplete" 3a "partial complete"

    However, Stata gives an error of invalid sytax and will not assign label for 3a.

    I also tried to recode the values for the variable:
    recode X (3a=3)

    but Stata gives an error message of unknown el 3a in rule

    Would appreciate if anyone could help with this issue.

  • #2
    Stata does not have value labels for strings. The information you can want can be held in another string variable, or the same string variable.


    • #3
      Thanks! Do you know if I could rename and then recode the values for the new variable? I tried recoding 3a but it gives an error message "unknown el 3a in rule."


      • #4
        As the help for recode explains (emphasis added):

        recode changes the values of numeric variables according to the rules specified.

        So recode does not apply to strings. It is safest to clone the variable and then replace values.

        clonevar X1 = X
        replace X1 = "complete" if X == "1"
        replace X1 = "incomplete" if X == "2"
        replace X1 = "partial complete" if X == "3a"
        Last edited by Nick Cox; 06 Aug 2015, 15:57.


        • #5
          ok, Thanks for your help!