Announcement

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

  • dtable for summary statistics

    Hello all,

    I am wondering if I could use -dtable- command for summary statistics export to a Word file, adding the min and max columns. I think the default gives mean and sd, but I would like to add min and max. How do I do this?


    Thanks

  • #2
    dtable option continuous() allows you to specify your
    statistics of interest with its option statistic(). These
    statistics are part of dtable's style, so you can design your
    table within one project, then save those style choices for future
    projects.

    Here is an example using the auto dataset.
    Code:
    sysuse auto
    
    * add min and max to default continuous statistics
    dtable mpg turn trunk, ///
        continuous(, statistic(mean sd min max))
    
    * as above, with some string adornments, and a different numeric format
    dtable mpg turn trunk, ///
        sformat("%s<" min) ///
        sformat("<%s" max) ///
        nformat("%12.1f" mean sd min max) ///
        continuous(, statistic(mean sd min max))
    
    * save this style for future projects
    collect style save dtab-min-max, replace
    
    * bigger table using our style
    dtable mpg turn trunk displ head gear, style(dtab-min-max)
    
    * If you want each statistic in its own column, you have to change the
    * layout after calling -dtable-.
    
    * query the current layout
    collect layout
    
    * get list of result levels and their labels
    collect label list result, all
    
    * shorten some result labels
    collect label levels result ///
        sd "SD" ///
        min "Min" ///
        max "Max" ///
        , modify
    
    * specify your layout
    collect layout (var) (result[mean sd min max])
    Here is the Stata session log for the above.
    Code:
    . sysuse auto
    (1978 automobile data)
    
    . 
    . * add min and max to default continuous statistics
    . dtable mpg turn trunk, ///
    >         continuous(, statistic(mean sd min max))
    
    --------------------------------------------------
                                     Summary          
    --------------------------------------------------
    N                                               74
    Mileage (mpg)         21.297 (5.786) 12.000 41.000
    Turn circle (ft.)     39.649 (4.399) 31.000 51.000
    Trunk space (cu. ft.)  13.757 (4.277) 5.000 23.000
    --------------------------------------------------
    
    . 
    . * as above, with some string adornments, and a different numeric format
    . dtable mpg turn trunk, ///
    >         sformat("%s<" min) ///
    >         sformat("<%s" max) ///
    >         nformat("%12.1f" mean sd min max) ///
    >         continuous(, statistic(mean sd min max))
    
    --------------------------------------------
                                  Summary       
    --------------------------------------------
    N                                         74
    Mileage (mpg)         21.3 (5.8) 12.0< <41.0
    Turn circle (ft.)     39.6 (4.4) 31.0< <51.0
    Trunk space (cu. ft.)  13.8 (4.3) 5.0< <23.0
    --------------------------------------------
    
    . 
    . * save this style for future projects
    . collect style save dtab-min-max, replace
    (style from DTable saved to file dtab-min-max.stjson)
    
    . 
    . * bigger table using our style
    . dtable mpg turn trunk displ head gear, style(dtab-min-max)
    
    ------------------------------------------------
                                    Summary         
    ------------------------------------------------
    N                                             74
    Mileage (mpg)             21.3 (5.8) 12.0< <41.0
    Turn circle (ft.)         39.6 (4.4) 31.0< <51.0
    Trunk space (cu. ft.)      13.8 (4.3) 5.0< <23.0
    Displacement (cu. in.) 197.3 (91.8) 79.0< <425.0
    Headroom (in.)               3.0 (0.8) 1.5< <5.0
    Gear ratio                   3.0 (0.5) 2.2< <3.9
    ------------------------------------------------
    
    . 
    . * If you want each statistic in its own column, you have to change the
    . * layout after calling -dtable-.
    . 
    . * query the current layout
    . collect layout
    
    Collection: DTable
          Rows: var
       Columns: result
       Table 1: 7 x 1
    
    ------------------------------------------------
                                    Summary         
    ------------------------------------------------
    N                                             74
    Mileage (mpg)             21.3 (5.8) 12.0< <41.0
    Turn circle (ft.)         39.6 (4.4) 31.0< <51.0
    Trunk space (cu. ft.)      13.8 (4.3) 5.0< <23.0
    Displacement (cu. in.) 197.3 (91.8) 79.0< <425.0
    Headroom (in.)               3.0 (0.8) 1.5< <5.0
    Gear ratio                   3.0 (0.5) 2.2< <3.9
    ------------------------------------------------
    
    . 
    . * get list of result levels and their labels
    . collect label list result, all
    
       Collection: DTable
        Dimension: result
            Label: Result
     Level labels:
    _dtable_stats  Summary
     _dtable_test  regress
        frequency  Frequency
              max  Maximum value
             mean  Mean
              min  Minimum value
          percent  
       proportion  
       rawpercent  
    rawproportion  
               sd  Standard deviation
             sumw  
    
    . 
    . * shorten some result labels
    . collect label levels result ///
    >         sd "SD" ///
    >         min "Min" ///
    >         max "Max" ///
    >         , modify
    
    . 
    . * specify your layout
    . collect layout (var) (result[mean sd min max])
    
    Collection: DTable
          Rows: var
       Columns: result[mean sd min max]
       Table 1: 6 x 4
    
    ------------------------------------------------
                            Mean   SD    Min    Max 
    ------------------------------------------------
    Mileage (mpg)           21.3  (5.8) 12.0<  <41.0
    Turn circle (ft.)       39.6  (4.4) 31.0<  <51.0
    Trunk space (cu. ft.)   13.8  (4.3)  5.0<  <23.0
    Displacement (cu. in.) 197.3 (91.8) 79.0< <425.0
    Headroom (in.)           3.0  (0.8)  1.5<   <5.0
    Gear ratio               3.0  (0.5)  2.2<   <3.9
    ------------------------------------------------

    Comment


    • #3
      Originally posted by Jeff Pitblado (StataCorp) View Post
      dtable option continuous() allows you to specify your
      statistics of interest with its option statistic(). These
      statistics are part of dtable's style, so you can design your
      table within one project, then save those style choices for future
      projects.

      Here is an example using the auto dataset.
      Code:
      sysuse auto
      
      * add min and max to default continuous statistics
      dtable mpg turn trunk, ///
      continuous(, statistic(mean sd min max))
      
      * as above, with some string adornments, and a different numeric format
      dtable mpg turn trunk, ///
      sformat("%s<" min) ///
      sformat("<%s" max) ///
      nformat("%12.1f" mean sd min max) ///
      continuous(, statistic(mean sd min max))
      
      * save this style for future projects
      collect style save dtab-min-max, replace
      
      * bigger table using our style
      dtable mpg turn trunk displ head gear, style(dtab-min-max)
      
      * If you want each statistic in its own column, you have to change the
      * layout after calling -dtable-.
      
      * query the current layout
      collect layout
      
      * get list of result levels and their labels
      collect label list result, all
      
      * shorten some result labels
      collect label levels result ///
      sd "SD" ///
      min "Min" ///
      max "Max" ///
      , modify
      
      * specify your layout
      collect layout (var) (result[mean sd min max])
      Here is the Stata session log for the above.
      Code:
      . sysuse auto
      (1978 automobile data)
      
      .
      . * add min and max to default continuous statistics
      . dtable mpg turn trunk, ///
      > continuous(, statistic(mean sd min max))
      
      --------------------------------------------------
      Summary
      --------------------------------------------------
      N 74
      Mileage (mpg) 21.297 (5.786) 12.000 41.000
      Turn circle (ft.) 39.649 (4.399) 31.000 51.000
      Trunk space (cu. ft.) 13.757 (4.277) 5.000 23.000
      --------------------------------------------------
      
      .
      . * as above, with some string adornments, and a different numeric format
      . dtable mpg turn trunk, ///
      > sformat("%s<" min) ///
      > sformat("<%s" max) ///
      > nformat("%12.1f" mean sd min max) ///
      > continuous(, statistic(mean sd min max))
      
      --------------------------------------------
      Summary
      --------------------------------------------
      N 74
      Mileage (mpg) 21.3 (5.8) 12.0< <41.0
      Turn circle (ft.) 39.6 (4.4) 31.0< <51.0
      Trunk space (cu. ft.) 13.8 (4.3) 5.0< <23.0
      --------------------------------------------
      
      .
      . * save this style for future projects
      . collect style save dtab-min-max, replace
      (style from DTable saved to file dtab-min-max.stjson)
      
      .
      . * bigger table using our style
      . dtable mpg turn trunk displ head gear, style(dtab-min-max)
      
      ------------------------------------------------
      Summary
      ------------------------------------------------
      N 74
      Mileage (mpg) 21.3 (5.8) 12.0< <41.0
      Turn circle (ft.) 39.6 (4.4) 31.0< <51.0
      Trunk space (cu. ft.) 13.8 (4.3) 5.0< <23.0
      Displacement (cu. in.) 197.3 (91.8) 79.0< <425.0
      Headroom (in.) 3.0 (0.8) 1.5< <5.0
      Gear ratio 3.0 (0.5) 2.2< <3.9
      ------------------------------------------------
      
      .
      . * If you want each statistic in its own column, you have to change the
      . * layout after calling -dtable-.
      .
      . * query the current layout
      . collect layout
      
      Collection: DTable
      Rows: var
      Columns: result
      Table 1: 7 x 1
      
      ------------------------------------------------
      Summary
      ------------------------------------------------
      N 74
      Mileage (mpg) 21.3 (5.8) 12.0< <41.0
      Turn circle (ft.) 39.6 (4.4) 31.0< <51.0
      Trunk space (cu. ft.) 13.8 (4.3) 5.0< <23.0
      Displacement (cu. in.) 197.3 (91.8) 79.0< <425.0
      Headroom (in.) 3.0 (0.8) 1.5< <5.0
      Gear ratio 3.0 (0.5) 2.2< <3.9
      ------------------------------------------------
      
      .
      . * get list of result levels and their labels
      . collect label list result, all
      
      Collection: DTable
      Dimension: result
      Label: Result
      Level labels:
      _dtable_stats Summary
      _dtable_test regress
      frequency Frequency
      max Maximum value
      mean Mean
      min Minimum value
      percent
      proportion
      rawpercent
      rawproportion
      sd Standard deviation
      sumw
      
      .
      . * shorten some result labels
      . collect label levels result ///
      > sd "SD" ///
      > min "Min" ///
      > max "Max" ///
      > , modify
      
      .
      . * specify your layout
      . collect layout (var) (result[mean sd min max])
      
      Collection: DTable
      Rows: var
      Columns: result[mean sd min max]
      Table 1: 6 x 4
      
      ------------------------------------------------
      Mean SD Min Max
      ------------------------------------------------
      Mileage (mpg) 21.3 (5.8) 12.0< <41.0
      Turn circle (ft.) 39.6 (4.4) 31.0< <51.0
      Trunk space (cu. ft.) 13.8 (4.3) 5.0< <23.0
      Displacement (cu. in.) 197.3 (91.8) 79.0< <425.0
      Headroom (in.) 3.0 (0.8) 1.5< <5.0
      Gear ratio 3.0 (0.5) 2.2< <3.9
      ------------------------------------------------
      Thanks, Jeff, for your reply. I replicated your suggestion code in my data. However, I could not get the min and max for all my variables ( just 4 of them ). Most of my variables are categorical on their own, and I encoded some when I cleaned the data, so I think this is the issue, right? Is it possible to have it somehow do it?

      The last code for giving each statistic in its own column, how do I get this table exported to a Word file after calling -dtable-?
      Here is the sample of my data :
      Code:
      * Example generated by -dataex-. For more info, type help dataex
      clear
      input double pidp byte sex float tq byte jbsat int dvage byte(jbstat sclfsat2 sclfsat7 sclfsato jbiindb_dv jbfxuse7 wah) float(treatment post treatment_post)
         22445 2 237 2 34  5 4 4 5 31 . . 1 0 0
         22445 2 241 . 35  6 3 3 5  0 . . 1 1 1
         29925 2 238 6 42  2 2 2 3 27 . . 0 0 0
         29925 2 242 5 43  2 2 5 3 27 0 . 0 1 0
         76165 2 236 5 36  5 6 4 6 23 . 1 1 0 0
         76165 2 241 5 37  2 5 1 6 23 0 1 1 1 1
        280165 2 239 5 40  2 5 2 5 16 . 4 1 0 0
        280165 2 243 . 41  1 1 5 2  0 . 4 1 1 1
        333205 2 237 6 29  2 5 5 7 33 . . 0 0 0
        469205 2 237 6 29  5 3 3 6 28 . . 0 0 0
        469205 2 241 6 30  2 3 2 3 28 0 . 0 1 0
        599765 2 236 6 32  2 6 5 7 33 . 1 1 0 0
        599765 2 240 6 33  2 6 6 6 33 . 1 1 0 0
        665045 1 237 3 37  2 5 6 6 24 . . 0 0 0
        665045 1 241 4 38  2 5 7 6 24 0 . 0 1 0
        732365 1 238 . 34  8 3 4 2  0 . . 0 0 0
        732365 1 242 . 35  8 2 4 2  0 . . 0 1 0
        760925 1 237 . 38  3 . . .  0 . . 0 0 0
       1587125 2 238 3 53  1 4 4 4 31 . 2 1 0 0
       1587125 2 242 5 54  1 3 3 4 31 . 2 1 1 1
       2626845 1 236 . 40  2 . . . 30 . . 0 0 0
       2888645 2 241 . 31  2 6 4 6  0 . . 0 1 0
       3229325 2 238 . 50  6 2 4 2  0 . . 0 0 0
       3424485 2 237 . 83  4 1 7 6  0 . . 0 0 0
       3424485 2 241 . 84  4 4 6 6  0 . . 0 1 0
       3667245 2 238 6 31  2 . . .  . . . 0 0 0
       3705325 2 236 . 65 97 6 6 5  0 . . 0 0 0
       3705325 2 240 . 66  4 6 5 6  0 . . 0 0 0
       3914085 2 239 5 57  2 7 6 6 28 . . 0 0 0
       3914765 1 239 5 56  1 . . . 15 . . 0 0 0
       3916125 2 239 6 21  7 6 5 6  4 . . 0 0 0
       4454005 1 238 . 74  4 4 5 4  0 . . 0 0 0
       4454005 1 242 . 75  4 3 6 6  0 . . 0 1 0
       4849085 1 237 2 36  2 6 4 3 28 . 3 1 0 0
       4849085 1 241 3 37  2 5 2 4 28 0 2 1 1 1
       4853165 1 236 6 48  2 2 2 2 33 0 . 0 0 0
       4853165 1 240 5 49  2 6 5 5 33 . . 0 0 0
       4853165 1 244 7 50  2 5 6 6 33 . . 0 1 0
      68002725 2 236 . 65  4 2 6 5  0 . . 0 0 0
      68002725 2 240 . 66  4 5 6 6  0 . . 0 1 0
      68006127 2 236 . 49  3 3 3 6  0 . . 0 0 0
      68008847 2 236 7 61  2 4 4 4 18 . . 0 0 0
      68008847 2 240 7 62  2 3 5 3 18 0 . 0 1 0
      68009527 1 236 5 41  2 5 2 5  9 . . 0 0 0
      68009527 1 240 5 42  2 5 3 5  9 . . 0 1 0
      68010887 2 236 6 55  2 5 3 5 28 . 4 1 0 0
      68010887 2 240 6 56  2 6 5 6 28 0 4 1 1 1
      68014287 2 237 . 50 97 2 4 3  0 . . 0 0 0
      68020407 2 237 . 82  4 5 6 6  0 . . 0 0 0
      68020564 1 236 . 48  8 3 4 7  0 . . 0 0 0
      68021765 2 237 7 60  2 4 3 3 18 . . 0 0 0
      68028575 2 236 . 28  6 6 6 7  0 . . 0 0 0
      68028575 2 240 . 28  6 7 5 7  0 . . 0 0 0
      68029927 2 236 . 47  6 2 5 6  0 . . 0 0 0
      68029927 2 240 . 48  6 3 5 6  0 . . 0 1 0
      68029931 1 236 6 50  2 2 3 2 14 . . 1 0 0
      68029935 2 236 4 22  2 4 3 5  . . . 0 0 0
      68029939 1 240 . 16  7 4 6 7  0 . . 0 1 0
      68031967 2 236 . 71  4 7 2 2  0 . . 0 0 0
      68031967 2 240 . 72  4 2 7 5  0 . . 0 1 0
      68035365 1 236 . 67  4 4 4 3  0 . . 0 0 0
      68035365 1 240 . 68  4 5 5 3  0 . . 0 0 0
      68035367 1 237 6 38  2 6 6 3 30 . 1 1 0 0
      68035367 1 240 6 39  2 7 6 6 30 0 2 1 1 1
      68036727 1 237 . 87  4 5 5 5  0 . . 0 0 0
      68036727 1 241 . 88  4 5 4 5  0 . . 0 1 0
      68037407 2 236 6 50  2 6 5 6 12 . . 0 0 0
      68037407 2 241 6 51  2 5 5 6 12 . . 0 1 0
      68037411 2 236 5 20 11 4 5 6 27 . . 0 0 0
      68041487 2 237 6 49  2 3 3 6  . . 1 1 0 0
      68041487 2 240 6 50  2 6 6 6 33 1 1 1 0 0
      68041491 1 237 6 46  2 3 3 6 16 . . 1 0 0
      68041491 1 240 . 46  2 5 7 6  0 . 4 1 0 0
      68041499 2 237 . 16  7 7 6 6  0 . . 0 0 0
      68042167 1 236 3 49  2 6 3 5 27 . . 0 0 0
      68042167 1 240 4 50  2 6 3 6 27 0 . 0 1 0
      68042171 2 237 . 48  2 2 1 4  0 . . 1 0 0
      68042171 2 240 5 49  2 . . . 27 . . 1 0 0
      68043527 1 237 . 65  4 6 6 4  0 . . 0 0 0
      68043527 1 240 . 66  4 5 6 5  0 . . 0 0 0
      68044207 2 236 4 43  2 4 2 4 27 . . 1 0 0
      68044207 2 240 6 44  2 3 2 5 27 1 . 1 0 0
      68044211 1 236 5 46  1 6 5 5 15 . . 0 0 0
      68044211 1 240 6 47  1 6 4 6 15 . . 0 1 0
      68044887 2 236 . 72  4 6 7 6  0 . . 0 0 0
      68044887 2 240 . 73  4 6 6 6  0 . . 0 0 0
      68045567 2 236 6 57  2 6 6 6 33 . 1 1 0 0
      68045567 2 240 6 58  2 6 6 6 33 1 1 1 0 0
      68045571 1 237 . 59  4 5 5 5  0 . . 0 0 0
      68045571 1 240 . 60  4 2 6 6  0 . . 0 1 0
      68046247 1 237 . 76  4 7 7 6  0 . . 0 0 0
      68046247 1 240 . 77  4 6 7 7  0 . . 0 1 0
      68046251 2 237 . 74  4 7 6 6  0 . . 0 0 0
      68046251 2 240 . 75  4 1 5 6  0 . . 0 1 0
      68049647 1 236 6 61  2 7 6 6 33 . . 0 0 0
      68049647 1 240 6 62  2 6 6 6 33 . . 0 0 0
      68049651 2 236 5 59  2 7 5 6 34 . . 0 0 0
      68049651 2 240 5 60  2 6 6 6 34 0 . 0 0 0
      68051007 1 236 . 58  3 3 6 3  0 . 2 1 0 0
      68051007 1 240 5 59  1 4 6 6 30 . 1 1 0 0
      end
      format %tq tq
      label values jbiindb_dv jbiindb_dv_label
      label def jbiindb_dv_label 0 "Not Applicable", modify
      label def jbiindb_dv_label 4 "Mining", modify
      label def jbiindb_dv_label 9 "Mechanical Eng.", modify
      label def jbiindb_dv_label 12 "Clothing/Text.", modify
      label def jbiindb_dv_label 14 " Construction", modify
      label def jbiindb_dv_label 15 "Constr. Relate", modify
      label def jbiindb_dv_label 16 "Wholesale", modify
      label def jbiindb_dv_label 18 "Retail", modify
      label def jbiindb_dv_label 23 "Insurance", modify
      label def jbiindb_dv_label 24 "Restaurants", modify
      label def jbiindb_dv_label 27 "Educ./Sport", modify
      label def jbiindb_dv_label 28 "Health Service", modify
      label def jbiindb_dv_label 30 "Other Services", modify
      label def jbiindb_dv_label 31 "Volunt./Church", modify
      label def jbiindb_dv_label 33 "Public Admin.", modify
      label def jbiindb_dv_label 34 "Social Sec.", modify
      label values wah ci_wah
      label def ci_wah 1 "Always", modify
      label def ci_wah 2 "Often", modify
      label def ci_wah 3 "Sometimes", modify
      label def ci_wah 4 "Never", modify

      Comment


      • #4
        Posting your query with an example of Stata code you tried with some
        corresponding (represenative) data really helps. Thanks for giving us some
        data to play with.

        By categorical, I assume you are using option factor() or the
        i. (factor fariables) notation. min and
        max are not available for variables of this kind.

        However, it is possible to build separate collections of dtable
        statistics, first treating your variables as continuous, then as
        factors, then combining the collections to produce your table. Once
        your collections are combined, you will need to manipulate the item tags
        depending on how you want to arrange the collected items into your
        table.

        Once you start using collect to make changes to the collection
        produced by dtable, you must use collect export to produce
        your Word document.

        It is not clear to me how you want the min and max of your categorical
        variables to show up in the table. Can you show us an example?

        Comment


        • #5
          Thanks for clarifying this for me

          In the end, I want to get a table like this using the i. (factor variables) variables as well :
          Click image for larger version

