Announcement

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

  • destringing variables




    Hi I am trying to destring a variable. Then replace those variables with numbers eg

    A = 1

    B = 2




    However I want the A and B to remain showing so as not to forget what 1 and 2 stand for. I found two ways how to do it. Are there any adv/disadv to any of the methods shown below




    METHOD 1

    Encode stringvariable, gen(encode_variable)

    label list encode_variable

    label define variable encode_variable “ 1 A, 2 B”




    METHOD 2

    Section B:




    gen new_variable = real (string_variable)

    replace new_variable = 1 if string_variable == “A”

    label define string_variablelabel 1 “A”

    label values new_variable string_variablelabel




    Is this just multiple ways how to skin a cat?

  • #2
    Martin:
    you may want to consider:
    Code:
     set obs 2
    Number of observations (_N) was 0, now 2.
    
    . g start="A" in 1
    
    
    . replace start="B" in 2
    
    . g wanted=_n
    
    . label define wanted 1 "A=1" 2 "B=2"
    
    . label val wanted wanted
    
    . list
    
         +----------------+
         | start   wanted |
         |----------------|
      1. |     A      A=1 |
      2. |     B      B=2 |
         +----------------+
    
    .
    As an aside, cats (and animals in general) should never be skinned (we live with 4 cats!!) !!
    Kind regards,
    Carlo
    (Stata 19.0)

    Comment


    • #3
      If you have string values such as A and B destring can't do anything but produce missing values. It's indeed encode that you seek here.

      Comment

      Working...
      X