Announcement

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

  • Use variable labels in putdocx text

    I am trying to create a word document with a number of histograms. I would like to describe each histogram with text using the variable label. I know that putdocx allows you to use functions and expressions from the documentation. I looked at the documentation for functions and expressions. It only has a documentation about label values (13.11). But I also know that I can extract the variable label using the local approach. So, I am wondering if it is possible to use variable label in putdocx text.

    Code:
    use https://www.stata-press.com/data/r17/nlsw88.dta
    histogram age
    graph export "path/histogram.age.png", replace as(png)
    
    ** Using local to store the variable label
    local age_lab: variable label age
    di "`age_lab'"
    
    putdocx begin
    putdocx paragraph, style(Heading1)
    putdocx text ("Histogram of `age_lab'")
    putdocx image ("histogram.age.png")
    putdocx save histogramreport, replace
    Crossposted on Stackoverflow.
    Last edited by Felix Kaysers; 02 Feb 2023, 01:18. Reason: Added Stackoverflow link and the dataset use
    Cheers,
    Felix
    Stata Version: MP 18.0
    OS: Windows 11

  • #2
    You could do this directly with

    Code:
    putdocx text ("Histogram of `: var lab age' ")

    Comment


    • #3
      Dear Andrew, thank you, that did indeed do the trick. I had tried the = but not : . What is the difference between them?

      Cheers,
      Felix
      Stata Version: MP 18.0
      OS: Windows 11

      Comment


      • #4
        If you add single quotes + a leading equal sign, then you are instructing Stata to evaluate the enclosed expression. In this case,

        display "`: var lab age'"
        is a string. So adding

        Code:
         display "`=`: var lab age''"
        instructs Stata to evaluate the string. For any string, Stata would interpret it as a variable name, and the evaluation amounts to displaying the first observation of the variable. Example:

        Code:
        sysuse auto, clear
        display "foreign"
        display "`=foreign'"
        display foreign[1]
        Res.:

        Code:
        .
        . display "foreign"
        foreign
        
        .
        . display "`=foreign'"
        0
        
        .
        . display foreign[1]
        0
        Now, the variable label of foreign in the auto dataset is "Car type", but there is no such variable in the dataset named Car type (the space in itself violates Stata name conventions). So if we force evaluate the string Car type, we would get the following error message:

        Code:
        sysuse auto, clear
        display "`:var lab foreign'"
        display "`=`:var lab foreign''"
        display Car type[1]
        Res.:

        Code:
        . display "`:var lab foreign'"
        Car type
        
        .
        . display "`=`:var lab foreign''"
        Car not found
        
        
        .
        . display Car type[1]
        Car not found
        r(111);
        Last edited by Andrew Musau; 02 Feb 2023, 06:17.

        Comment

        Working...
        X