Name:	asdoc-summary-statistic-in-Word-RTF-from-Stata.png
Views:	1
Size:	57.2 KB
ID:	1739203



          I tried first to use asdoc but could not export the file for Word (the problem with exporting the Word file)
          Last edited by Malik Saendman; 08 Jan 2024, 14:03.

          Comment


          • #6
            Here is how I reproduced the example table given in #5.
            Code:
            sysuse auto
            
            * use -dtable- to get the statistics and do a little formatting
            dtable price mpg rep78 headroom trunk weight length turn displ gear for, ///
                continuous(, statistic(count mean sd min max)) ///
                sformat("%s" sd) /// remove parens
                nformat("%9.0g" min max) /// try to show unformatted values
                title("Descriptive Statistics")
            
            * match labels from original poster
            collect label levels result ///
                count "Obs" ///
                sd "Std.Dev." ///
                min "Min" ///
                max "Max" ///
                , modify
            
            * show variable names instead of labels
            collect style header var, level(value)
            
            * split statistics into their own columns
            collect layout (var) (result[count mean sd min max])
            
            * export to MS Word document
            collect export table.docx, replace
            Here is the final table from Stata's result window.
            Code:
            Descriptive Statistics
            -----------------------------------------------
                         Obs    Mean    Std.Dev.  Min  Max 
            -----------------------------------------------
            price         74 6,165.257 2,949.496 3291 15906
            mpg           74    21.297     5.786   12    41
            rep78         69     3.406     0.990    1     5
            headroom      74     2.993     0.846  1.5     5
            trunk         74    13.757     4.277    5    23
            weight        74 3,019.459   777.194 1760  4840
            length        74   187.932    22.266  142   233
            turn          74    39.649     4.399   31    51
            displacement  74   197.297    91.837   79   425
            gear_ratio    74     3.015     0.456 2.19  3.89
            foreign       74     0.297     0.460    0     1
            -----------------------------------------------
            Here is a screen shot of the Print Preview of the table in Libre Office on my Mac.


            Click image for larger version

