Announcement

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

  • Using table and collect command for 6 outcomes - help with loop please!

    Hello!

    Jeff Pitalbo from Stata very kindly helped me with getting row %s using the code below. I need to replicate this for 6 binary outcomes and am getting stuck. I would be grateful for assistance with this. Many thanks!!


    HTML Code:
     clear all  
    webuse nhanes2l  
    local cvars age weight bpsystol
    local fvars sex race  
    
    * compute the summary and continuous variable statistics table () (diabetes), ///     statistic(frequency) ///    
    statistic(percent) ///    
    statistic(mean `cvars') ///    
    statistic(sd `cvars')  
    
    * show "N" for the sample frequency and percents collect recode var _hide = N, fortags(result[frequency percent])  
    
    * compute tabulations of each factor variable across levels of diabetes
    foreach fvar of local fvars {    
    table (`fvar') (diabetes), ///            
    statistic(frequency) ///            
    statistic(percent, across(diabetes)) ///        
    totals(`fvar') ///        
    name(`fvar')
    }  
    
    * combine all above collections into a new one called 'all'
    collect combine all = Table `fvars'
    
     * define the composite result similar to that of -dtable-
    collect composite define ///    
    stats = frequency percent ///        
    mean sd ///  
      , trim collect style autolevels result stats, clear collect style header result[stats], level(hide)  
    
    * some other -dtable- style choices
    collect style cell result[percent], sformat("(%s%%)") nformat(%9.1f)
    collect style cell result[sd], sformat("(%s)")
    collect style cell result[mean sd], nformat(%9.3f)  
    
    * arrange the composite results like -dtable-
    collect layout (var `fvars') (diabetes#result)
    
     * publish your table to a supported file format
    collect export table1b.html, replace
    Last edited by Sara Khan; 09 Jun 2025, 05:33.

  • #2
    Duplicate post. If interested please follow https://www.statalist.org/forums/for...ample-uncluded

    (Please don't ask the same question in two (or more) threads. Matters can get very confused, with repetition of material in replies.)

    Comment


    • #3
      Apologies Nick! I wasn't sure if older posts get picked up. Happy for this one to be deleted.

      Comment

      Working...
      X