Announcement

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

  • Question about the command collect

    Dear all,

    here I use collect to make a table. The header observations number "N" is in the first row,just like table 1.
    But I want to put it in the center of the first and second row,just like table 2.how can I change my code ?
    many thanks in advance.

    Code:
    collect clear              
    sysuse auto,clear
    local x "price mpg weight"
    table (foreign) (var),stat(count foreign)  stat(mean `x')  ///
                       stat(sd `x') stat(median `x')              
    collect layout (foreign) (result[count] var[`x']#result )
    collect style header foreign, title(hide)
    collect style cell cell_type, border(right, pattern(nil))
    collect style cell cell_type[column-header]#var[`x'], ///
            border(bottom, pattern(single))
    collect style cell result, nformat(%04.3f)
    collect style cell result[count], nformat(%7.0f)
    collect label levels result count "N"  ///
             mean "Mean" sd "sd" median "Median",modify  
    collect style cell, border(right, pattern(nil))
    collect preview              
                                                               Table 1            
    -------------------------------------------------------------------------------------------------------
               N                Price                    Mileage (mpg)                Weight (lbs.)        
                 -----------------------------------------------------------------------------------------
                       Mean         sd     Median     Mean      sd   Median       Mean        sd     Median
    -------------------------------------------------------------------------------------------------------
    Domestic  52   6072.423   3097.104   4782.500   19.827   4.743   19.000   3317.115   695.364   3360.000
    Foreign   22   6384.682   2621.915   5759.000   24.773   6.611   24.500   2315.909   433.003   2180.000
    Total     74   6165.257   2949.496   5006.500   21.297   5.786   20.000   3019.459   777.194   3190.000
    -------------------------------------------------------------------------------------------------------
                    
                                                               Table 2                      
    -------------------------------------------------------------------------------------------------------
                              Price                    Mileage (mpg)                Weight (lbs.)        
              N    -----------------------------------------------------------------------------------------
                       Mean         sd     Median     Mean      sd   Median       Mean        sd     Median
    -------------------------------------------------------------------------------------------------------
    Domestic  52   6072.423   3097.104   4782.500   19.827   4.743   19.000   3317.115   695.364   3360.000
    Foreign   22   6384.682   2621.915   5759.000   24.773   6.611   24.500   2315.909   433.003   2180.000
    Total     74   6165.257   2949.496   5006.500   21.297   5.786   20.000   3019.459   777.194   3190.000
    -------------------------------------------------------------------------------------------------------




    Last edited by Raymond Zhang; 12 Mar 2022, 12:35.
    Best regards.

    Raymond Zhang
    Stata 17.0,MP
Working...
X