Announcement

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

  • Recode/ replac

    Sorry, this should be simple.

    I typed for the variable votefentanyl1new

    . fre votefentanyl1new

    votefentanyl1new
    -----------------------------------------------------------
    | Freq. Percent Valid Cum.
    --------------+--------------------------------------------
    Valid 1 0 | 71 8.18 14.06 14.06
    2 1 | 289 33.29 57.23 71.29
    3 6 | 132 15.21 26.14 97.43
    4 9 | 13 1.50 2.57 100.00
    Total | 505 58.18 100.00
    Missing . | 363 41.82
    Total | 868 100.00
    -----------------------------------------------------------

    . recode votefentanyl1new (0 = 9)
    (votefentanyl1new: 0 changes made)

    . replace votefentanyl1new = . if votefentanyl1new == 9
    (0 real changes made)

    . dataex votefentanyl1new in 1/30

    ----------------------- copy starting from the next line -----------------------
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input long votefentanyl1new
    2
    3
    2
    2
    3
    1
    2
    1
    2
    2
    4
    2
    2
    2
    2
    3
    2
    2
    3
    1
    1
    2
    3
    1
    1
    2
    2
    2
    4
    2
    end
    label values votefentanyl1new votefentanyl1new
    label def votefentanyl1new 1 "0", modify
    label def votefentanyl1new 2 "1", modify
    label def votefentanyl1new 3 "6", modify
    label def votefentanyl1new 4 "9", modify
    ------------------ copy up to and including the previous line ------------------

    Listed 30 out of 868 observations

    Why can;t I replace the values of votefentanylnew? Thanks for any help

    Ric Uslaner


  • #2
    The problem is you are using the value labels, rather than the actual values in your -recode- and -replace- commands.

    If you look at your -dataex- output, you will see that the actual values of the variable votefentanyl1new are 1, 2, 3, and 4. Value labels 0, 1, 6, and 9 are attached to those. But as far as Stata is concerned, the 0, 1, 6, and 9 are just for convenient display and are not used in calculation. So what you really want to do here is rewrite those commands as:

    Code:
    recode votefentanyl1new (1 = 4)
    
    replace votefentanyl1new = . if votefentanyl1new == 4
    Actually, if those two commands appear consecutively in the code you are executing, so that no other changes are made to variable votefentanyl1new are made in between, you could obtain the same result with a single command:
    Code:
    replace votefentanyl1new = . if inlist(votefentanyl1new, 1, 4)
    Finally, I would just point out that it is usually not a good idea to have value labels assigned to a variable if the value labels themselves are numbers. That is a setup for exactly the kind of confusion that led to your difficulties here. I'll also speculate that most often this kind of variable, with value labels that are also numbers, arises from applying -encode- to a variable that was originally a string variable containing numbers represented as strings. -encode- is the wrong tool for that: -destring- is the appropriate way to do handle that.

    Comment


    • #3
      See also

      Code:
      SJ-18-4 dm0098  . . Speaking Stata: Seven steps for vexatious string variables
              . . . . . . . . . . . . . . . . . . . .  N. J. Cox and C. B. Schechter
              Q4/18   SJ 18(4):981--994                                (no commands)
              provides a step-by-step guide explaining how to convert string
              variables or -- as the case may merit -- to leave them as they
              are
      which among other advice touches on when encode is a good idea and when it isn't.

      If the original values were really 0 1 6 9 (and perhaps others) did those values have inherent meaning?

      Comment


      • #4
        Thanks again but now one more question please. I have for a data set:

        . dataex wanted in 363/400

        ----------------------- copy starting from the next line -----------------------
        Code:
        * Example generated by -dataex-. For more info, type help dataex
        clear
        input str6 wanted
        "AK-2" 
        "AK-4" 
        "AL-1" 
        "AL-12"
        "AL-12"
        "AL-16"
        "AL-3" 
        "AL-3" 
        "AL-51"
        "AL-6" 
        "AL-9" 
        "AR-2" 
        "AR-24"
        "AR-6" 
        "AR-8" 
        "AZ-1" 
        "AZ-1" 
        "AZ-1" 
        "AZ-11"
        "AZ-2" 
        "AZ-3" 
        "AZ-3" 
        "AZ-4" 
        "AZ-40"
        "AZ-8" 
        "AZ-9" 
        "CA-1" 
        "CA-1" 
        "CA-1" 
        "CA-1" 
        "CA-1" 
        "CA-1" 
        "CA-1" 
        "CA-1" 
        "CA-10"
        "CA-10"
        "CA-10"
        "CA-11"
        end
        ------------------ copy up to and including the previous line ------------------

        Listed 38 out of 868 observations

        .
        How might I eliminate the - sign to get, for example,

        AK2 instead of:
        AK-2

        Thanks for any help.

        Comment


        • #5
          replace wanted = subinstr(wanted,"-","",.)

          Comment


          • #6
            Thanks George

            Comment

            Working...
            X