Search code examples
regressionstata

How to batch rename variables in esttab


I am using the community-contributed command esttab with the rename() option.

I have a special situation in which I run multiple regressions where each regression has a coefficient that is from a different (similarly-named) variable, but each corresponds to the same idea.

Here is a (very contrived) toy example:

sysuse auto, clear

rename weight mpg1
rename mpg mpg2
rename turn mpg3

I want to display the results of three regressions, but have only one line for mpg1, mpg2, and mpg3 (instead of each one appearing on a separate line).

One way to accomplish this is to do the following:

eststo clear
eststo: quietly reg price mpg1
eststo: quietly reg price mpg2
eststo: quietly reg price mpg3
esttab, rename(mpg1 mpg mpg2 mpg mpg3 mpg)

Can I rename all of the variables at the same time by doing something such as rename(mpg* mpg)?

If I want to run a large number of regressions, it becomes more advantageous to do this instead of writing them all out by hand.


Solution

  • Stata's rename group command can handle abbreviations and wildcards, unlike the rename() option of estout. However, for the latter, you need to build a list of names and store it in a local macro.

    Below you can find an improved version of your toy example code:

    sysuse auto, clear
    eststo clear
    
    rename (weight mpg turn) mpg#, addnumber
    
    forvalues i = 1 / 3 {
        eststo: quietly reg price mpg`i'
        local mpglist `mpglist' mpg`i' mpg
    }
    
    esttab, rename(`mpglist')
    
    ------------------------------------------------------------
                          (1)             (2)             (3)   
                        price           price           price   
    ------------------------------------------------------------
    mpg                 2.044***       -238.9***        207.6** 
                       (5.42)         (-4.50)          (2.76)   
    
    _cons              -6.707         11253.1***      -2065.0   
                      (-0.01)          (9.61)         (-0.69)   
    ------------------------------------------------------------
    N                      74              74              74   
    ------------------------------------------------------------
    t statistics in parentheses
    * p<0.05, ** p<0.01, *** p<0.001