Announcement

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

  • Combine two tables using collect and table

    I built a sample description table in Stata 17 and have recently switched over to Stata 18. I want to combine two tables. The first table summarizes the sample selection criterion and tells me how many people are included in the regressions. The second table summarizes the sex and age only for those people included. My MRE looks like this at the moment:

    Code:
      
    clear all
     input id inclusion age sex
    1 1 35 0
    2 1 56 1
    3 0 78 1
    3 1 43 1
    4 0 3 0
    5 1 40 0
     6 1 5 0
    7 0 55 1
    8 1 31 0
    9 1 26 0
    10 0 34 1
    11 0 45 1
    12 0 67 0
    end  
    putdocx begin
    table () () (), statistic(mean inclusion) statistic(sd inclusion) statistic(min inclusion) statistic(max inclusion) nformat(%6.2f mean sd)
        collect layout (var) (result)
        collect label levels result mean "Mean" sd "SD" min "Min." max "Max.", modify    
    putdocx pagebreak    
    putdocx paragraph, style(Heading1)    
    putdocx text ("Table 2: Sample description")    
    putdocx collect    
     table ( ) () () if inclusion == 1, /*
     */ statistic(mean age) statistic(sd age) statistic(min age) statistic(max age) /*
     */ statistic(mean sex) statistic(sd sex) statistic(min sex) statistic(max sex)    
    collect layout (var) (result)    
    collect label levels result mean "Mean" sd "SD" min "Min." max "Max.", modify    
    putdocx collect    
    putdocx save Tables, append
    I have tried using the collect combine command but the combination does not show the right order of the variables. The goal is to create one table to put into Word using putdocx.

    Code:
    *
    putdocx begin
    table () () (), statistic(mean inclusion) statistic(sd inclusion) statistic(min inclusion) statistic(max inclusion) nformat(%6.2f mean sd)    
    collect layout (var) (result)    
    collect label levels result mean "Mean" sd "SD" min "Min." max "Max.", modify    
    collect rename Table table21    
     table ( ) () () if inclusion == 1, /*  
    */ statistic(mean age) statistic(sd age) statistic(min age) statistic(max age) /*  
    */ statistic(mean sex) statistic(sd sex) statistic(min sex) statistic(max sex)    
    collect layout (var) (result)    
    collect label levels result mean "Mean" sd "SD" min "Min." max "Max.", modify    
    collect rename Table table22    
    collect combine Table2 = table21 table22, layout(right) style(right)    
    collect preview
    Crosspost from Stackoverflow: https://stackoverflow.com/questions/...lect-and-table
    Last edited by Felix Kaysers; 03 May 2023, 03:38.
    Cheers,
    Felix
    Stata Version: MP 18.0
    OS: Windows 11

  • #2
    If you do not want to respecify the collect layout with var[inclusion age sex], then you can control the order and selection of the levels of var using collect style autolevels. Here is how using the above example
    Code:
    collect style autolevels var inclusion age sex
    Here is the resulting table
    Code:
    . collect preview
    
    ----------------------------------------
              |   Mean      SD   Min.   Max.
    ----------+-----------------------------
    inclusion |   0.54    0.52      0      1
    age       |  33.71   15.89      5     56
    sex       |   0.29    0.49      0      1
    ----------------------------------------

    Comment


    • #3
      Thank you, Jeff!

      Just writing out the first solution for future readers who might be similarly proficient with the table command.

      Code:
      putdocx begin table () () (),
      statistic(mean inclusion) statistic(sd inclusion) statistic(min inclusion) statistic(max inclusion) nformat(%6.2f mean sd)    
      collect layout (var) (result)    
      collect label levels result mean "Mean" sd "SD" min "Min." max "Max.", modify    
      collect rename Table table21      
      table ( ) () () if inclusion == 1, /*  
      */ statistic(mean age) statistic(sd age) statistic(min age) statistic(max age) /*  
      */ statistic(mean sex) statistic(sd sex) statistic(min sex) statistic(max sex)    
      collect layout (var) (result)    
      collect label levels result mean "Mean" sd "SD" min "Min." max "Max.", modify    
      collect rename Table table22    
      collect combine Table2 = table21 table22, layout(right) style(right)
      collect layout (var[inclusion age sex]) (result)    
      collect preview
      Last edited by Felix Kaysers; 09 May 2023, 08:48.
      Cheers,
      Felix
      Stata Version: MP 18.0
      OS: Windows 11

      Comment

      Working...
      X