Announcement

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

  • Automating (?) value label changes

    Hi,

    I strongly suspect this question has been answered elsewhere but I haven't found it (possibly because I'm not really sure how to pose the question articulately).

    I am wondering if there is a simple, "automated" way to change the values associated with a given value label. For example, say I have the following variable ('smoke'):

    How often do you smoke?
    1=never
    2=once a week or less
    3=every few days
    4=most days
    5=every day

    But lets say that I would like this variable coded so that never=0.

    This could be done via the following command:

    replace smoke = smoke-1

    The issue here is that while the values have changed, the value labels have not.

    Is there a way to convert the value labels to the following without running 'label define ...., replace'?
    0=never
    1=once a week or less
    2=every few days
    3=most days
    4=every day

    I'm thinking something along the lines of the completely made up:
    replace smoke_label = smoke_label - 1

    Kind regards,

    Owen Gallupe


  • #2
    Two options: first, use describe to figure out what label is being applied. Then re-code, and use label_drop to allow for new a new set of labels.

    more safely, create a new variable, new label, and apply that:
    Code:
    gen smoke2=smoke-1
    label define smokelable2 0 "never" 1 "once a week or less" 2 "every few days" 3 "most days" 4 "every day"
    label values smoke2 smokelabel2

    Comment


    • #3
      You can partially automate Ben Earnhart's suggestion. Instead of having to write out the new label smokelabel2, you can do it in a loop:

      Code:
      capture label drop smokelabel2
      forvalues j = 1/5 {
      label define smokelabel2 `=`j'-1' "`:label smokelabel1 `j''", modify
      }
      (where smokelabel1 is the label on the original variable).

      Comment


      • #4
        Have a look at the labutil2 package from SSC, especially labcpy.

        Code:
        g smoke2 = smoke - 1
        labcpy smoke_label smoke_label2 (1/5 = 0/4) ,values(smoke2)
        An updated version (hopefully finished around September) will also allow you to replace value labels that are already defined.

        Best
        Daniel

        Comment


        • #5
          Thanks Daniel. I was thinking that if something like labutli2 didn't exist, somebody should write it.
          -------------------------------------------
          Richard Williams, Notre Dame Dept of Sociology
          Stata Version: 17.0 MP (2 processor)

          EMAIL: [email protected]
          WWW: https://www3.nd.edu/~rwilliam

          Comment


          • #6
            Playing with value labels like this seems error prone. If you don't like the current encoding, why not simply decode the variable and recode to your liking. You do not have to completely redefine the value labels, just the one(s) you care about having a specific numeric value

            Code:
            sysuse voter, clear
            decode candidat, gen(s)
            label def candidat2 0 Bush
            encode s, gen(candidat2) label(candidat2)
            label list
            This is probably a good time to remind users that you don't have to know/remember the value label's numeric association. You can use the string label directly

            Code:
            sum inc if candidat != "Bush":candidat

            Comment


            • #7
              Thanks for the various tips on handling this. Robert, your warning is well received. "labcpy" as suggested by Daniel is exactly what I was looking for (thanks!) but its ease of use does not substitute for caution.

              Comment

              Working...
              X