Announcement

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

  • dtable - aligning output

    I have a problem aligning the columns with dtable. Using the example from The Stata Blog » Creating tables of descriptive statistics in Stata 18: The new dtable command
    Code:
    webuse idu, clear
    dtable, by(male, tests testnotes nototal) ///
     sample(, statistic(frequency proportion) ///
     place(seplabels) ) continuous(age, statistics(mean minmax) test(kwallis)) ///
     continuous(ltime rtime, statistics(mean skewness kurtosis) test(poisson)) ///
     factor(needle, statistics(fvfrequency fvproportion)) ///
     factor(jail inject, statistics(fvfrequency) test(fisher)) ///
     define(minmax = min max,  delimiter(-)) nformat(%9.1f  mean minmax) ///
     sformat("(%s)" fvproportion minmax proportion) ///
     nformat(%9.2f proportion fvproportion)
     collect style header male, title(hide)
    collect label levels male 0 "Female", modify
    collect label levels male 1 "Male", modify
    collect style cell var[rtime 1.needle 1.jail], border( bottom, width(1))
    collect style cell male[_dtable_test], shading( background(lightyellow)) ///
     font(, bold)
    collect notes "Kruskal–Wallis rank test performed for age."
    collect notes "Poisson regression main effects test performed for ltime and rtime."
    collect notes "Pearson's chi-squared test performed for needle."
    collect notes "Fisher's exact test performed for jail and inject."
    collect preview
    The results for each by variable are right aligned within each by category
    Code:
    Age (in years)                     28.8 (18.0-46.0)  31.7 (17.0-52.0)  0.002
    Last time seronegative for HIV-1  22.1 -0.305 2.017 24.3 -0.353 2.251 <0.001
    First time seropositive for HIV-1  12.0 0.951 2.285  14.4 0.749 3.024  0.020
    but I would like all the results to be equally spaced and aligned within the category.

    Thank you.
    Eddy

  • #2
    Here is a way to split the results into separate columns.
    Code:
    clear all
    
    * original code - minus -place(seplabels)- in option -sample()-
    webuse idu, clear
    dtable, by(male, tests testnotes nototal) ///
     sample(, statistic(frequency proportion)) ///
     continuous(age, statistics(mean minmax) test(kwallis)) ///
     continuous(ltime rtime, statistics(mean skewness kurtosis) test(poisson)) ///
     factor(needle, statistics(fvfrequency fvproportion)) ///
     factor(jail inject, statistics(fvfrequency) test(fisher)) ///
     define(minmax = min max,  delimiter(-)) nformat(%9.1f  mean minmax) ///
     sformat("(%s)" fvproportion minmax proportion) ///
     nformat(%9.2f proportion fvproportion)
     collect style header male, title(hide)
    collect label levels male 0 "Female", modify
    collect label levels male 1 "Male", modify
    collect style cell var[rtime 1.needle 1.jail], border( bottom, width(1))
    collect style cell male[_dtable_test], shading( background(lightyellow)) ///
     font(, bold)
    collect notes "Kruskal–Wallis rank test performed for age."
    collect notes "Poisson regression main effects test performed for ltime and rtime."
    collect notes "Pearson's chi-squared test performed for needle."
    collect notes "Fisher's exact test performed for jail and inject."
    collect preview
    
    * find the name of the composite results being used in the layout
    collect query autolevels result
    * find the names of the results in the statistics composite result
    collect query composite _dtable_stats
    
    * split the results into 3 columns
    collect composite define col1 = frequency mean fvfrequency
    collect composite define col2 = proportion min skewness fvproportion
    collect composite define col3 = max kurtosis
    * we can still bind the min/max with parens, but adding the dash is weird
    * so I skip it
    collect style cell result[min], sformat("(%s")
    collect style cell result[max], sformat("%s)")
    * redefine the result autolevels for the layout
    collect style autolevels result col1 col2 col3 _dtable_test, clear
    
    * style the sample statistics as-if they were in the column header
    collect style cell border_block[row-header item], border(top, pattern(none))
    collect style cell var[_N], border( bottom, width(1))
    collect style header var[_N], level(hide)
    
    * force equal widths for the items when producing text output, this does
    * nothing for Word, PDF, HTML, Excel, and Markdown
    collect style column , width(equal)
    
    collect preview
    Here is the resulting table.
    Code:
    . collect preview
    
    -----------------------------------------------------------------------------------------
                                               Female                   Male            Test 
                                           76  (0.07)           1,048  (0.93)                
    -----------------------------------------------------------------------------------------
    Age (in years)                       28.8 (18.000 46.000)    31.7 (17.000 52.000)   0.002
    Last time seronegative for HIV-1     22.1  -0.305   2.017    24.3  -0.353   2.251  <0.001
    First time seropositive for HIV-1    12.0   0.951   2.285    14.4   0.749   3.024   0.020
    -----------------------------------------------------------------------------------------
    Shared needles                                                                           
      No                                   43  (0.57)             679  (0.65)           0.149
      Yes                                  33  (0.43)             369  (0.35)                
    -----------------------------------------------------------------------------------------
    Imprisoned at recruitment                                                                
      No                                   21                     351                   0.315
      Yes                                  55                     697                        
    -----------------------------------------------------------------------------------------
    Injected drugs before recruitment                                                        
      No                                   47                     659                   0.902
      Yes                                  29                     389                        
    -----------------------------------------------------------------------------------------
    Kruskal–Wallis rank test performed for age.
    Poisson regression main effects test performed for ltime and rtime.
    Pearson's chi-squared test performed for needle.
    Fisher's exact test performed for jail and inject.

    Comment


    • #3
      Thank you very much.
      I do not find the code for dtable / collect very intuitive but this has given plenty of information to work with.
      Eddy

      Comment

      Working...
      X