Announcement

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

  • Retrieve value from value label, given the label text?

    From a value label I need to retrieve the value associated with a particular label. That is, I need the inverse of:

    Code:
    sysuse auto
    local lab : label domestic 1
    di "`lab'"
    Given the string "Foreign" and the value label , I need to retrieve the value 1. There is the usage

    Code:
    sum foreign if foreign=="Foreign":origin
    But there doesn't seem to be an extended macro function, or anything else I can find, that uses the "string":labname construct.

    I've created this to do the job, but I wonder if I'm missing somethign inbuilt?

    Code:
    program getvalfromlab, rclass
        version 14.2
        syntax anything , [ local(string) ]
        
        gettoken vl rest : anything
        // trim
        local rest `rest'
        local foundit 0
    
        qui la list `vl'
        forval i=`r(min)'/`r(max)' {
            if `"`: label `vl' `i''"'==`"`rest'"' {
                di `"{txt}"{res}`rest'{txt}" --> {res}`i'"'
                return scalar value = `i'
                return local label "`rest'"
                if !mi(`"local"') {
                    c_local `local' = `i'
                }
                local foundit 1
                continue, break
            }
        }
        if !`foundit'{
            if !mi(`"local"') {
                c_local `local'
            }
            di `"{res}`rest'{txt} not found `i'"'
        }
    end

  • #2
    Note that nothing stops two or more values having the same label. This can be used on purpose e.g. to label years by whichever party was in power for a graph. So, the idea of an inverse is problematic. But nevertheless


    Code:
    . sysuse auto
    (1978 Automobile Data)
    
    . mata : st_vlsearch("origin", "Domestic")
      0

    Comment


    • #3
      Nick correctly points out that the "value":label notation you started with is potentially ambiguous. Here's an approach to your problem that uses that notation, rather than mata, and is extended to show how it gives an incomplete result in the face of ambiguity, and that the notation can present misleading results when used in an if clause on a Stata command.

      Code:
      . sysuse auto, clear
      (1978 Automobile Data)
      
      . label list origin
      origin:
                 0 Domestic
                 1 Foreign
      
      . tab foreign
      
         Car type |      Freq.     Percent        Cum.
      ------------+-----------------------------------
         Domestic |         52       70.27       70.27
          Foreign |         22       29.73      100.00
      ------------+-----------------------------------
            Total |         74      100.00
      
      . count if foreign=="Foreign":origin
        22
      
      . local x = "Foreign":origin
      
      . local y = "Domestic":origin
      
      . display "Foreign `x' --- Domestic `y'"
      Foreign 1 --- Domestic 0
      
      . label define origin 0 "Foreign", modify
      
      . label list origin
      origin:
                 0 Foreign
                 1 Foreign
      
      . tab foreign
      
         Car type |      Freq.     Percent        Cum.
      ------------+-----------------------------------
          Foreign |         52       70.27       70.27
          Foreign |         22       29.73      100.00
      ------------+-----------------------------------
            Total |         74      100.00
      
      . count if foreign=="Foreign":origin
        52
      
      . local x = "Foreign":origin
      
      . local y = "Domestic":origin
      (value label dereference "Domestic":origin not found)
      
      . display "Foreign `x' --- Domestic `y'"
      Foreign 0 --- Domestic .
      
      .

      Comment


      • #4
        Thank you Nick and William; two quicker and easier solutions than my program.

        And thank you for the point about ambiguity. In my particular use, I know for a fact that there are no duplicates, but that is good to be careful about. I suppose in theory I could extend my program to return multiple values if such exist.

        Comment

        Working...
        X