Announcement

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

  • Bolding row/colname dimensions but not their levels

    Hello all: This has been irking me for a while but I cannot get it to work. I need the row variable labels (not each of their levels) bolded in the final word table I am create with the dtable.
    Would like some help with getting the code right. I can get until collect style cell [rowheader] but it is not working as I want it. Below I would like "t2" and "Baseline parms. [IQR]" to be in bold.

    Code:
    sysuse auto, clear
    collect clear 
    putdocx clear
    
    **# Table 1 
    *----------------------
    gen turnbinary = turn > 35
    lab def tlab 0 "Low Turn" 1 "High Turn"
    lab val turnbinary tlab
    
    
    local binvars "i.turnbinary"
    
    dtable c.weight `binvars', by(foreign, tests nototals nomissing) column(by(hide)) ///
            continuous(weight length mpg, statistics(p50 iqi))     ///
                define(iqi = min max, delimiter("-"))             ///
                sformat("[%s]" iqi)                              ///
                nformat(%5.0f iqi)                                 ///
                nformat(%3.1f p50)                                 ///
            factor(`binvars', statistics( fvfreq fvpercent) test(pearson) font( arial,nobold)) ///
            sample(Sample, statistic(frequency percent) place(seplabels)) sformat("N=%s" frequency) ///
    
    
    // Add a tag to nest contvars into a group under Baseline labs
    *------------------------------------------------------------
    collect addtags vargrp[Baseline parms. [IQR]], fortags(var[length mpg])
    
    // Bold column headers and stack 
    *-------------------------------
    collect style cell cell_type[column-header], font(arial, bold)
    collect style row stack, truncate(head)
    collect style cell cell_type[row-header]#turnbinary, font(arial, bold)
    
    
    // Final layout & export
    *-----------------------
    collect layout (vargrp#var var[`binvars']) (foreign#result#_dtable_sample_dim)
    collect preview
    
    collect style putdocx, layout(autofitcontents) ///
    
    collect export "./test.docx", replace

  • #2
    Thanks for the reproducible example. Try something like:

    Code:
    sysuse auto, clear
    collect clear 
    putdocx clear
    
    **# Table 1 
    *----------------------
    gen turnbinary = turn > 35
    lab def tlab 0 "Low Turn" 1 "High Turn"
    lab val turnbinary tlab
    
    
    local binvars "i.turnbinary"
    
    dtable c.weight `binvars', by(foreign, tests nototals nomissing) column(by(hide)) ///
            continuous(weight length mpg, statistics(p50 iqi))     ///
                define(iqi = min max, delimiter("-"))             ///
                sformat("[%s]" iqi)                              ///
                nformat(%5.0f iqi)                                 ///
                nformat(%3.1f p50)                                 ///
            factor(`binvars', statistics( fvfreq fvpercent) test(pearson) font( arial,nobold)) ///
            sample(Sample, statistic(frequency percent) place(seplabels)) sformat("N=%s" frequency) ///
    
    
    // Add a tag to nest contvars into a group under Baseline labs
    *------------------------------------------------------------
    collect addtags vargrp[Baseline parms. [IQR]], fortags(var[length mpg])
    collect addtags vargrp2[turbinary], fortags(turnbinary[0 1])
    // Bold column headers and stack 
    *-------------------------------
    collect style cell cell_type[column-header], font(arial, bold)
    collect style cell vargrp#cell_type[row-header], font(arial,bold)
    collect style cell vargrp2#cell_type[row-header], font(arial,bold)
    collect style cell var[length mpg]#cell_type[row-header], font(arial,nobold)
    collect style cell turnbinary[0 1]#cell_type[row-header], font(arial,nobold)
    collect style row stack, truncate(head)
    
    // Final layout & export
    *-----------------------
    collect layout (vargrp#var vargrp2#turnbinary) (foreign#result#_dtable_sample_dim)
    collect preview
    
    collect style putdocx, layout(autofitcontents) ///
    
    collect export "./test.docx", replace
    Res.:

    Click image for larger version

Name:	Capture.PNG
Views:	1
Size:	18.5 KB
ID:	1737223

    Comment


    • #3
      Thanks much for this, Andrew Musau. This solves the last piece for me in creating my ideal Table 1. I kind of get the logic of how the tags work. Wish -dtable- had an option built-in to do this like Daniel Sjobergs' R::gtsummary does automatically. Perhaps I will throw in a request in the sticky page for wish lists. For now, I will try to see how I can loop your process for any categorical variable dimension names in rows in a generic dtable.

      Comment


      • #4
        Andrew provides the best (to my mind) working solution to the problem of
        bolding factor variable names/labels but not their levels.

        In the following, I provide a slightly modified version of Andrew's idea
        that uses a few subtle features of collect that I want to make
        better known in this community.

        Girish introduces a variable grouping dimension for the continuous
        variables to the collection and Andrew expands on this providing a
        second grouping dimension for the factor variables. We can consolidate
        these two dimensions into one, but use a different dimension name that has
        some helpful built-in features. The idea is to replace vargrp and
        vargrp2 with roweq, fill it with a custom level like Girish
        does originally, and the factor variables names like in Andrew's code.
        This simplifies some style settings and the layout. Also collect
        will pull variable labels from the current frame (dataset) for levels of
        roweq that match variable names.

        I also add/modify some local macros for the variable list groups. The
        weight variable that was specified in the call to dtable
        somehow got lost in the example code, so I add a macro for the
        continuous variables that allows for better targeting and tagging.
        I also treat the binvars macro as if it contains a list of factor
        variables, providing a loop for adding factor variable specific levels of
        roweq that will be targeted for bold in the row header styles.

        Code:
        sysuse auto, clear
        collect clear
        putdocx clear
        
        **# Table 1
        *----------------------
        gen turnbinary = turn > 35
        lab def tlab 0 "Low Turn" 1 "High Turn"
        lab val turnbinary tlab
        * label this variable; later -roweq- will pick it up too
        lab var turnbinary "Turn circle"
        
        * use macro for continuous variables, -weight- got lost in the original code
        local convars weight length mpg
        
        * no need for 'i.' here, we will add it to a spot later
        local binvars turnbinary
        
        * variables specified in options -continuous()- and -factor()- do not
        * need to be specified in the varlist unless you want a special variable
        * order that is otherwise too difficult to get using the options
        dtable, by(foreign, tests nototals nomissing)        ///
            column(by(hide))                ///
            continuous(`convars', statistics(p50 iqi))    ///
            define(iqi = min max, delimiter("-"))        ///
            sformat("[%s]" iqi)                ///
            nformat(%5.0f iqi)                ///
            nformat(%3.1f p50)                ///
            factor(`binvars',                ///
                statistics(fvfreq fvpercent)        ///
                test(pearson)                ///
            )                        ///
                sample(Sample,                    ///
                statistic(frequency percent)        ///
                place(seplabels)            ///
            )                        ///
            sformat("N=%s" frequency)
        
        *------------------------------------------------------------
        * Add -roweq- tag to nest vars into groups; -roweq- is a special
        * dimension that will grab variable labels for it's levels that match
        * variable names in the current frame
        collect addtags roweq[Baseline parms. [IQR]], fortags(var[`convars'])
        foreach b of local binvars {
            collect addtags roweq[`b'], fortags(var[i.`b'])
            * hide this factor variable's title since we plan to use this
            * -roweq- level to title this variable in the header
            collect style header `b', title(hide)
        }
        collect style header roweq, title(hide) level(label)
        
        *-------------------------------
        * bold all the column headers
        collect style cell cell_type[column-header], font(arial, bold)
        * bold the levels of -roweq-
        collect style cell roweq#cell_type[row-header], font(arial, bold)
        * unbold the variable names/labels
        collect style cell var#cell_type[row-header], font(arial, nobold)
        collect style row stack, truncate(head)
        
        // Final layout & export
        *-----------------------
        collect layout (roweq#var) (foreign#result#_dtable_sample_dim)
        
        collect style putdocx, layout(autofitcontents) ///
        
        collect export "./test.docx", replace
        I additionally exported to HTML in my Stata session, here is a
        screenshot of the resul from my browser.


        Click image for larger version

Name:	Screenshot 2023-12-15 at 11.16.17 AM.png
Views:	1
Size:	57.6 KB
ID:	1737285

        Comment


        • #5
          Love it, Jeff Pitblado (StataCorp) . Good to learn about -roweq-. Between #2 from Andrew Musau and your #3, I envisage that the much of your code should be quite commutable across different datasets without having rewrite those -collect styles-(arial, bold) lines for each binvar/contvar groups I may want to bold.

          Comment

          Working...
          X