Announcement

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

  • Getting value of specific value label

    Dear Statalisters,

    I've got a (probably very easy) problem and I'm somehow blind for the answer..

    I'm using the vallist statement (I know there are several other statements which produce simlar results, but in no one I'm getting it right..) to write out a local, in which my value labels are included. There are two options, of which one writes the values into a r(list) (option nolabels) and otherwise, the value labels.

    For example:

    Code:
    vallist risk
    No Yes
    
    vallist risk, nolabels sort local(val)
    di "`val'"
    1 2
    Now I need a new local which includes only the value, which belongs to "Yes", in this case the value 2. Is there a possibility to get this?

    Thanks a lot for your help,
    Anna

  • #2
    In principle, value labels need not be distinct. And there can be reasons to have the same value label for different numeric values. But that aside, this may help:

    Code:
    . sysuse auto, clear
    (1978 Automobile Data)
    
    . mata: st_vlsearch("origin", ("Domestic", "Foreign"))
           1   2
        +---------+
      1 |  0   1  |
        +---------+
    
    . label define origin 2 Foreign, modify
    
    . mata: st_vlsearch("origin", ("Domestic", "Foreign"))
           1   2
        +---------+
      1 |  0   1  |
        +---------+

    Comment


    • #3
      Thanks for your answer! But at the moment, I'm not sure, if this is what I need. Unfortunately I'm not very familiar with mata. Is it possible to get a local including the value which is attached to "Yes" with your commands?

      Comment


      • #4
        Naturally.

        Code:
        . mata: st_local("wanted", strofreal(st_vlsearch("origin", "Foreign")))
        
        . di "`wanted'"
        1

        Comment


        • #5
          Ah ok, thanks a lot

          Comment


          • #6
            An alternative to the mata approach, again if the value labels are distinct. This is based on a syntax documented only (as best I can determine) in Section 13.11 of the Stata User's Guide PDF, and no amount of help or search in Stata seems to find a link to it. After 20 minutes hunting for it, I've now added it to my list of FQA (frequent question answers) for future reference, since this isn't the first time I spent 20 minutes looking for it.
            Code:
            . sysuse auto, clear
            (1978 Automobile Data)
            
            . local wanted = "Foreign":origin
            
            . display "`wanted'"
            1
            
            . count if foreign == "Domestic":origin
              52

            Comment


            • #7
              Also see elabel (SSC)

              Code:
              . sysuse auto , clear
              (1978 Automobile Data)
              
              . elabel list origin if (@ == "Foreign")
              origin:
                         1 Foreign
              
              . return list
              
              scalars:
                              r(min) =  1
                              r(max) =  1
                         r(hasemiss) =  0
                           r(nemiss) =  0
                                r(k) =  1
              
              macros:
                             r(name) : "origin"
                           r(values) : "1"
                           r(labels) : ""Foreign""
              Edit:

              By the way, value labels need not be mutually exclusive. Compare

              Code:
              . label define origin 42 "Foreign" , add
              
              . 
              . local wanted = "Foreign":origin
              
              . display "`wanted'"
              1
              
              . 
              . mata: st_local("wanted", strofreal(st_vlsearch("origin", "Foreign")))
              
              . display "`wanted'"
              1
              
              . 
              . elabel list origin if @ == "Foreign"
              origin:
                         1 Foreign
                        42 Foreign
              
              . return list
              
              scalars:
                              r(min) =  1
                              r(max) =  42
                         r(hasemiss) =  0
                           r(nemiss) =  0
                                r(k) =  2
              
              macros:
                             r(name) : "origin"
                           r(values) : "1 42"
                           r(labels) : ""Foreign" "Foreign""
              Best
              Daniel
              Last edited by daniel klein; 14 May 2019, 10:52.

              Comment


              • #8
                See also

                SJ-4-4 dm0009 . . . . . . . Stata tip 14: Using value labels in expressions
                . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . K. Higbee
                Q4/04 SJ 4(4):486--487 (no commands)
                tips for using value labels in expressions
                https://www.stata-journal.com/sjpdf....iclenum=dm0009

                Comment

                Working...
                X