Search code examples
stata

Create variable group headings and indent labels in table


I am generating a table of means in Stata and trying to export it as a TeX file after formatting it in a certain way. I am using the community-contributed estout family of commands to do so, and I am very close to get the result I want.

However, I cannot figure out how to insert a few additional rows and labels in the generated output.

I have tried the code below:

eststo  clear
eststo, title("Mean"): estpost sum Male Female Race1 Race2 Age1-Age6 Education1-Education6
esttab using "${outdir}\Demographic.tex", label mtitles title(Demographic Information) ///
main(mean) b(2) nostar nogaps nonote replace

The above code generates the following table:

This code generates table like this.

What I want is to present different variables in a group. For example Male and Female should be indented under the heading "Gender" and similarly other variables to be indented under the headings of "Race", "Age" and "Education".

Of course. I can manually add those rows in the TeX code and indent these variables, but I want to generate the fully formatted table from Stata.

Is there a way to do it?


Solution

  • The estout command does not directly support this feature but below is a workaround demonstrated with a toy example using artificially-generated data:

    clear
    set obs 246
    
    local vars Male Female Race1 Race2 Age1 Age2 Age3 Age4 Age5 Age6 Education1 /// 
          Education2 Education3 Education4 Education5 Education6
    
    foreach var of local vars {
        generate `var' = runiform()
        label variable `var' "     `var'"
    }
    
    eststo clear
    eststo, title("Mean"): estpost sum Male Female Race1 Race2 Age1-Age6 Education1-Education6
    
    esttab, label mtitles title(Demographic Information) main(mean) b(2) nostar nogaps nonote ///
    refcat(Male "Gender" Race1 "Race" Age1 "Age" Education1 "Education", nolabel)
    

    The idea here is to introduce the spacing in each variable's label and then instruct esttab to create 'reference categories' before each group.

    The above code snippet produces the desired output:

    Demographic Information
    ---------------------------------
                                  (1)
                                 Mean
    ---------------------------------
    Gender                           
         Male                    0.52
    
         Female                  0.55
    
    Race                             
         Race1                   0.51
    
         Race2                   0.46
    
    Age                              
         Age1                    0.48
    
         Age2                    0.47
    
         Age3                    0.51
    
         Age4                    0.48
    
         Age5                    0.50
    
         Age6                    0.52
    
    Education                        
         Education1              0.51
    
         Education2              0.52
    
         Education3              0.50
    
         Education4              0.48
    
         Education5              0.48
    
         Education6              0.50
    
    ---------------------------------
    Observations                  246
    ---------------------------------
    

    Note that the available space is fixed so you may have to shorten some of your labels. Also, for longer labels you will need to adjust the spacing to bring them in alignment with the rest.


    EDIT:

    For LaTeX to typeset the spaces correctly, you need to introduce them in each variable's label using the appropriate markup as follows:

    label variable `var' "\hspace{0.5cm}`var'"