Announcement

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

  • How to display value label "value" for a specific value and not the value label name

    Hi, I am trying to display the value label actual "value" and not the value label name. For example, I have the variable -gender- that is labeled with value label -genderlab2-

    Code:
    . label list
    genderlab2:
               0 Female
               1 Male
    I am trying to display only "Female" or "Male". This is part of a larger loop with -putexcel-.

    Code:
    foreach x of varlist bc_dm {
    
    local row = 2    
    
    foreach var of gender {
        
        putexcel clear
        putexcel set results.xlsx, sheet (`x') open modify
        ranksum `var', by(`x')
        putexcel B`row' = "p =", right
        local pval = (r(p_exact))
        putexcel C`row' = `pval'
        local row = `row'+1
        quietly summarize `var' if (`x') == 0, detail
        local q50 = r(p50)
        putexcel B`row' = "Female median =", right
        putexcel C`row'= `q50'
        local row = `row'+1
        local q25 = r(p25)
        putexcel B`row' = "Q25% =", right
        putexcel C`row'= `q25'
        local row = `row'+1
        local q75 = r(p75)
        putexcel B`row' = "Q75% =", right
        putexcel C`row'= `q75'
        local row = `row'+1
        quietly summarize `var' if (`x') == 1, detail
        local q50 = r(p50)
        putexcel B`row' = "Male median =", right
        putexcel C`row'= `q50'
        local row = `row'+1
        local q25 = r(p25)
        putexcel B`row' = "Q25% =", right
        putexcel C`row'= `q25'
        local row = `row'+1
        local q75 = r(p75)
        putexcel B`row' = "Q75% =", right
        putexcel C`row'= `q75'
        
        putexcel save
            }
    }
    I am showing only one of the variables (-gender-) for the sake of simplicity. There are many more, which makes the loop necessary. The main problem I am trying to overcome is the need for knowing the value label contents for this line of code:

    Code:
    putexcel B`row' = "Female median =", right
    Thanks in advance!

  • #2
    I don't fully understand the problem but
    Code:
    display "`: label genderlab2 0'"
    will display "Female".

    Conversely,
    Code:
    display `"`="Female":genderlab2'"'
    will display 0.


    For a community-contributed command, see also elabel (SSC or SJ).

    Comment


    • #3
      Thank you Daniel. Is there a way to do it in the loop without knowing the value label name? I will have many variables each with their own associated value label. Your code works, but if its within the loop and agnostic to the value label name it would need `x' in the code.

      Comment


      • #4
        Code:
        local value_label_name : value label varname
        puts into local macro value_label_name the name of the value label attached to varname (in the current label language).

        Comment


        • #5
          That just stores the value label name, here -genderlab2-. I want to store one value of -genderlab2- (either "Male" or "Female") and output either option.

          Comment


          • #6
            I figured it out using a combination of your suggestions.


            Code:
            local value_label_name : value label `x' 
            di "`: label `value_label_name' 0'"

            Comment


            • #7
              It can be simpler:

              Code:
              . sysuse auto, clear
              (1978 automobile data)
              
              . di "`: label (foreign) 1'"
              Foreign
              
              . di "`: label origin 1'"
              Foreign
              When checking, I always start at

              Code:
              help macro
              and click my way towards what I want.

              Comment

              Working...
              X