Announcement

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

  • New on SSC: renlabv

    Thanks to Kit Baum a new package renlabv is available on SSC (see ssc describe renlabv).

    As a wrapper of the SSC program elabel by Daniel Klein, renlabv renames value labels to match the name(s) of the assigned variable(s). Hence, the program will require that elabel is installed. After using renlabv without specifying any variable the names of all value labels of the data set will be equal to the names of the assigned variables. By default renlabv will drop the original value labels if they are no longer assigned to any variable. Note that value labels originally not assigned to variables will not be renamed or dropped.

    Stata allows to attach a value label to several variables; this may be regarded as a feature (see the PDF documentation [D] for label). However, if for example the sign (sense) of only some items (variables) of a scale have to be recoded (e.g. reversed) such that all items correlate positively with each other, their value labels should be recoded, as well. But if only one (or only some) of the scale items have to be recoded (reversed) and the user is not aware that their value label is shared with other items it may happen that recoding this value label inadvertently recodes (reverses) the value label of the other items, as well, resulting in improper value labels for some items and consequently wrong conclusions from the analyses.

    You could use elabel to avoid this, for example
    Code:
    // recode variable "item1" and define a new value label "item1" with the old value label recoded:
    recode item1 (0=1) (1=0)
    elabel recode (item1) (0=1) (1=0), define(item1)
    
    // assign the new value label to variable "item1":
    label value item1 item1
    But you can use renlabv to avoid altogether the problem of inadvertently recoding value labels when recoding only some items of a set of items originally assigned to the same value labels. After having run renlabv you can then use the tandem of recode and elabel recode (without the option define())to safely recode some of the scale items. For example:
    Code:
    renlab
    recode item1 (0=1) (1=0)
    elabel recode (item1) (0=1) (1=0)
    Another use of renlabv might be that you import SPSS data containing variables with value labels defined into Stata: Stata will automatically generate label values named "labels0", "labels1", "labels2", etc. Running renlabv will rename them to better recognizable names since they will match the names of the assigned variables.

    The disadvantage of using renlabv is that subsequently you cannot longer use Stata's feature to relabel several variables instantaneously by redefining their common value label. If this bothers you, you can call renlabv by specifying only those variables which you actually want to recode.







  • #2
    The help of the SSC package renlabv has been updated improving the description and correcting smaller errors.

    When writing renlabv I was unaware of the SSC package valtolab by Johannes N. Blumenberg (2012) which aims to do the same. Differences are:
    • valtolab requires a varlist (you can use _all) whereas renlabv does not (not specifying a variable list will match all value label names to the names of the assigned variables).
    • valtolab will keep the original value labels by default whereas renlabv will drop the original value labels by default if they have been renamed to the name of an assigned variable.
    • valtolab will drop all unused value labels whereas renlabv will drop only unused value labels if renamed by renlabv.
    • valtolab optionally generates a detailed report whereas renlabv will only return the names of dropped value labels in a local macro.
    • valtolab runs much faster than renlabv.

    Comment


    • #3
      I think the command by Johannes Blumenberg is called valtovar (SSC).

      There is a serious problem with that command when variables have value labels attached that share the names of other variables in the dataset. I have discussed the issue here.

      Unfortunately, renlabv has similar problems. Here is an example:

      Code:
      clear
      input alive dead
      1 0
      0 1
      end
      
      label define not_dead 0 dead 1 alive
      label define alive    0 alive 1 dead
      
      label values alive not_dead
      label values dead  alive
      
      list
      label list
      
      renlabv
      
      list
      label list
      The (relevant) output:

      Code:
      . list
      
           +---------------+
           | alive    dead |
           |---------------|
        1. | alive   alive |
        2. |  dead    dead |
           +---------------+
      
      . label list
      alive:
                 0 alive
                 1 dead
      not_dead:
                 0 dead
                 1 alive
      
      .
      . renlabv
      
      .
      . list
      
           +---------------+
           | alive    dead |
           |---------------|
        1. |     1    dead |
        2. |     0   alive |
           +---------------+
      
      . label list
      dead:
                 0 dead
                 1 alive
      I also imagine that multiple label languages could be problematic because variables would typically have different labels attached in different languages; the label name could then only match the variable name in one of the languages.

      Ignoring the multiple label languages problems, here is a sketch that -- intentionally -- does not allow restrictions to specific variables; it renames them all as that seems to be safer:

      Code:
      program rename_to_match_label_name
          
          version 18
          
          preserve
          
          foreach varname of varlist _all {
              
              local labelname : value label `varname'
              if ("`labelname'" == "") continue
              
              tempname templabel
              
              label copy `labelname' `templabel'
              
              label values `varname' `templabel'
              
              local dropnames `dropnames' `labelname'
              local oldnames `oldnames' `templabel'
              local newnames `newnames' `varname'
              
          }
          
          if ("`dropnames'" != "") ///
              label drop `dropnames'
          
          elabel rename (`oldnames') (`newnames')
          
          restore , not
          
      end
      You could replace elabel rename with second loop if you did not want to rely on third-party commands.
      Last edited by daniel klein; 10 May 2024, 12:53.

      Comment


      • #4
        I just installed and read the help file. Seems I will find uses for this. Thanks for putting it out.

        Comment


        • #5
          daniel klein : Thanks a lot for alerting us to the problems involved using renlabv and valtovar [sic!] -- I should have consulted you before posting renlabv at SSC!

          I will take a closer look and rewrite (or retract) renlabv. I hope potential users of either program will have read your posts here (#3) and there.

          Comment

          Working...
          X