btable

Extensive Summary Tables in Stata

Lukas Bütikofer, CTU Bern

18 November 2022

Why to use it

  • Easy-to-use: simple basic syntax

  • Flexible: variable-specific statistics and formatting

  • Type-specific defaults for continuous, categorical, count and time-to-event variables

  • Extensive: more than 15 descriptive statistics, 10 effect measures (with 25 confidence interval methods) and 10 tests

  • Customizable: user-specific descriptive statistics, effect measures and tests

  • Two-stage approach: keeps analysis and formatting separated

How to get it

net install github, from("https://haghish.github.io/github/")
  • btable can then be installed via:
github install CTU-Bern/btable

Basic syntax

use "auto3", clear
btable price weight repbin, saving("btab1") 

Basic syntax

use "auto3", clear
btable price weight repbin, saving("btab1") ///
  conti(price weight) cat(repbin)

Basic syntax

use "auto3", clear
btable price weight repbin, saving("btab1") ///
  conti(price weight) cat(repbin)
btable_format using "btab1", clear
list, ds
     | variable                     out_t      desc_info |
     |---------------------------------------------------|
  1. |                     Total (N = 74)   Descriptives |
     |                                                   |
  2. |                 mean (sd) or n (%)                |
     |                                                   |
  3. | Price                  6194 (2960)      mean (sd) |
     |                                                   |
  4. | Weight (lbs.)           3019 (777)      mean (sd) |
     |                                                   |
  5. | Repair record                                     |
     |                                                   |
  6. |     Poor                  40 (54%)          n (%) |
     |                                                   |
  7. |     Good                  29 (39%)          n (%) |
     +---------------------------------------------------+

Choice of descriptives

btable_format using "btab1", clear   ///
  descriptive(conti "mean ± sd" price "median (lq, uq)" ///
                cat "nlev (perc%, perclci-percuci%)") ///
  format_desc(conti "%8.4g" cat "%8.1f") ///
  design(row) drop(info) 
list, ds
     | variable                                                  out_t |
     |-----------------------------------------------------------------|
  1. |                                                  Total (N = 74) |
     |                                                                 |
  2. | Price - median (lq, uq)                       5079 (4296, 6342) |
     |                                                                 |
  3. | Weight (lbs.) - mean ± sd                          3019 ± 777.2 |
     |                                                                 |
  4. | Repair record - n (percentage, 95% CI)                          |
     |                                                                 |
  5. |     Poor                                 40 (54.1%, 42.8-64.9%) |
     |                                                                 |
  6. |     Good                                 29 (39.2%, 28.9-50.6%) |
     +-----------------------------------------------------------------+

More than one row of descriptives

btable_format using "btab1", clear ///
  desc(conti "mean (sd)" | conti "median [min, lq-uq, max]") ///
  design(row) drop(info) 
list, ds
     | variable                                               out_t |
     |--------------------------------------------------------------|
  1. |                                               Total (N = 74) |
     |                                                              |
  2. | Price                                                        |
     |                                                              |
  3. |     mean (sd)                                    6194 (2960) |
     |                                                              |
  4. |     median [min, lq-uq, max]   5079 [3291, 4296-6342, 15906] |
     |                                                              |
  5. | Weight (lbs.)                                                |
     |                                                              |
  6. |     mean (sd)                                     3019 (777) |
     |                                                              |
  7. |     median [min, lq-uq, max]    3190 [1760, 2240-3600, 4840] |
     |                                                              |
  8. | Repair record - n (%)                                        |
     |                                                              |
  9. |     Poor                                            40 (54%) |
     |                                                              |
 10. |     Good                                            29 (39%) |
     +--------------------------------------------------------------+

Missings

btable_format using "btab1", clear ncol(non-missing) 
list, ds
     | variable                  ns_t                out_t      desc_info |
     |--------------------------------------------------------------------|
  1. |                 Total (N = 74)       Total (N = 74)   Descriptives |
     |                                                                    |
  2. |                    non-missing   mean (sd) or n (%)                |
     |                                                                    |
  3. | Price                       73          6194 (2960)      mean (sd) |
     |                                                                    |
  4. | Weight (lbs.)               74           3019 (777)      mean (sd) |
     |                                                                    |
  5. | Repair record               69                                     |
     |                                                                    |
  6. |     Poor                                   40 (54%)          n (%) |
     |                                                                    |
  7. |     Good                                   29 (39%)          n (%) |
     +--------------------------------------------------------------------+
btable_format using "btab1", clear design(missing) 
list, ds
     | variable                             out_t      desc_info |
     |-----------------------------------------------------------|
  1. |                             Total (N = 74)   Descriptives |
     |                                                           |
  2. | Price                                                     |
     |                                                           |
  3. |     mean (sd)                  6194 (2960)      mean (sd) |
     |                                                           |
  4. |     missing - n (%)               1 (1.4%)          n (%) |
     |                                                           |
  5. | Weight (lbs.) - mean (sd)       3019 (777)      mean (sd) |
     |                                                           |
  6. | Repair record - n (%)                                     |
     |                                                           |
  7. |     Poor                          40 (54%)          n (%) |
     |                                                           |
  8. |     Good                          29 (39%)          n (%) |
     |                                                           |
  9. |     missing                       5 (6.8%)          n (%) |
     +-----------------------------------------------------------+

By group

