Announcement

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

  • dtable with variable names and labels

    Dear all,

    It is possible to construct a summary statistics table that has the variable names in the first column, the variable label in the second, and then the statistics.

    To be more precise in the example below I want an extra column on the left with the name of the variables.

    Code:
    sysuse auto
    dtable headroom price mpg , by(foreign , nototal)

  • #2
    You can add additional formatting, but this is the gist of it.

    Code:
    sysuse auto, clear
    collect clear
    dtable headroom price mpg , by(foreign , nototal)
    foreach var of varlist headroom price mpg{
        collect get varname= "`var'", tags(var[`var'])
    }
    collect label levels result varname "Variable", replace 
    collect style header result, level(label)
    collect layout (var) (result[varname] foreign#result)
    Res.:

    Code:
    . collect layout (var) (result[varname] foreign#result)
    
    Collection: DTable
          Rows: var
       Columns: result[varname] foreign#result
       Table 1: 4 x 3
    
    -------------------------------------------------------------------------------------
                   Variable                           Car origin                         
                                       Domestic                        Foreign           
                            frequency (percent%) mean (sd) frequency (percent%) mean (sd)
    -------------------------------------------------------------------------------------
    N                                           52 (70.3%)                     22 (29.7%)
    Headroom (in.) headroom                  3.154 (0.916)                  2.614 (0.486)
    Price             price          6,072.423 (3,097.104)          6,384.682 (2,621.915)
    Mileage (mpg)       mpg                 19.827 (4.743)                 24.773 (6.611)
    -------------------------------------------------------------------------------------

    Comment


    • #3
      Thanks a lot Andrew!

      Comment


      • #4
        Originally posted by Nikolaos Kanellopoulos View Post
        variable names in the first column, the variable label in the second, and then the statistics.
        For variable names preceding labels, turn off the labels and use collect get to obtain labels instead of variable names. The following also left-aligns the labels.

        Code:
        sysuse auto, clear
        collect clear
        dtable headroom price mpg , by(foreign , nototal)
        foreach var of varlist headroom price mpg{
            collect get varlab= "`:var lab `var''", tags(var[`var'])
        }
        collect label drop var
        collect label levels var _N "N"
        collect label levels result varlab " Label", replace 
        collect style header result, level(label)
        collect style cell result[varlab], halign(left) font(, nobold)
        collect layout (var) (result[varlab] foreign#result)
        Res.:

        Code:
        . collect layout (var) (result[varlab] foreign#result)
        
        Collection: DTable
              Rows: var
           Columns: result[varlab] foreign#result
           Table 1: 4 x 3
        
        -------------------------------------------------------------------------------------
                 Label                                    Car origin                         
                                           Domestic                        Foreign           
                                frequency (percent%) mean (sd) frequency (percent%) mean (sd)
        -------------------------------------------------------------------------------------
        N                                           52 (70.3%)                     22 (29.7%)
        headroom Headroom (in.)                  3.154 (0.916)                  2.614 (0.486)
        price    Price                   6,072.423 (3,097.104)          6,384.682 (2,621.915)
        mpg      Mileage (mpg)                  19.827 (4.743)                 24.773 (6.611)
        -------------------------------------------------------------------------------------

        Comment

        Working...
        X