Announcement

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

  • Relabel to ident labels for LaTex

    Dear all,

    I am quite new to Stata, so please excuse my basic question.

    At the moment, I am running a couple of regressions and I am using the 'estout' package to generate tables for LaTex. With the 'refcat' option, I assign headers to subgroups of the regression variables. For the sake ot a nice looking table, I want to ident the variable labels after the headers.

    I have already found a solution for identing in LaTex. However, this approach changes the variables labes and puts "\hspace{0.1cm} " in front of each label. This works perfectly fine.
    My question: after creating all LaTex files, I want to change the labels back to its originals, i.e. I want to remove the LaTex command at the beginning of each label. I assume, that I need to use another for-loop to loop through all variables and remove the LaTex command. However, I don't know how to do that. Does anyone have an idea?


    My current code looks the following:
    Code:
    foreach v of varlist * {
        label variable `v' `"\hspace{0.1cm} `: variable label `v''"'
        }
    
    esttab using testfile.tex, replace refcat(ESG_lag "\emph{ESG Data}" TobinsQ "\emph{Control Variables}", nolabel) label booktabs collabels(none) gaps se r2 ar2 mtitles("Cash" "Incentive" "Total") star(* 0.10 ** 0.05 *** 0.01) compress
    eststo clear
    Thank you in advance!


  • #2
    Welcome to Statalist, Dennis, and thank you for the clearly presented code.

    All you need to do is to save the original labels of each of the variables in a set of local macros so that you can restore them later. The example below accomplishes that.
    Code:
    sysuse auto, clear
    keep weight mpg
    
    foreach v of varlist * {
        local lbl_`v' : variable label `v'
        label variable `v' `"\hspace{0.1cm} `lbl_`v''"'
        }
    describe
    foreach v of varlist * {
        label variable `v' `"`lbl_`v'''"'
        }
    describe
    And here is some of the output.
    Code:
    -------------------------------------------------------------------------------------------------
                  storage   display    value
    variable name   type    format     label      variable label
    -------------------------------------------------------------------------------------------------
    mpg             int     %8.0g                 \hspace{0.1cm} Mileage (mpg)
    weight          int     %8.0gc                \hspace{0.1cm} Weight (lbs.)
    -------------------------------------------------------------------------------------------------
    
    -------------------------------------------------------------------------------------------------
                  storage   display    value
    variable name   type    format     label      variable label
    -------------------------------------------------------------------------------------------------
    mpg             int     %8.0g                 Mileage (mpg)'
    weight          int     %8.0gc                Weight (lbs.)'
    -------------------------------------------------------------------------------------------------
    Or, alternatively, a solution that doesn't involve saving the original labels. It could be tightened up, but this presentation is more transparent. If the string you were trying to match contained characters that are syntactic to Stata, this might be more difficult to make work, while the original is more robust. I think.
    Code:
    foreach v of varlist * {
        label variable `v' `"\hspace{0.1cm} `: variable label `v''"'
        }
    describe
    foreach v of varlist * {
        local tmp : variable label `v'
        local tmp : subinstr local tmp "\hspace{0.1cm} " ""
        label variable `v' `"`tmp'"'
        }
    describe

    Comment


    • #3
      Originally posted by William Lisowski View Post
      All you need to do is to save the original labels of each of the variables in a set of local macros so that you can restore them later. The example below accomplishes that.
      Thank you, that works perfectly! I really didn't think about this faily easy solution.

      Comment

      Working...
      X