use "auto3", clear
btable price weight repbin, by(foreign) saving("btab2")
btable_format using "btab2", clear collapse(repbin)
list, ds
     | variable                            out_t                out_1                out_2                              out_d        pv |
     |----------------------------------------------------------------------------------------------------------------------------------|
  1. |                            Total (N = 74)    Domestic (N = 52)     Foreign (N = 22)   Mean or risk difference (95% CI)   P-value |
     |                                                                                                                                  |
  2. |                        mean (sd) or n (%)   mean (sd) or n (%)   mean (sd) or n (%)                                              |
     |                                                                                                                                  |
  3. | Price                         6194 (2960)          6111 (3115)          6385 (2622)               -274 (-1788 to 1241)      0.72 |
     |                                                                                                                                  |
  4. | Weight (lbs.)                  3019 (777)           3317 (695)           2316 (433)                 1001 (682 to 1321)    <0.001 |
     |                                                                                                                                  |
  5. | Repair record (Good)             29 (39%)             11 (21%)             18 (82%)             -0.61 (-0.80 to -0.41)    <0.001 |
     +----------------------------------------------------------------------------------------------------------------------------------+
     |    desc_info                effect_info             test_info |
     |---------------------------------------------------------------|
  1. | Descriptives                    Effects                 Tests |
     |                                                               |
  2. |                                                               |
     |                                                               |
  3. |    mean (sd)   mean difference (95% CI)      Student's t-test |
     |                                                               |
  4. |    mean (sd)   mean difference (95% CI)      Student's t-test |
     |                                                               |
  5. |        n (%)   risk difference (95% CI)   Fisher's exact test |
     +---------------------------------------------------------------+

Effects and tests

btable_format using "btab2", clear  ///
  desc(price "median [lq, uq]") ///
  effect(price "mws (mws_lci-mws_uci)" cat "or (or_lci; or_uci)") ///
  test(price "ranksum" cat "chi2") ///
  collapse(repbin) design(row) drop(total) abbreviation 
list, ds
     | variable                                   out_1               out_2                    out_d        pv |
     |---------------------------------------------------------------------------------------------------------|
  1. |                                Domestic (N = 52)    Foreign (N = 22)   MD, MWS or OR (95% CI)   P-value |
     |                                                                                                         |
  2. | Price - median [lq, uq]        4816 [4187, 6303]   5759 [4499, 7140]         0.43 (0.30-0.57)      0.33 |
     |                                                                                                         |
  3. | Weight (lbs.) - mean (sd)             3317 (695)          2316 (433)       1001 (682 to 1321)    <0.001 |
     |                                                                                                         |
  4. | Repair record (Good) - n (%)            11 (21%)            18 (82%)        0.06 (0.02; 0.21)    <0.001 |
     +---------------------------------------------------------------------------------------------------------+
     |       desc_info                       effect_info   abbr_effec~o                    test_info |
     |-----------------------------------------------------------------------------------------------|
  1. |    Descriptives                           Effects        Effects                        Tests |
     |                                                                                               |
  2. | median [lq, uq]   Mann-Whitney statistic (95% CI)   MWS (95% CI)   Wilcoxon-Mann-Whitney test |
     |                                                                                               |
  3. |       mean (sd)          mean difference (95% CI)    MD (95% CI)             Student's t-test |
     |                                                                                               |
  4. |           n (%)               odds ratio (95% CI)    OR (95% CI)             chi-squared test |
     +-----------------------------------------------------------------------------------------------+

Further variable types

use "auto3", clear
btable price repbin n_checkups time_to_accident, ///
  count(n_checkups) exp_time(age) ///
  tte(time_to_accident) fail(accident) ///
  by(foreign) saving("btab3")

btable_format using "btab3", clear collapse(repbin) ///
  drop(total) abbreviation design(row) 
list, ds
     | variable                                                     out_1              out_2                         out_d        pv |
     |-------------------------------------------------------------------------------------------------------------------------------|
  1. |                                                  Domestic (N = 52)   Foreign (N = 22)   HR, IRR, MD or RD* (95% CI)   P-value |
     |                                                                                                                               |
  2. | Price - mean (sd)                                      6111 (3115)        6385 (2622)          -274 (-1788 to 1241)      0.72 |
     |                                                                                                                               |
  3. | Repair record (Good) - n (%)                              11 (21%)           18 (82%)        -0.61 (-0.80 to -0.41)    <0.001 |
     |                                                                                                                               |
  4. | Number of checkups - n/person-time (incidence)      177/521 (0.34)      74/159 (0.47)           0.73 (0.55 to 0.97)     0.025 |
     |                                                                                                                               |
  5. | Accident - failures (person-time at risk)                 31 (200)            10 (97)             1.5 (0.72 to 3.1)      0.28 |
     +-------------------------------------------------------------------------------------------------------------------------------+
     |                      desc_info                     effect_info   abbr_effec~o             test_info |
     |-----------------------------------------------------------------------------------------------------|
  1. |                   Descriptives                         Effects        Effects                 Tests |
     |                                                                                                     |
  2. |                      mean (sd)        mean difference (95% CI)    MD (95% CI)      Student's t-test |
     |                                                                                                     |
  3. |                          n (%)        risk difference (95% CI)    RD (95% CI)   Fisher's exact test |
     |                                                                                                     |
  4. |      n/person-time (incidence)   incidence rate ratio (95% CI)   IRR (95% CI)    Poisson exact test |
     |                                                                                                     |
  5. | failures (person-time at risk)           hazard ratio (95% CI)    HR (95% CI)         log-rank test |
     +-----------------------------------------------------------------------------------------------------+

Further options (see help files)

btable:

  • User-specific descriptives and effect measures: user_command(function) and user_var(varlist)

  • Alternative confidence intervals for effect measures

  • Subset of effects and tests: effects() and tests()

  • Hierarchical clustering: parent(varlist) and kid(varlist)

  • Denominator for categorical variables: denom(nonmiss)

btable_format:

  • Blocks: block() and block_head()

  • Formatting of p-values: p_digits() and p_breaks()