Announcement

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

  • #16
    Originally posted by Trixi Bo View Post
    You mean like this?
    Yes, this is fine - thanks. In your setup, the dimension is named "var" and both the categorical and continuous variables are levels of this dimension. So you just need to tweak the code slightly (see highlighted).

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input byte(SD14 SD17 SD18 SD5_h SD6_h) float LK_group byte(SD26 SD27) float hourly_wage_eb
    1 1 2 3 4 3 1  1  .
    2 1 2 3 4 4 3  3  .
    3 1 2 2 1 4 3  3 12
    1 1 2 3 1 . 1  1  .
    5 1 1 3 4 1 1  3 42
    3 1 2 3 1 3 1  1 29
    3 1 2 1 2 1 1  3 27
    2 1 2 3 4 4 3  3 14
    3 3 2 3 4 1 1  3  8
    3 1 2 3 4 1 1  3 20
    3 1 2 3 4 1 1  3 33
    3 1 2 3 4 1 1  3 12
    3 1 2 3 4 3 1  1  .
    3 1 2 3 4 5 5 80  .
    3 1 2 3 3 1 1  3 37
    3 1 2 2 1 3 1  1 15
    3 1 2 3 4 3 1  1 33
    4 1 1 2 4 4 3  3  .
    5 1 2 3 4 1 1  3  7
    3 3 1 1 4 1 1  3 32
    end
    label values SD14 SD14_mod
    label def SD14_mod 1 "Ledig", modify
    label def SD14_mod 2 "Lebe mit Partner bzw. Partnerin in einem Haushalt", modify
    label def SD14_mod 3 "Verheiratet oder eingetragene Partnerschaft", modify
    label def SD14_mod 4 "Verwitwet", modify
    label def SD14_mod 5 "Geschieden", modify
    label values SD17 SD17_mod
    label def SD17_mod 1 "In Deutschland / Im Gebiet des heutigen Deutschlands", modify
    label def SD17_mod 3 "Im Ausland / In einem anderen Land", modify
    label values SD18 SD18_mod
    label def SD18_mod 1 "Ja, mindestens ein Elternteil wurde ausserhalb von Deutschland geboren", modify
    label def SD18_mod 2 "Nein, beide Elternteile wurden in Deutschland bzw. den ehemaligen deutschen Staatsgebieten geboren", modify
    label values SD5_h SD5_hlbl_mod
    label def SD5_hlbl_mod 1 "kein/HS/sonstiges", modify
    label def SD5_hlbl_mod 2 "MR", modify
    label def SD5_hlbl_mod 3 "FHR/Abi", modify
    label values SD6_h SD6_hlbl_mod
    label def SD6_hlbl_mod 1 "Lehre/Berufsausbildung/anderer berufsqual.", modify
    label def SD6_hlbl_mod 2 "Meister- oder Technikerausbildung", modify
    label def SD6_hlbl_mod 3 "Abschluss einer Berufsakademie oder einer dualen Hochschule", modify
    label def SD6_hlbl_mod 4 "Uni/(F)H-Abschluss", modify
    label values LK_group LK_grouplbl
    label def LK_grouplbl 1 "(Solo-)Selbständig", modify
    label def LK_grouplbl 3 "Hybride Beschäftigung", modify
    label def LK_grouplbl 4 "Rente", modify
    label def LK_grouplbl 5 "sonstiges/unklar", modify
    label values SD26 SD26_mod
    label def SD26_mod 1 "Erwerbstaetig", modify
    label def SD26_mod 3 "In Rente", modify
    label def SD26_mod 5 "Nicht erwerbstaetig", modify
    label values SD27 SD27_mod
    label def SD27_mod 1 "Ja, ein Beschaeftigungsverhaeltnis als Angestellter oder Beamter", modify
    label def SD27_mod 3 "Nein, kein Beschaeftigungsverhaeltnis als Angestellter oder Beamter", modify
    label def SD27_mod 80 "Ausgefiltert", modify
    
    
    local fvars SD14 SD17 SD18 SD5_h SD6_h SD26 SD27
    local cvars hourly_wage_eb
    set seed 10232025
    replace LK_group= runiformint(3,4)
    collect clear
    
    
    dtable `fvars' `cvars', ///
    by(LK_group, nototals) ///
    column(by(hide)) ///
    sample(, place(seplabels) ) ///
    continuous(, statistics(p50 mean)) ///
    factor(, statistic(fvfrequency fvpercent)) ///
    export("table1", as(docx) replace)
    
    collect style cell var, font(Arial, size(10))
    collect style cell result[p50 mean], nformat(%3.1f)
    collect style cell result[fvpercent], nformat(%16.2fc)
    collect style cell cell_type[column-header], font(Arial, size(11) color(black) bold)
    collect style cell cell_type[row-header], halign(right)
    collect style row stack, spacer
    
    *CHECK TABLE DIMENSIONS 
    collect dims
    *CHECK LEVELS OF DIMENSION "var"
    collect levelsof var
    
    collect preview
    collect style cell var[`fvars']#cell_type[row-header], font(arial,bold)
    collect preview
    Res.:

    Code:
    . *CHECK TABLE DIMENSIONS 
    . collect dims
    
    Collection dimensions
    Collection: DTable
    -----------------------------------------
                       Dimension   No. levels
    -----------------------------------------
    Layout, style, header, label
                        LK_group   2         
              _dtable_sample_dim   2         
                          across   2         
                          cmdset   1         
                         colname   8         
                         command   1         
                          result   7         
                         statcmd   4         
                             var   10        
    
    Style only
                    border_block   4         
                       cell_type   4         
    -----------------------------------------
    
    . *CHECK LEVELS OF DIMENSION "var"
    . collect levelsof var
    
    Collection: DTable
     Dimension: var
        Levels: _N _hide SD14 SD17 SD18 SD5_h SD6_h SD26 SD27 hourly_wage_eb

    Click image for larger version

Name:	Capture.PNG
Views:	1
Size:	22.3 KB
ID:	1782597

    Comment


    • #17
      In case you specify the categorical variables in dtable using factor variable notation (i.e., as i.catvar), then you can identify these in the stored levels as having periods in their names. Here, you'd need to loop. But one could also extract them and avoid looping. I show the former.

      Code:
      * Example generated by -dataex-. For more info, type help dataex
      clear
      input byte(SD14 SD17 SD18 SD5_h SD6_h) float LK_group byte(SD26 SD27) float hourly_wage_eb
      1 1 2 3 4 3 1  1  .
      2 1 2 3 4 4 3  3  .
      3 1 2 2 1 4 3  3 12
      1 1 2 3 1 . 1  1  .
      5 1 1 3 4 1 1  3 42
      3 1 2 3 1 3 1  1 29
      3 1 2 1 2 1 1  3 27
      2 1 2 3 4 4 3  3 14
      3 3 2 3 4 1 1  3  8
      3 1 2 3 4 1 1  3 20
      3 1 2 3 4 1 1  3 33
      3 1 2 3 4 1 1  3 12
      3 1 2 3 4 3 1  1  .
      3 1 2 3 4 5 5 80  .
      3 1 2 3 3 1 1  3 37
      3 1 2 2 1 3 1  1 15
      3 1 2 3 4 3 1  1 33
      4 1 1 2 4 4 3  3  .
      5 1 2 3 4 1 1  3  7
      3 3 1 1 4 1 1  3 32
      end
      label values SD14 SD14_mod
      label def SD14_mod 1 "Ledig", modify
      label def SD14_mod 2 "Lebe mit Partner bzw. Partnerin in einem Haushalt", modify
      label def SD14_mod 3 "Verheiratet oder eingetragene Partnerschaft", modify
      label def SD14_mod 4 "Verwitwet", modify
      label def SD14_mod 5 "Geschieden", modify
      label values SD17 SD17_mod
      label def SD17_mod 1 "In Deutschland / Im Gebiet des heutigen Deutschlands", modify
      label def SD17_mod 3 "Im Ausland / In einem anderen Land", modify
      label values SD18 SD18_mod
      label def SD18_mod 1 "Ja, mindestens ein Elternteil wurde ausserhalb von Deutschland geboren", modify
      label def SD18_mod 2 "Nein, beide Elternteile wurden in Deutschland bzw. den ehemaligen deutschen Staatsgebieten geboren", modify
      label values SD5_h SD5_hlbl_mod
      label def SD5_hlbl_mod 1 "kein/HS/sonstiges", modify
      label def SD5_hlbl_mod 2 "MR", modify
      label def SD5_hlbl_mod 3 "FHR/Abi", modify
      label values SD6_h SD6_hlbl_mod
      label def SD6_hlbl_mod 1 "Lehre/Berufsausbildung/anderer berufsqual.", modify
      label def SD6_hlbl_mod 2 "Meister- oder Technikerausbildung", modify
      label def SD6_hlbl_mod 3 "Abschluss einer Berufsakademie oder einer dualen Hochschule", modify
      label def SD6_hlbl_mod 4 "Uni/(F)H-Abschluss", modify
      label values LK_group LK_grouplbl
      label def LK_grouplbl 1 "(Solo-)Selbständig", modify
      label def LK_grouplbl 3 "Hybride Beschäftigung", modify
      label def LK_grouplbl 4 "Rente", modify
      label def LK_grouplbl 5 "sonstiges/unklar", modify
      label values SD26 SD26_mod
      label def SD26_mod 1 "Erwerbstaetig", modify
      label def SD26_mod 3 "In Rente", modify
      label def SD26_mod 5 "Nicht erwerbstaetig", modify
      label values SD27 SD27_mod
      label def SD27_mod 1 "Ja, ein Beschaeftigungsverhaeltnis als Angestellter oder Beamter", modify
      label def SD27_mod 3 "Nein, kein Beschaeftigungsverhaeltnis als Angestellter oder Beamter", modify
      label def SD27_mod 80 "Ausgefiltert", modify
      
      
      local fvars SD14 SD17 SD18 SD5_h SD6_h SD26 SD27
      local cvars hourly_wage_eb
      set seed 10232025
      replace LK_group= runiformint(3,4)
      collect clear
      
      
      dtable i.(`fvars') `cvars', ///
      by(LK_group, nototals) ///
      column(by(hide)) ///
      sample(, place(seplabels) ) ///
      continuous(, statistics(p50 mean)) ///
      factor(, statistic(fvfrequency fvpercent)) ///
      export("table1", as(docx) replace) 
      
      collect style cell var, font(Arial, size(10))
      collect style cell result[p50 mean], nformat(%3.1f)
      collect style cell result[fvpercent], nformat(%16.2fc)
      collect style cell cell_type[column-header], font(Arial, size(11) color(black) bold)
      collect style cell cell_type[row-header], halign(right)
      collect style row stack, spacer
      
      *CHECK TABLE DIMENSIONS 
      collect dims
      *CHECK LEVELS OF DIMENSION "var"
      collect levelsof var
      
      collect preview
      qui collect levelsof var
      foreach var in `s(levels)'{
          if regexm("`var'", "\."){
              collect style cell var[`var']#cell_type[row-header], font(arial,bold)
          }
      }
      collect preview
      Res.:

      Click image for larger version

Name:	Capture.PNG
Views:	1
Size:	34.7 KB
ID:	1782599


      Comment


      • #18
        Hm, maybe I should post the result, what the table sholuld look like - because now, everythin is bolded (like in your example), but not only the headings. The table I would like to produce, shuld look like this (this is not via stata):

        Attached Files

        Comment


        • #19
          Originally posted by Trixi Bo View Post
          Hm, maybe I should post the result, what the table sholuld look like - because now, everything is bolded (like in your example), but not only the headings. The table I would like to produce, should look like this (this is not via stata):
          Ah! But Jeff's post in this thread shows you exactly how to do this.

          Code:
          * Example generated by -dataex-. For more info, type help dataex
          clear
          input byte(SD14 SD17 SD18 SD5_h SD6_h) float LK_group byte(SD26 SD27) float hourly_wage_eb
          1 1 2 3 4 3 1  1  .
          2 1 2 3 4 4 3  3  .
          3 1 2 2 1 4 3  3 12
          1 1 2 3 1 . 1  1  .
          5 1 1 3 4 1 1  3 42
          3 1 2 3 1 3 1  1 29
          3 1 2 1 2 1 1  3 27
          2 1 2 3 4 4 3  3 14
          3 3 2 3 4 1 1  3  8
          3 1 2 3 4 1 1  3 20
          3 1 2 3 4 1 1  3 33
          3 1 2 3 4 1 1  3 12
          3 1 2 3 4 3 1  1  .
          3 1 2 3 4 5 5 80  .
          3 1 2 3 3 1 1  3 37
          3 1 2 2 1 3 1  1 15
          3 1 2 3 4 3 1  1 33
          4 1 1 2 4 4 3  3  .
          5 1 2 3 4 1 1  3  7
          3 3 1 1 4 1 1  3 32
          end
          label values SD14 SD14_mod
          label def SD14_mod 1 "Ledig", modify
          label def SD14_mod 2 "Lebe mit Partner bzw. Partnerin in einem Haushalt", modify
          label def SD14_mod 3 "Verheiratet oder eingetragene Partnerschaft", modify
          label def SD14_mod 4 "Verwitwet", modify
          label def SD14_mod 5 "Geschieden", modify
          label values SD17 SD17_mod
          label def SD17_mod 1 "In Deutschland / Im Gebiet des heutigen Deutschlands", modify
          label def SD17_mod 3 "Im Ausland / In einem anderen Land", modify
          label values SD18 SD18_mod
          label def SD18_mod 1 "Ja, mindestens ein Elternteil wurde ausserhalb von Deutschland geboren", modify
          label def SD18_mod 2 "Nein, beide Elternteile wurden in Deutschland bzw. den ehemaligen deutschen Staatsgebieten geboren", modify
          label values SD5_h SD5_hlbl_mod
          label def SD5_hlbl_mod 1 "kein/HS/sonstiges", modify
          label def SD5_hlbl_mod 2 "MR", modify
          label def SD5_hlbl_mod 3 "FHR/Abi", modify
          label values SD6_h SD6_hlbl_mod
          label def SD6_hlbl_mod 1 "Lehre/Berufsausbildung/anderer berufsqual.", modify
          label def SD6_hlbl_mod 2 "Meister- oder Technikerausbildung", modify
          label def SD6_hlbl_mod 3 "Abschluss einer Berufsakademie oder einer dualen Hochschule", modify
          label def SD6_hlbl_mod 4 "Uni/(F)H-Abschluss", modify
          label values LK_group LK_grouplbl
          label def LK_grouplbl 1 "(Solo-)Selbständig", modify
          label def LK_grouplbl 3 "Hybride Beschäftigung", modify
          label def LK_grouplbl 4 "Rente", modify
          label def LK_grouplbl 5 "sonstiges/unklar", modify
          label values SD26 SD26_mod
          label def SD26_mod 1 "Erwerbstaetig", modify
          label def SD26_mod 3 "In Rente", modify
          label def SD26_mod 5 "Nicht erwerbstaetig", modify
          label values SD27 SD27_mod
          label def SD27_mod 1 "Ja, ein Beschaeftigungsverhaeltnis als Angestellter oder Beamter", modify
          label def SD27_mod 3 "Nein, kein Beschaeftigungsverhaeltnis als Angestellter oder Beamter", modify
          label def SD27_mod 80 "Ausgefiltert", modify
          
          
          local fvars SD14 SD17 SD18 SD5_h SD6_h SD26 SD27
          local cvars hourly_wage_eb
          set seed 10232025
          replace LK_group= runiformint(3,4)
          collect clear
          
          
          dtable i.(`fvars') `cvars', by(LK_group, nototals) ///
          sample(, place(seplabels) ) ///
          continuous(, statistics(p50 mean)) ///
          factor(, statistic(fvfrequency fvpercent)) ///
          export("table1", as(docx) replace)
          
          collect style cell var, font(Arial, size(10))
          collect style cell result[p50 mean], nformat(%3.1f)
          collect style cell result[fvpercent], nformat(%16.2fc)
          collect style cell cell_type[column-header], font(Arial, size(11) color(black) bold)
          collect style cell cell_type[row-header], halign(right)
          collect style row stack, spacer
          
          collect dims
          collect levelsof var
          
          foreach var of local fvars {
              collect addtags roweq[`var'], fortags(var[i.`var'])
              * hide this factor variable's title since we plan to use this
              * -roweq- level to title this variable in the header
              collect style header `var', title(hide)
          }
          
          collect layout (roweq#var) (LK_group#result#_dtable_sample_dim)
          
          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 preview
          Res.:
          Click image for larger version

Name:	Capture.PNG
Views:	1
Size:	114.5 KB
ID:	1782610

          Comment


          • #20
            Originally posted by Andrew Musau View Post

            Ah! But Jeff's post in this thread shows you exactly how to do this.

            Code:
            * Example generated by -dataex-. For more info, type help dataex
            clear
            input byte(SD14 SD17 SD18 SD5_h SD6_h) float LK_group byte(SD26 SD27) float hourly_wage_eb
            1 1 2 3 4 3 1 1 .
            2 1 2 3 4 4 3 3 .
            3 1 2 2 1 4 3 3 12
            1 1 2 3 1 . 1 1 .
            5 1 1 3 4 1 1 3 42
            3 1 2 3 1 3 1 1 29
            3 1 2 1 2 1 1 3 27
            2 1 2 3 4 4 3 3 14
            3 3 2 3 4 1 1 3 8
            3 1 2 3 4 1 1 3 20
            3 1 2 3 4 1 1 3 33
            3 1 2 3 4 1 1 3 12
            3 1 2 3 4 3 1 1 .
            3 1 2 3 4 5 5 80 .
            3 1 2 3 3 1 1 3 37
            3 1 2 2 1 3 1 1 15
            3 1 2 3 4 3 1 1 33
            4 1 1 2 4 4 3 3 .
            5 1 2 3 4 1 1 3 7
            3 3 1 1 4 1 1 3 32
            end
            label values SD14 SD14_mod
            label def SD14_mod 1 "Ledig", modify
            label def SD14_mod 2 "Lebe mit Partner bzw. Partnerin in einem Haushalt", modify
            label def SD14_mod 3 "Verheiratet oder eingetragene Partnerschaft", modify
            label def SD14_mod 4 "Verwitwet", modify
            label def SD14_mod 5 "Geschieden", modify
            label values SD17 SD17_mod
            label def SD17_mod 1 "In Deutschland / Im Gebiet des heutigen Deutschlands", modify
            label def SD17_mod 3 "Im Ausland / In einem anderen Land", modify
            label values SD18 SD18_mod
            label def SD18_mod 1 "Ja, mindestens ein Elternteil wurde ausserhalb von Deutschland geboren", modify
            label def SD18_mod 2 "Nein, beide Elternteile wurden in Deutschland bzw. den ehemaligen deutschen Staatsgebieten geboren", modify
            label values SD5_h SD5_hlbl_mod
            label def SD5_hlbl_mod 1 "kein/HS/sonstiges", modify
            label def SD5_hlbl_mod 2 "MR", modify
            label def SD5_hlbl_mod 3 "FHR/Abi", modify
            label values SD6_h SD6_hlbl_mod
            label def SD6_hlbl_mod 1 "Lehre/Berufsausbildung/anderer berufsqual.", modify
            label def SD6_hlbl_mod 2 "Meister- oder Technikerausbildung", modify
            label def SD6_hlbl_mod 3 "Abschluss einer Berufsakademie oder einer dualen Hochschule", modify
            label def SD6_hlbl_mod 4 "Uni/(F)H-Abschluss", modify
            label values LK_group LK_grouplbl
            label def LK_grouplbl 1 "(Solo-)Selbständig", modify
            label def LK_grouplbl 3 "Hybride Beschäftigung", modify
            label def LK_grouplbl 4 "Rente", modify
            label def LK_grouplbl 5 "sonstiges/unklar", modify
            label values SD26 SD26_mod
            label def SD26_mod 1 "Erwerbstaetig", modify
            label def SD26_mod 3 "In Rente", modify
            label def SD26_mod 5 "Nicht erwerbstaetig", modify
            label values SD27 SD27_mod
            label def SD27_mod 1 "Ja, ein Beschaeftigungsverhaeltnis als Angestellter oder Beamter", modify
            label def SD27_mod 3 "Nein, kein Beschaeftigungsverhaeltnis als Angestellter oder Beamter", modify
            label def SD27_mod 80 "Ausgefiltert", modify
            
            
            local fvars SD14 SD17 SD18 SD5_h SD6_h SD26 SD27
            local cvars hourly_wage_eb
            set seed 10232025
            replace LK_group= runiformint(3,4)
            collect clear
            
            
            dtable i.(`fvars') `cvars', by(LK_group, nototals) ///
            sample(, place(seplabels) ) ///
            continuous(, statistics(p50 mean)) ///
            factor(, statistic(fvfrequency fvpercent)) ///
            export("table1", as(docx) replace)
            
            collect style cell var, font(Arial, size(10))
            collect style cell result[p50 mean], nformat(%3.1f)
            collect style cell result[fvpercent], nformat(%16.2fc)
            collect style cell cell_type[column-header], font(Arial, size(11) color(black) bold)
            collect style cell cell_type[row-header], halign(right)
            collect style row stack, spacer
            
            collect dims
            collect levelsof var
            
            foreach var of local fvars {
            collect addtags roweq[`var'], fortags(var[i.`var'])
            * hide this factor variable's title since we plan to use this
            * -roweq- level to title this variable in the header
            collect style header `var', title(hide)
            }
            
            collect layout (roweq#var) (LK_group#result#_dtable_sample_dim)
            
            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 preview
            Res.: [ATTACH=CONFIG]n1782610[/ATTACH]
            Now, a final "Thank you very much"! I am not sure, why that didn´t work in the beginning, but now, it works and I finally got the correct table! Thanks again for your patience!

            Comment


            • #21
              Originally posted by Andrew Musau View Post
              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.:

              [ATTACH=CONFIG]n1737223[/ATTACH]
              Thank you for this really helpful post. I have tried to implement this in my code below, where I have inputted dummy data. What I am trying to achieve is to have bold sub-titles of Age, Gender and Diagnosis and under the heading of Age to have the following:

              Mean (SD)
              Median (IQR)
              Range

              This is the code I have so far:

              Code:
              * Example generated by -dataex-. For more info, type help dataex
              
              clear all
              
              *DUMMY DATA
              input float(Gender_BL Age_BL Diagnosis_BL) str4 group
              1 18 2 "A"
              0 22 3 "A"
              0 20 2 "A"
              0 21 1 "B"
              1 25 1 "B"
              1 24 2 "B"
              end
              label values Gender_BL Gender_BL
              label def Gender_BL 0 "Female", modify
              label def Gender_BL 1 "Male", modify
              label values Diagnosis_BL Diagnosis_BL
              label def Diagnosis_BL 1 "Anxiety", modify
              label def Diagnosis_BL 2 "Depression", modify
              label def Diagnosis_BL 3 "Psychosis", modify
              
              *BASELINE TABLE
              dtable Age_BL i.Gender_BL i.Diagnosis, by(group) nformat(%9.1g) ///
              column(by(hide)) ///
              define(iqi = q1 q3, delimiter(" to ")) ///
              define(rangei = min max, delimiter(" to ")) ///
              continuous(Age_BL, statistic(mean p50 iqi rangei)) ///
              sample(Sample, statistic(frequency) place(seplabels)) sformat("(N=%s)" frequency) ///
              title(Table 1 Baseline characteristics, by randomised group.)
              
              *FORMATTING BASELINE TABLE
              collect addtags vargrp["Age (years)"], fortags(Age_BL[mean p50 iqi rangei])
              *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 var[mean p50 iqi rangei]#cell_type[row-header], font(arial,nobold)
              collect style row stack, truncate(head)
              *Final layout & export
              collect preview
              Many thanks,
              Jenna

              Comment


              • #22
                This is covered in the earlier posts. #4 in this thread gives you a superior alternative to the method I show here (similar to #3).

                Code:
                * Example generated by -dataex-. For more info, type help dataex
                
                clear all
                collect clear
                *DUMMY DATA
                input float(Gender_BL Age_BL Diagnosis_BL) str4 group
                1 18 2 "A"
                0 22 3 "A"
                0 20 2 "A"
                0 21 1 "B"
                1 25 1 "B"
                1 24 2 "B"
                end
                label values Gender_BL Gender_BL
                label def Gender_BL 0 "Female", modify
                label def Gender_BL 1 "Male", modify
                label values Diagnosis_BL Diagnosis_BL
                label def Diagnosis_BL 1 "Anxiety", modify
                label def Diagnosis_BL 2 "Depression", modify
                label def Diagnosis_BL 3 "Psychosis", modify
                
                *BASELINE TABLE
                dtable Age_BL i.Gender_BL i.Diagnosis, by(group) nformat(%9.1g) ///
                column(by(hide)) ///
                define(iqi = q1 q3, delimiter(" to ")) ///
                define(rangei = min max, delimiter(" to ")) ///
                continuous(Age_BL, statistic(mean p50 iqi rangei)) ///
                sample(Sample, statistic(frequency) place(seplabels)) sformat("(N=%s)" frequency) ///
                title(Table 1 Baseline characteristics, by randomised group.)
                
                *CHECK THE TABLE'S LAYOUT
                collect layout
                
                *CHECK LEVELS OF THE VAR DIMENSION
                collect levelsof var
                
                *CREATE GROUPING DIMENSIONS FOR FACTOR VARIABLES
                collect addtags vargrp1[Gender], fortags(var[0.Gender_BL 1.Gender_BL])
                collect addtags vargrp2[Diagnosis], fortags(var[1.Diagnosis_BL 2.Diagnosis_BL 3.Diagnosis_BL])
                
                *BOLD AS DESIRED
                collect style cell var[Age_BL]#cell_type[row-header], font(arial,bold)
                collect style cell vargrp1#cell_type[row-header], font(arial,bold)
                collect style cell vargrp2#cell_type[row-header], font(arial,bold)
                collect style cell var[0.Gender_BL 1.Gender_BL 1.Diagnosis_BL 2.Diagnosis_BL ///
                3.Diagnosis_BL]#cell_type[row-header], font(arial,nobold)
                
                *HIDE TITLES OF FACTOR VARIABLES
                collect style header var[0.Gender_BL 1.Gender_BL 1.Diagnosis_BL 2.Diagnosis_BL 3.Diagnosis_BL], title(hide) level(label)
                
                *CREATE DESIRED TABLE
                collect layout (var[Age_BL] vargrp1#var[0.Gender_BL 1.Gender_BL] ///
                vargrp2#var[1.Diagnosis_BL 2.Diagnosis_BL 3.Diagnosis_BL] ) ///
                (group#result#_dtable_sample_dim)

                Res.:
                Click image for larger version

Name:	Screenshot 2026-03-19 181844.png
Views:	1
Size:	33.2 KB
ID:	1785356


                Comment


                • #23
                  Thank you Andrew Musau. I did try to implement what was above but I'm very new to using dtable. I really appreciate your assistance.
                  Is there a way to get the mean, median, IQR and range of ages on different lines?
                  Or is the only way to do this is to generate them as their own varaibles then use collect?

                  Many thanks,
                  Jenna

                  Comment


                  • #24
                    Thank you Andrew Musau. I did try to implement what was above but I'm very new to using dtable. I really appreciate your assistance.
                    Is there a way to get the mean, median, IQR and range of ages on different lines?
                    Or is the only way to do this is to generate them as their own varaibles then use collect?

                    Many thanks,
                    Jenna

                    Comment


                    • #25
                      Originally posted by Jenna Grabey View Post
                      Is there a way to get the mean, median, IQR and range of ages on different lines?
                      It's just a matter of changing the layout of the table.

                      What I am trying to achieve is to have the heading of Age to have the following:

                      Mean (SD)
                      Median (IQR)
                      Range
                      You need to define composite statistics for this.

                      Code:
                      * Example generated by -dataex-. For more info, type help dataex
                      
                      clear all
                      collect clear
                      *DUMMY DATA
                      input float(Gender_BL Age_BL Diagnosis_BL) str4 group
                      1 18 2 "A"
                      0 22 3 "A"
                      0 20 2 "A"
                      0 21 1 "B"
                      1 25 1 "B"
                      1 24 2 "B"
                      end
                      label values Gender_BL Gender_BL
                      label def Gender_BL 0 "Female", modify
                      label def Gender_BL 1 "Male", modify
                      label values Diagnosis_BL Diagnosis_BL
                      label def Diagnosis_BL 1 "Anxiety", modify
                      label def Diagnosis_BL 2 "Depression", modify
                      label def Diagnosis_BL 3 "Psychosis", modify
                      
                      *BASELINE TABLE, CREATING CUSTOM STATS
                      dtable Age_BL i.Gender_BL i.Diagnosis, by(group) nformat(%9.1g) ///
                      column(by(hide)) ///
                      define(iqi = q1 q3, delimiter("-")) sformat("[%s]" iqi) ///
                      define(stat2 = p50 iqi, delimiter(" ")) ///
                      define(stat1 = mean sd, delimiter(" ")) ///
                      define(rangei = min max, delimiter(" to "))  ///
                      continuous(Age_BL, statistic(stat1 stat2 rangei)) ///
                      sample(Sample, statistic(frequency) place(seplabels)) sformat("(N=%s)" frequency) ///
                      title(Table 1 Baseline characteristics, by randomised group.)
                      
                      *LABEL CUSTOM STATS
                      collect label levels result stat1 "Mean (SD)", modify
                      collect label levels result stat2 "Median [IQR]", modify
                      collect label levels result rangei "Range", modify
                      
                      *CHECK THE TABLE'S LAYOUT
                      collect layout
                      
                      *CHECK LEVELS OF THE VAR DIMENSION
                      collect levelsof var
                      
                      *CREATE GROUPING DIMENSIONS FOR VARIABLES
                      collect addtags vargrp1[Gender], fortags(var[0.Gender_BL 1.Gender_BL])
                      collect addtags vargrp2[Diagnosis], fortags(var[1.Diagnosis_BL 2.Diagnosis_BL 3.Diagnosis_BL])
                      
                      *BOLD AS DESIRED
                      collect style cell var[Age_BL]#cell_type[row-header], font(arial,bold)
                      collect style cell vargrp1#cell_type[row-header], font(arial,bold)
                      collect style cell vargrp2#cell_type[row-header], font(arial,bold)
                      collect style cell var[0.Gender_BL 1.Gender_BL 1.Diagnosis_BL 2.Diagnosis_BL ///
                      3.Diagnosis_BL]#cell_type[row-header], font(arial,nobold)
                      collect style cell result[stat1 stat2 rangei], font(arial,nobold)
                      
                      *HIDE TITLES OF VARIABLES
                      collect style header var[0.Gender_BL 1.Gender_BL 1.Diagnosis_BL 2.Diagnosis_BL 3.Diagnosis_BL], title(hide) level(label)
                      collect style header result[stat1 stat2 rangei], title(hide) level(label)
                      
                      *CREATE DESIRED TABLE
                      collect layout (var[Age_BL]#result[stat1] var[Age_BL]#result[stat2] ///
                      var[Age_BL]#result[rangei] vargrp1#var[0.Gender_BL 1.Gender_BL]#result ///
                      vargrp2#var[1.Diagnosis_BL 2.Diagnosis_BL 3.Diagnosis_BL]#result ) ///
                      (group#_dtable_sample_dim)
                      
                      collect export mytable, as(docx) replace
                      Click image for larger version

Name:	Screenshot 2026-03-20 163938.png
Views:	1
Size:	34.7 KB
ID:	1785390

                      Comment


                      • #26
                        Wonderful thank you so much!

                        Comment

                        Working...
                        X