Announcement

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

  • Command to return name of a value label of a variable?

    I am working with a number of large datasets which have had value labels applied inappropriately. As part of an automated solution to deal with this, I need to generate a list of all variables (in this example a local called "mislabelled") which contains the name of all variables which have the value label "bad_label".

    Code:
    local mislabelled
    foreach v of varlist _all {
    some_command `v'if ("`r_somecommand'"=="bad_label"){
    [INDENT=2]local mislabelled "`mislabelled'" "`v'"[/INDENT]
    }
    }
    Last edited by Mike Murphy; 14 May 2018, 10:08. Reason: Tried & failed to fix indenting in code-block

  • #2
    See uselabel, especially option var, as in

    Code:
    preserve
    uselabel bad_label , var
    local mislabelled `r(bad_label)'
    restore
    
    display "`mislabelled'"
    Also see labutil2 (SSC).

    Best
    Daniel

    Comment


    • #3
      That does exactly what I need, thanks Daniel!

      Comment


      • #4
        The problem appears to be not

        command to return name of a value label of a variable
        but

        command to return names of variables with specified value label.

        Also,

        Code:
        . sysuse auto, clear
        (1978 Automobile Data)
        
        . ds, has(vallabel origin)
        foreign
        
        . findname, vallabel(origin)
        foreign
        
        . search findname, sj
        
        Search of official help files, FAQs, Examples, SJs, and STBs
        
        SJ-15-2 dm0048_3  . . . . . . . . . . . . . . . . Software update for findname
                (help findname if installed)  . . . . . . . . . . . . . . .  N. J. Cox
                Q2/15   SJ 15(2):605--606
                updated to be able to find strL variables
        
        SJ-12-1 dm0048_2  . . . . . . . . . . . . . . . . Software update for findname
                (help findname if installed)  . . . . . . . . . . . . . . .  N. J. Cox
                Q1/12   SJ 12(1):167
                correction for handling embedded double quote characters
        
        SJ-10-4 dm0048_1  . . . . . . . . . . . . . . . . Software update for findname
                (help findname if installed)  . . . . . . . . . . . . . . .  N. J. Cox
                Q4/10   SJ 10(4):691
                update for not option
        
        SJ-10-2 dm0048  . . . . . . . . . . . . . .  Speaking Stata: Finding variables
                (help findname if installed)  . . . . . . . . . . . . . . .  N. J. Cox
                Q2/10   SJ 10(2):281--296
                produces a list of variable names showing which variables
                have specific properties, such as being of string type, or
                having value labels attached, or having a date format
        Here, as documented above, findname is from the Stata Journal and so must be installed before it can be used. In this case, it provides the same functionality with different syntax, which is a little deal. However, I mention it because findname can do everything that ds can do, and much more besides. (and further extensions are planned).
        Last edited by Nick Cox; 14 May 2018, 11:43.

        Comment


        • #5
          That is correct, my first impulse was to seek a generalized solution to the problem (returning the property of a variable) but trying to implement this in Stata indeed resulted in a different (and much more specific) question.

          Comment

          Working...
          X