Name:	Screenshot 2024-01-08 at 3.17.46 PM.png
Views:	1
Size:	31.9 KB
ID:	1739212

            Comment


            • #7
              I focused on the table asis, and missed the i. part. I will repost the above example, but treat rep78 and foreign as factor variables.

              Comment


              • #8

                In the following, I try to reproduce the table in #5, but treating
                variables foreign and rep78 as factor variables while still
                showing their minimum and maximum values like for continuous variables.
                Code:
                sysuse auto
                
                * continuous variables
                dtable price mpg headroom trunk weight length turn displ gear, ///
                    nosample ///
                    continuous(, statistic(count mean sd min max)) ///
                    title("Descriptive Statistics") ///
                    name(ctable)
                
                * get list of continuous variables, for call to -collect layout- later
                collect levels var
                local cvars = s(levels)
                
                * list of factor variables, will do some looping and for call to
                * -collect layout- later
                unab fvars : for rep
                
                * factor variables, continuous part
                dtable `fvars', ///
                    nosample ///
                    continuous(, statistic(count min max)) ///
                    name(f1table)
                foreach v of local fvars {
                    collect remap var[`v'] = `v'[_hide], fortags(var[`v'])
                }
                
                * factor variables, factor part
                dtable i.(`fvars'), ///
                    nosample ///
                    name(f2table)
                
                collect combine all = ctable f1table f2table
                
                * try to show unformatted values
                collect style cell result[min max], nformat("%9.0g")
                * remove the parens
                collect style cell result[sd], sformat("%s")
                * remove the parens, keep % sign
                collect style cell result[fvpercent], sformat("%s%%")
                
                * force new hidden level of factors to be the first; this will allow us
                * to show count, min, and max in the same row as the factor variable's
                * name
                foreach v of local fvars {
                    collect levels `v'
                    collect style autolevels `v' _hide `s(levels)', clear
                }
                
                * stack non-missing counts and factor level frequencies
                collect composite define col1 = count fvfrequency, trim
                collect label levels result col1 "Obs"
                * stack means and factor level percentages
                collect composite define col2 = mean fvpercent, trim
                collect label levels result col2 "Mean"
                
                * set result autolevels so we do not have to specify them in the layout
                collect style autolevels result col1 col2 sd min max, clear
                
                * show variable names instead of labels
                collect style header var, level(value)
                collect style header `fvars', title(name)
                
                * match labels from original poster
                collect label levels result ///
                    count "Obs" ///
                    sd "Std.Dev." ///
                    min "Min" ///
                    max "Max" ///
                    , modify
                
                * split statistics into their own columns
                collect layout (var[`cvars'] `fvars') (result)
                
                * export to MS Word document
                collect export table.docx, replace
                Here is the final table from Stata's result window.
                Code:
                Descriptive Statistics
                -----------------------------------------------
                             Obs    Mean    Std.Dev.  Min  Max 
                -----------------------------------------------
                price         74 6,165.257 2,949.496 3291 15906
                mpg           74    21.297     5.786   12    41
                headroom      74     2.993     0.846  1.5     5
                trunk         74    13.757     4.277    5    23
                weight        74 3,019.459   777.194 1760  4840
                length        74   187.932    22.266  142   233
                turn          74    39.649     4.399   31    51
                displacement  74   197.297    91.837   79   425
                gear_ratio    74     3.015     0.456 2.19  3.89
                foreign       74                        0     1
                  Domestic    52     70.3%                     
                  Foreign     22     29.7%                     
                rep78         69                        1     5
                  1            2      2.9%                     
                  2            8     11.6%                     
                  3           30     43.5%                     
                  4           18     26.1%                     
                  5           11     15.9%                     
                -----------------------------------------------
                Here is a screen shot of the Print Preview of the table in Libre Office on my Mac.

                Click image for larger version

Name:	Screenshot 2024-01-08 at 4.00.36 PM.png
Views:	1
Size:	82.6 KB
ID:	1739218

                Comment

                Working...
                X