Announcement

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

  • How to get the number corresponding to a string in a value label

    I have a county field that was a string and has been encoded, creating a value label of the same name.

    If I want to get the name of county 3, I can just use local foo : label county 3.

    But how can I programmatically get the number corresponding to a string?

    So for example:

    label define county 1 "Wade" 2 "Higgs" 3 "Leon"
    lab val county county


    I want to be able to get 3 from the string "Leon". I know I used to know how to do this (something like county["Leon"], if I recall correctly, slash am not just imagining the whole thing), but I can't seem to find any documentation. Obviously I can use -levels- to get all the numerics, and then loop through them, but I'm looking for a more direct syntax, as there are a lot of counties.

    Thanks!

  • #2
    The problem here is that nothing stops you using the same label for different values, and indeed that can be useful. (Imagine labelling election years in some country by the party which won.) So, the reverse look-up is not guaranteed to yield the same value, which is presumably why it is not a standard.

    But consider this as a way forward:

    Code:
    . sysuse auto
    (1978 Automobile Data)
    
    . su foreign if foreign=="Foreign":origin
    
        Variable |       Obs        Mean    Std. Dev.       Min        Max
    -------------+--------------------------------------------------------
         foreign |        22           1           0          1          1
    If the minimum and maximum are the same, you are seeing a constant.

    I'd program this if I found myself wanting it, but in practice I just use label dir and label list interactively.

    levels was superseded by levelsof, as from Stata 9. (It should still work, but I don't think that's guaranteed indefinitely.)


    .
    Last edited by Nick Cox; 13 Nov 2014, 09:33.

    Comment


    • #3
      Brian,

      Are you thinking of this?

      Code:
      "Leon":county
      See http://www.stata-journal.com/article...article=dm0009 for more details.

      Regards,
      Joe

      Comment


      • #4
        That was it! Good to know about the potential dangers of it, though. Thanks!

        Comment

        Working...
        X