Announcement

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

  • Changing Label Code in Label

    Hey everyone,

    I have received a data set where the sender incorrectly assigned the wrong codes for my sex variable. Instead of having male, female, and other as 0, 1 and 2 respectively; they have instead assigned 1 as male, 2 as female, and 3 as other. I have tried to fix this with my commands and while it works for female and other, when typing the command for male replaces it with 3.

    I would like to replace the code for male from 3 to 0. Is there a way to change the label codes itself?

    Thanks very much for the help!

  • #2
    We can't say what's wrong with your code because you don't show any. Nor do you give a data example.

    But this may help.

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input float wrong
    1
    2
    3
    end
    label values wrong wrong
    label def wrong 1 "male", modify
    label def wrong 2 "female", modify
    label def wrong 3 "other", modify
     
    gen right = wrong - 1 
    
    label def right 0 male 1 female 2 other 
    
    label val right right 
    
    list 
    
         +-----------------+
         |  wrong    right |
         |-----------------|
      1. |   male     male |
      2. | female   female |
      3. |  other    other |
         +-----------------+
    
    list, nola 
    
         +---------------+
         | wrong   right |
         |---------------|
      1. |     1       0 |
      2. |     2       1 |
      3. |     3       2 |
         +---------------+
    Many experienced users would reach for recode here. That's fine by me if it's fine by them.

    Comment


    • #3
      Drawing on Nick's example, here are two ways of doing this with elabel (SSC)

      Code:
      clear
      input float wrong
      1
      2
      3
      end
      label values wrong wrong
      label def wrong 1 "male", modify
      label def wrong 2 "female", modify
      label def wrong 3 "other", modify
      
      // # 1
      gen right:right = wrong - 1
      
      label copy wrong right
      elabel define right (= # - 1) (= @) , replace
      
      // # 2
      elabel recode wrong (1/3=0/2) , define(right2)
      recode wrong `r(rules)' , generate(right2)
      label values right2 right2
      
      list
      list , nolabel
      Best
      Daniel

      Comment

      Working...
      X