Announcement

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

  • Using the -display- command to show the value labels of a numeric variable

    I am puzzled by the following Stata code:



    If I use -tabulate- or -list-, the value labels attached to variable values are displayed. If I use the -display- command, the value labels are ignored and the underlying values are displayed.

    I would like to use -display- to show the value labels, not the values. Can someone explain how to do this or point me to the the appropriate documentation? I searched the archive and help files without finding any clues.

    Best,
    Alan
    Attached Files

  • #2
    list is better than display for displaying things in a dataset. Nevertheless, you can get value labels for displaying by means of the extended functions of macros. Type help extended_fcn for more information.
    Code:
    version 13.1
    
    clear *
    set more off
    set seed `=date("2014-04-30", "YMD")'
    
    sysuse auto
    generate double randu = runiform()
    sort randu
    list foreign in 1/4, noobs
    
    quietly levelsof foreign, local(foreigns)
    foreach foreign of local foreigns {
        local value_label : label (foreign) `foreign'
        display in smcl as text "`value_label'"
    }
    
    exit

    Comment


    • #3
      Joseph,

      Thank you for your suggestion.

      I am trying to create a custom output for each data record, not just list the unique value labels. For example, the following code shows the results of using -display- with a variable that has a defined value label assigned to it (-display- shows the underlying values) and a string version of these values created using -decode-.

      I am trying to understand why display does not show the value label but instead shows the values.

      sysuse auto, clear
      decode foreign, gen(foreignstr)
      foreach num of numlist 1(1)74 {
      di `num' _col(6) foreign[`num'] _col(9) foreignstr[`num']
      }


      Best,
      Alan

      Comment


      • #4
        OK, then try something like the following:
        Code:
        version 13.1
        
        clear *
        set more off
        sysuse auto
        
        forvalues observation = 1/`=_N' {
            local value = foreign[`observation']
            local value_label : label (foreign) `value'
            display in smcl as text "`observation' -> " as result "`value_label'"
        }
        
        exit

        Comment


        • #5
          Here is the way I think about it, which is a matter of knowing what the command does, rather than what one might guess.

          By the way, Alan didn't ask about this, but let's spell out for anyone puzzled that

          display foreign

          is interpreted by Stata as

          display foreign[1]

          i.e. if you don't specify a subscript for a variable, [1] is assumed.

          I would explain three things if asked to explain display to a Martian or R user.

          1. display has a role as an on-line calculator, as in di 2 + 2 or di -digamma(1). That could involve some of the data, but need not.

          2. display has a role to show text as instructed as in di "Nice header for output". Same comment.

          3. display is going to show one line of output, unless instructed otherwise.

          #3 is not a fundamental, as you can always call display repeatedly, say in a loop, as already discussed.

          #1 and #2 are not necessarily in order of importance. Indeed I guess many users never use #1 while they repeatedly use #2, or at least other commands do so on their behalf. But display always chooses a numeric interpretation when there is ambiguity. Hence it evaluates numerical expressions, replacing names by values and applying functions and operators to the extent possible. You have to spell out that you want text when that's not obvious to display.

          What would you expect given

          display foreign[1] * mpg[1]

          or

          display exp(foreign[1])

          Would you expect to be told that there was a type mismatch because the result of evaluating foreign[1] is text (namely, a value label)?

          So, as Joseph underlined, you can see the value label, but you have to spell out that's what you want.

          Comment


          • #6
            I thank Nick Cox for his insights on what -display- does (and does not do) and Joseph Coveney for his suggested Stata code. I proposed using -decode- to display the value labels which works but is inefficient in the sense that new variables need to be created.

            Joseph's solution solves my issue "on the fly" with little impact on memory and processing. While efficiency is not a concern in my immediate problem, I appreciate learning more about extended macro functions and have applied his suggestion in my program.

            Again, thank you both for your help and perspicacity.

            Best,
            Alan

            Comment

            Working...
            X