Announcement

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

  • Value labels for variable with lots of different values

    I want to use the user-written latab command which tabulates data in a latex table. Apparently, that command requires that all variable values have a label assigned. I have a variable that takes on lots of different values, so I don't want to assign the labels manually. Instead I thought I could just write in the variable value as the value label (which is obviously pointless, but it should make the latab command happy.)

    So say the variable takes on the value 1234, then I would like to label the variable with a value label of 1234 via >label define value_label 1234 "1234"<. I thought I could do it like this:

    Code:
    clear all
    set obs 100
    
    set seed 1234
    g x = round(rnormal(5,10))
    
    local label_string=""
    levelsof x, local(values)
    foreach value in "`values'" { 
        local label_string = "`label_string'" + " " + "`value'" + " " + `""`value'""'
    }
    dis `"`label_string'"'
    label define value_label `"`label_string'"'
    label values x value_label
    
    // this seems to work: 
    local j = "xxx"
    local i = `""`j'""'
    dis `"`i'"'
    But the string label is not what I intend it to be and it also is quite confusing with the syntax. There must be an easier way to achieve this?





  • #2
    The user written labmask command, part of the user-written labutil package from SSC (see the output of ssc describe labutil) will ease your task greatly, I think. With the labutil package installed, the following should start you in a useful direction.
    Code:
    clear all
    set obs 100
    set seed 1234
    generate x = round(rnormal(5,3))
    tab x
    labmask x, values(x) lblname(xlabel)
    tab x
    describe x
    label list xlabel
    Code:
    . label list xlabel
    xlabel:
              -5 -5
              -2 -2
              -1 -1
               0 0
               1 1
               2 2
               3 3
               4 4
               5 5
               6 6
               7 7
               8 8
               9 9
              10 10
              11 11
              12 12

    Comment


    • #3
      Thanks, that works great, and obviously much easier.

      Comment


      • #4
        I think labmask is simpler to use in this context; however, here is a way to do this with elabel (SSC).

        Code:
        clear all
        set obs 100
        set seed 1234
        generate x = round(rnormal(5,3))
        tab x
        quietly levelsof x , local(levels) // <- get values
        elabel define xlabel (`levels') (= strofreal(#)) // <- put into value label
        label values x xlabel // <- assign value label
        tab x
        describe x
        label list xlabel
        Best
        Daniel
        Last edited by daniel klein; 06 May 2019, 00:56.

        Comment


        • #5
          Thanks for the mentions of labmask. I suggest that it be linked with publication in the Stata Journal:

          SJ-8-2 gr0034 . . . . . . . . . . Speaking Stata: Between tables and graphs
          (help labmask, seqvar if installed) . . . . . . . . . . . . N. J. Cox
          Q2/08 SJ 8(2):269--289
          outlines techniques for producing table-like graphs

          https://www.stata-journal.com/articl...article=gr0034 gives free access to a .pdf version.

          Comment

          Working...
          X