Announcement

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

  • Formatting Issue in dtable: How to Collapse Binary Variables into a Compact Table?

    Hello everyone,

    I am using dtable in Stata to summarize binary variables (hasSepsis, hasInfection, hasUTI, hasOther) by a treatment variable (treated). However, my table output is not quite how I would like.

    I would like to have the results on one row with the corresponding variable name.

    I know how to hide the rows with the variable names and only show the rows with the results, but i'd rather not have to manually re-label all the "1"s to something meaningful. Rather I'm looking for a way to shift the results up to the variable name.

    Current output:
    Code:
    --------------------------------------------
                  Control   Treated      All    
    --------------------------------------------
    N            3 (30.0%) 7 (70.0%) 10 (100.0%)
    hasSepsis                                   
      1          1 (33.3%)  0 (0.0%)   1 (10.0%)
    hasInfection                                
      1          2 (66.7%)  0 (0.0%)   2 (20.0%)
    hasUTI                                      
      1           0 (0.0%) 1 (14.3%)   1 (10.0%)
    hasOther                                    
      1           0 (0.0%)  0 (0.0%)    0 (0.0%)
    --------------------------------------------

    Desired output, with results on one row with the variable name:
    Code:
    -------------------------------------------------
                  Control    Treated     All    
    -------------------------------------------------
    N             3 (30.0%)  7 (70.0%)   10 (100.0%)
    hasSepsis     1 (33.3%)  0 (0.0%)     1 (10.0%)
    hasInfection  2 (66.7%)  0 (0.0%)     2 (20.0%)
    hasUTI        0 (0.0%)   1 (14.3%)    1 (10.0%)
    hasOther      0 (0.0%)   0 (0.0%)     0 (0.0%)
    -------------------------------------------------
    My code:
    Code:
    clear
    input float treated byte(hasSepsis hasInfection hasUTI hasOther)
    1 0 0 0 0
    0 1 1 0 0
    1 0 0 0 0
    0 0 1 0 0
    1 0 0 1 0
    0 0 0 0 0
    1 0 0 0 0
    1 0 0 0 0
    1 0 0 0 0
    1 0 0 0 0
    end
    
    label values treated treated_lab
    label def treated_lab 0 "Control" 1 "Treated", modify
    
    dtable 1.has*, ///
    by(treated) ///
    column(by(hide) total(All) test(P-value))




  • #2
    After your dtable command, do the following:
    Code:
    collect style header hasSepsis hasInfection hasUTI hasOther, level(hide)
    collect preview

    Comment


    • #3
      Thank you Hemanshu Kumar!

      This gave me the wanted output:
      Code:
      --------------------------------------------
                    Control   Treated      All    
      --------------------------------------------
      N            3 (30.0%) 7 (70.0%) 10 (100.0%)
      hasSepsis    1 (33.3%)  0 (0.0%)   1 (10.0%)
      hasInfection 2 (66.7%)  0 (0.0%)   2 (20.0%)
      hasUTI        0 (0.0%) 1 (14.3%)   1 (10.0%)
      hasOther      0 (0.0%)  0 (0.0%)    0 (0.0%)
      --------------------------------------------

      Comment


      • #4
        If you want to fix the alignment issue that is causing the table in #3 to look a little different from the one in #1, you can additionally throw in
        Code:
        collect style cell, halign(left)
        so that you get
        Code:
        . collect preview
        
        --------------------------------------------
                     Control   Treated   All        
        --------------------------------------------
        N            3 (30.0%) 7 (70.0%) 10 (100.0%)
        hasSepsis    1 (33.3%) 0 (0.0%)  1 (10.0%)  
        hasInfection 2 (66.7%) 0 (0.0%)  2 (20.0%)  
        hasUTI       0 (0.0%)  1 (14.3%) 1 (10.0%)  
        hasOther     0 (0.0%)  0 (0.0%)  0 (0.0%)   
        --------------------------------------------

        Comment

        Working...
        X