Announcement

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

  • How to change value labels?

    My variable "q2" has two values: 1 and 2. Their labels are No and Yes, respectively.

    Now I want to change it to 0==No, 1==Yes.

    I know one method: generating a new variable. Do we have better method that can replace current q2 instead of generating a new var? Because I've already have a lot of variables.

    Many thanks in advance!

  • #2
    You have several options, though I would encourage you to create a new variable. It is generally a bad idea to overwrite the original data.

    Code:
    *recoding the original variable
    recode q2 (1=0) (2=1)
    lab define newlabel 0 "No" 1 "Yes", modify
    lab val q2 newlabel
    
    *creating a new variable
    recode q2 (1=0) (2=1), gen(newvar)
    lab define newlabel 0 "No" 1 "Yes", modify
    lab val newvar newlabel
    Stata/MP 14.1 (64-bit x86-64)
    Revision 19 May 2016
    Win 8.1

    Comment


    • #3
      Yao:
      are you looking for something along the following lines?
      Code:
      . set obs 2
      number of observations (_N) was 0, now 2
      
      . g id=_n-1
      
      . label define id 0 "Yao" 1 "Carlo"
      
      . label val id id
      
      . list
      
           +-------+
           |    id |
           |-------|
        1. |   Yao |
        2. | Carlo |
           +-------+
      
      . label define id 1 "Yao" 0 "Carlo", modify
      
      . list
      
           +-------+
           |    id |
           |-------|
        1. | Carlo |
        2. |   Yao |
           +-------+
      
      .
      PS: crossed in the cyberspace with Carole's helpful reply.
      Kind regards,
      Carlo
      (Stata 19.0)

      Comment


      • #4
        I think you might be mixing up variables and (value) labels here. Those are two different concepts in Stata. Thus, you will need to change the values in the variable, q2, and it the respective value label, whatever the name is.

        elabel (SSC) can modify value labels in a systematic way. In your example, you could type

        Code:
        elabel define (q2) (= #-1) (= @) , replace
        This code will modify the value label that is attached to variable q2 (in the current label language); this is what the first parentheses are for: refer to the value label name attached to q2. In the second pair or parentheses, you subtract 1 from the existing values 1 and 2, so you will end up with 0 and 1. Inside the last pair of parentheses, you just copy the existing labels No and Yes. You would need to replace the values in variable q2, accordingly.

        Alternatively, you could type

        Code:
        elabel recode (q2) (1/2 = 0/1)
        You can then

        Code:
        recode q2 `r(rules)'
        to change the values in q2.

        Before you do any of the above, make sure that (i) variable q2 has only two values, 1 and 2, and (ii) the value label that is attached to q2 is not attached to any other variables.

        Best
        Daniel
        Last edited by daniel klein; 26 Jan 2019, 09:16.

        Comment


        • #5
          The option to re-label "on the fly" while recoding is also nice. It's concise but documents itself nicely. Wish I had learned about this sooner than I did:
          Code:
          recode q2 (1 = 0 "no") ( 2 = 1 "yes"), gen(q2new)

          Comment


          • #6
            Thanks, Mike! I don't think I was aware of this construction. I don't know how I missed this since it is clearly documented, but I guess I thought I knew the full extent of the command and never looked back. Very helpful!
            Stata/MP 14.1 (64-bit x86-64)
            Revision 19 May 2016
            Win 8.1

            Comment


            • #7
              Carole--One of my relative neophyte students pointed this out to me at a point when I had been using Stata for about 10 years and thought I knew the ins and outs etc. <grin>

              Comment


              • #8
                Thank you everyone!

                Comment

                Working...
                X