Announcement

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

  • How to extract variable labels.

    Hello everyone, I have a question about the use of variable labels.


    Scenario:

    Looking at the code below, you will notice that I am writing a variables name in a text.file if its value is greater than 0. However, instead of the variables name (`var'), I want to write down its label. How can I achieve this? Thank you for the help.


    Code:

    file open keywords using `file_title'.txt, text write replace

    local N = _N

    forvalues i = 1 / `N' {
    file write keywords _n
    file write keywords _n
    file write keywords "Article: `= pdf_name[`i']'" _n
    file write keywords _n
    file write keywords "Keywords:" _n
    foreach var of varlist `var_list' {
    if `var'[`i'] > 0 {
    file write keywords "`var'" _n
    }
    else if `var'[`i'] == 0 {
    }
    }
    }
    file close keywords

  • #2
    Code:
    local lbl : variable label `var'
    file write keywords "`lbl'" _n

    Comment


    • #3
      Originally posted by William Lisowski View Post
      Code:
      local lbl : variable label `var'
      file write keywords "`lbl'" _n
      This worked. Thank you very much.

      Comment


      • #4
        It can be shorted to
        Code:
        file write keywords "`: variable label `var''"
        similar to the way you handle getting the value of pdf_name[`i'] without generating an intermediate local macro.

        But on Statalist I tend to like to spell things out step-by-step to make it more accessible to later readers who may not be familiar with a particular shortcut. The important lesson in this post was the macro function variable label, one of many such functions described in the output of help macro.

        Comment

        Working...
        X