Announcement

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

  • Copying labels when recoding a variable

    Hi,

    I have a variable in my dataset which is a combination variable of two other variables which I created using the below code
    Code:
    egen varAB = group(varA varB), label
    Now, due to small cells in some of the resulting groups I am recoding some of the categories of this variable and creating a new variable using this code:
    Code:
    recode varAB(156/191=191 "newname"), gen(newvarAB)
    . My question is how can I copy the labels of the unchanged categories from the old to the new variable without having to do it manually one by one.

    Thanks in advance

  • #2
    use "clonevar" as your first step, then recode without generating a new variable

    Comment


    • #3
      My initial reaction was similar to Rich's but recode won't oblige on the value label. Any shorter alternative to this?

      Code:
      egen newvarAB = group(varA varB), label 
      replace newvarAB = 191 if inrange(newvarAB, 156, 191) 
      label def newvarAB 191 "newname", modify

      Comment


      • #4
        Thanks a lot, I was not aware of this command. How can i rename the labels of the recoded categories only when using this command though? I am merging some of the small cells together but when I use
        Code:
        recode clonedvar(1/5=1 "newname")
        with the cloned variable I get the following error
        Rules defining value labels not allowed when overwriting a variable

        Comment


        • #5
          #3 is a reply to #4.

          Comment


          • #6
            thank you very much. #3 works. So there is no way I can actually keep both the old and the new recoded variable? It would be very helpful if i could also keep the old variable for reference.

            Comment


            • #7
              #3 is already an answer to #6 (sorry, couldn't resist). My code is all about generating a new variable, so it follows that you get to keep the existing variable if you wish.

              Comment


              • #8
                you are right, I didn't notice that your first line of code was not a repetition of my initial code so I wasn't creating the group variable twice but with a different name. Many thanks

                Comment


                • #9
                  label copy will copy value labels that can then be modified. Also see labcpy (part pf labutil2, SSC).

                  Best
                  Daniel

                  Comment

                  Working...
                  X