Announcement

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

  • Formatting output when using dtable for descriptive statistics

    I am using dtable to produce a baseline characteristics table for a clinical trial (Stata 19.5). It's such a nice addition to Stata!

    The table is a mixture of continuous and categorical variables. For the continuous variables, I would like these presented as median [IQR]. The code I have at the moment is:

    Code:
        dtable `ord',                                                        ///
            by(randgroup, nototals)                                        ///
            continuous(, statistics(q2 iqi))                                ///
            factor(, statistics(fvfrequency fvpercent))                        ///
            nformat(%6.1f q2 q1 q3 fvpercent)                                ///
            nformat(%6.0f fvfrequency)                                        ///
            define(iqi = q1 q3) sformat("[%s]" iqi)                            ///
            title("Table 1: Baseline characteristics")
    
        collect export "$results/Table1_BaselineCharacteristics.xlsx",    ///
            sheet("mITT") modify
    Where `ord' is my list of variables. For my continuous variables, this presents them as, for example: 36.0 [30.0 43.0]

    I have two questions:
    1) For the continuous variables, it would be good to present some variables to 1 dp and some to 0 dp (for example, age might be 24 [22 28], but a lab result might need to be 5.1 [4.8 5.3]). Is it possible to programme this format mix into the code?
    2) Can I define the interquartile range format so that there is a dash or comma between the values (eg, 24 [22-28])

    Thank you in advance for any help!

  • #2
    Code:
    sysuse auto, clear
    collect clear   
       
    dtable weight headroom i.rep78,                                       ///
            by(foreign, nototals)                                        ///
            continuous(, statistics(q2 iqi))                            ///
            factor(, statistics(fvfrequency fvpercent))                 ///
            nformat(%6.1f q2 q1 q3 fvpercent)                           ///
            nformat(%6.0f fvfrequency)                                  ///
            define(iqi = q1 q3,  delimiter("-")) sformat("[%s]" iqi)    ///
            title("Table 1: Baseline characteristics")
            
    collect style cell result[_dtable_stats]#var[headroom], nformat(%5.1f)
    collect style cell result[_dtable_stats]#var[weight], nformat(%5.0f)
    collect preview
    Res.:

    Code:
    . collect preview
    
    Table 1: Baseline characteristics
    ----------------------------------------------------
                                   Car origin           
                           Domestic          Foreign    
    ----------------------------------------------------
    N                        52 (70.3%)       22 (29.7%)
    Weight (lbs.)      3360 [2790-3730] 2180 [2020-2650]
    Headroom (in.)        3.5 [2.2-4.0]    2.5 [2.5-3.0]
    Repair record 1978                                  
      1                        2 (4.2%)         0 (0.0%)
      2                       8 (16.7%)         0 (0.0%)
      3                      27 (56.2%)        3 (14.3%)
      4                       9 (18.8%)        9 (42.9%)
      5                        2 (4.2%)        9 (42.9%)
    ----------------------------------------------------

    Code:
    collect levelsof result
    collect levelsof var
    to see how to reference the specific levels.

    Comment

    Working...
    X