Announcement

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

  • Possible to replace underlying integer values with their value labels?

    Hi,

    I have two variables that both contain information about ethnicity, but the underlying values in the variables are different for the same value label. For e.g variable hh_ethnicity=1 with value label "Igbo" and variable recipient_ethnicity=2 with the same value label "Igbo". I want to replace hh_ethnicity and recipient_ethnicity with the value label "Igbo" as the underlying value. There's already help on this forum on how to transfer value labels between variables, but it's not what I want--- I need the underlying values to be the same for the same value label between two different variables. Or, I could also do with a code, if it exists, to replace values with their value labels.

    Thanks,
    Sampada

  • #2
    The decode command will create a new string variable based on your existing numeric variable and its value label. Here's an example, but do see the output of help decode for more details.
    Code:
    sysuse auto, clear
    describe foreign
    tab foreign
    tab foreign, nolabel
    decode foreign, generate(sforeign)
    describe sforeign
    tab sforeign
    Code:
    . sysuse auto, clear
    (1978 Automobile Data)
    
    . describe foreign
    
                  storage   display    value
    variable name   type    format     label      variable label
    ------------------------------------------------------------------------------------------------
    foreign         byte    %8.0g      origin     Car type
    
    . tab foreign
    
       Car type |      Freq.     Percent        Cum.
    ------------+-----------------------------------
       Domestic |         52       70.27       70.27
        Foreign |         22       29.73      100.00
    ------------+-----------------------------------
          Total |         74      100.00
    
    . tab foreign, nolabel
    
       Car type |      Freq.     Percent        Cum.
    ------------+-----------------------------------
              0 |         52       70.27       70.27
              1 |         22       29.73      100.00
    ------------+-----------------------------------
          Total |         74      100.00
    
    . decode foreign, generate(sforeign)
    
    . describe sforeign
    
                  storage   display    value
    variable name   type    format     label      variable label
    ------------------------------------------------------------------------------------------------
    sforeign        str8    %9s                   Car type
    
    . tab sforeign
    
       Car type |      Freq.     Percent        Cum.
    ------------+-----------------------------------
       Domestic |         52       70.27       70.27
        Foreign |         22       29.73      100.00
    ------------+-----------------------------------
          Total |         74      100.00

    Comment


    • #3
      Crossed with William

      So you want both variables to become string variables? See: help decode

      Code:
      decode hh_ethnicity, gen(hh_ethnicity_str)
      decode recipient_ethnicity, gen(recipient_ethnicity_str)
      drop hh_ethnicity recipient_ethnicity
      rename hh_ethnicity_str hh_ethnicity
      rename recipient_ethnicity_str recipient_ethnicity
      Stata/MP 14.1 (64-bit x86-64)
      Revision 19 May 2016
      Win 8.1

      Comment


      • #4
        Ah of course! Thanks both.

        Comment

        Working...
        X