Announcement

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

  • Is it possible to display a header in the first column of a table?

    The following Stata script produces the subsequent table:

    Code:
    use https://www.stata-press.com/data/r18/nhanes2,clear
    
    collect clear
    table (region) (), stat(count bmi) ///
    stat(mean bmi) ///
    stat(sd bmi) ///
    stat(min bmi) ///
    stat(max bmi) nototal ///
    nformat(%4.0f count mean min max) nformat(%4.1f )
    
    *Creating range, which combines min and max.
    collect composite define range = min max, delimiter(", ") trim
    collect style cell result[range]#cell_type[item], sformat((%s))
    
    *Labelling the column headers.
    collect label levels result count "N", modify
    collect label levels result sd "SD", modify
    collect label levels result range "Range", modify
    
    *Specifying the layout.
    collect layout (region) (result[count mean sd range])
    
    *Hiding the super row for region and horizontally aligning.
    collect style header region, title(hide)
    collect style cell result, halign(center)
    collect preview
    ----------------------------------------
    | N Mean SD Range
    ---+------------------------------------
    NE | 2096.0 25.6 4.7 (15.4, 57.1)
    MW | 2774.0 25.5 4.9 (14.1, 61.1)
    S | 2853.0 25.6 5.1 (12.4, 55.4)
    W | 2628.0 25.4 4.9 (15.7, 54.1)
    ----------------------------------------


    The rows are categories of the variable region. Is it possible to label that column "Region", as shown below? If so, how?
    --------------------------------------------
    Region | N Mean SD Range
    -------+------------------------------------
    NE | 2096.0 25.6 4.7 (15.4, 57.1)
    MW | 2774.0 25.5 4.9 (14.1, 61.1)
    S | 2853.0 25.6 5.1 (12.4, 55.4)
    W | 2628.0 25.4 4.9 (15.7, 54.1)
    --------------------------------------------



    Kind regards,
    Suzanna

    After posting I noticed the columns for the tables in this post are not aligned and I can't seem to be able to align them now. Hopefully my question is clear regardless.
    Last edited by Suzanna Vidmar; 25 May 2023, 22:01.

  • #2
    I worked out a solution to my own question. For anyone who's interested, here's my code.

    Code:
    use https://www.stata-press.com/data/r18/nhanes2,clear
    
    collect clear
    table (region) (), stat(count bmi) ///
                       stat(mean bmi) ///
                       stat(sd bmi) ///
                       stat(min bmi) ///
                       stat(max bmi) nototal ///
                       nformat(%4.0f)
                       
    *Creating range, which combines min and max.                   
    collect composite define range = min max, delimiter(", ") trim
    collect style cell result[range]#cell_type[item], sformat((%s))
    
    *Labelling the column headers.
    collect label levels result count "N", modify
    collect label levels result sd "SD", modify
    collect label levels result range "Range", modify
    
    *Specifying the layout.
    collect layout (region) (result[count mean sd range])
    
    *Hiding the super row for region and horizontally aligning.
    collect style header region, title(hide)
    collect style cell result, halign(center)
    collect style putdocx, layout(autofitcontents)
    collect preview
    
    /*This is the current table:
    ----------------------------------------
       |     N     Mean    SD       Range   
    ---+------------------------------------
    NE |  2096.0   25.6   4.7   (15.4, 57.1)
    MW |  2774.0   25.5   4.9   (14.1, 61.1)
    S  |  2853.0   25.6   5.1   (12.4, 55.4)
    W  |  2628.0   25.4   4.9   (15.7, 54.1)
    ----------------------------------------
    
    I want the "Region" header added to the first column:
    --------------------------------------------
    Region |       N   Mean    SD      Range
    -------+------------------------------------
      NE   |  2096.0   25.6   4.7   (15.4, 57.1)
      MW   |  2774.0   25.5   4.9   (14.1, 61.1)
      S    |  2853.0   25.6   5.1   (12.4, 55.4)
      W    |  2628.0   25.4   4.9   (15.7, 54.1)
    --------------------------------------------
    This can be done by giving the table a name and using the -putdocx table- command.
    */
    
    
    capture putdocx clear 
    putdocx begin
    putdocx paragraph
    putdocx text ("Table 1:"), bold font("Times New Roman", 12) 
    putdocx text (" Summary of BMI by Region "), font("Times New Roman", 12)
    
    *-putdocx collect- allows you to export a customized table from a collection to 
    *a table in the active .docx file.
    putdocx collect, tablename(table1)
    
    *Adding column headings.
    putdocx table table1(1,1)=("Region"), halign(left)     
    
    putdocx save "Table 1.docx", replace

    Cheers,
    Suzanna
    Attached Files

    Comment

    Working...
    X