Announcement

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

  • Listing values and labels

    I have a bunch of countries. They are coded as numbers (values) in an integer variable. For each value, the country name has been attached as value (label).

    It's a sizable dataset and I need to do some manipulations based on country.

    Initially, I didn't notice that what appear as values are actually labels, and hence did a
    Code:
    tab country
    and wrote commands based on country names, e.g.
    Code:
    replace countrycode = "ALB" if country == "Albania"
    Got a type mismatch and only then realized that country is not string.

    So, now, I want to change the commands to reflect country (number, as opposed to name). But how do I figure out which number (value) corresponds to which country name (label). Is there a tab command that will show me a "dictionary" of values and labels?
    Thank you for your help!

    Stata SE/17.0, Windows 10 Enterprise

  • #2
    Many possibilities.

    To get a listing of values and attached labels (without using community-contributed commands):

    Code:
    label list lblname
    where lblname is the name of the value label attached to country; you get that name from, e.g., describe.

    Another possibility is

    Code:
    codebook country , tabulate(99)
    where you replace 99 with (at least) the number of distinct values in country. With codebook, you do not need to know the value label name.

    Yet another way is

    Code:
    numlabel , add
    tabulate country
    where numlabel will add numeric values to (all) the value labels.

    A last, not so well known, technique (cf. Higbee 2004) is

    Code:
    replace countrycode = "ALB" if country == "Albania":lblname
    where, again, lblname is the name of the value label attached to country.

    You could also use decode to create a string variable (I will not show an example here) and refer to that string variable. For the example that you provide, I would consider sticking with strings for code readability. Compare

    Code:
    replace countrycode = "ALB" if country == "Albania":lblname
    with

    Code:
    replace countrycode = "ALB" if country == 42
    assuming that Albania has code 42. Which of these two is easier to understand in a couple of months from now?


    Anyway, returning to the task of obtaining a list of values and associated labels, there is also community-contributed software that could be helpful. For example, fre (SSC) will achieve the same thing as numlabel but more conveniently and without actually changing value labels

    Code:
    ssc install fre // <- only need to do this once
    fre country
    Also see elabel (SSC) for convenient extensions to Stata's label commands. For example, you do no longer need to lookup value label names to list value labels

    Code:
    ssc install elabel // <- only need to do this once
    elabel list (country)
    Best
    Daniel


    Higbee, K. 2014. Stata tip 14: Using value labels in expressions. The Stata Journal, 4(4):486-487.
    Last edited by daniel klein; 17 Jul 2019, 23:24.

    Comment


    • #3
      Many thanks, Daniel
      Originally posted by daniel klein View Post
      Anyway, returning to the task of obtaining a list of values and associated labels, there is also community-contributed software that could be helpful. For example, fre (SSC) will achieve the same thing as numlabel but more conveniently and without actually changing value labels

      Code:
      ssc install fre // <- only need to do this once
      fre country
      This directly answers the question I asked.

      Originally posted by daniel klein View Post
      A last, not so well known, technique (cf. Higbee 2004) is

      Code:
      replace countrycode = "ALB" if country == "Albania":lblname
      This addresses the issue, or the question behind the question. Just one follow-up: how do I find out the lblname?





      Thank you for your help!

      Stata SE/17.0, Windows 10 Enterprise

      Comment


      • #4
        Originally posted by Pratap Pundir View Post
        how do I find out the lblname?
        Hmm...looks like this is displayed in the variable properties window. So I should be set. Thanks!
        Thank you for your help!

        Stata SE/17.0, Windows 10 Enterprise

        Comment

        Working...
        X