Announcement

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

  • Equivalent command for dtable (Stat 18) in Stata 17 (example uncluded)

    Hello

    I have Stata 17 and am trying to recreate the table in the link. It has been created using the dtable command in Stata 18.

    I require descriptive statistics for my independent variables (mix of continuous and categorical) by a categorical outcome variable.

    I'd be very grateful for help with this. Thank you!


    https://www.stata.com/new-in-stata/c...tistic-tables/

    Click image for larger version

Name:	Capture.PNG
Views:	1
Size:	42.8 KB
ID:	1750121



  • #2
    Here is how you can use the table and collect commands in Stata 17 to recreate the table you link.
    Code:
    clear all
    
    webuse nhanes2l
    
    local cvars age weight bpsystol
    local fvars sex race
    
    * compute the summary statistics
    table () (diabetes), ///
        statistic(frequency) ///
        statistic(percent) ///
        statistic(mean `cvars') ///
        statistic(sd `cvars') ///
        statistic(fvfrequency `fvars') ///
        statistic(fvpercent `fvars') ///
    
    * look at the default layout specification
    collect layout
    
    * define the composite result similar to that of -dtable-
    collect composite define ///
        stats = frequency percent ///
            mean sd ///
            fvfrequency fvpercent ///
        , trim
    collect style autolevels result stats, clear
    collect style header result[stats], level(hide)
    
    * some other -dtable- style choices
    collect style cell result[percent fvpercent], sformat("(%s%%)") nformat(%9.1f)
    collect style cell result[sd], sformat("(%s)")
    collect style cell result[mean sd], nformat(%9.3f)
    
    * -table- uses -var[_hide]- to tag the frequency and percent,
    * but -dtable- shows "N"
    collect recode var _hide = N, fortags(result[frequency percent])
    
    * -dtable- uses the stacked row style with -nobinder-
    collect style row stack, nobinder
    
    * arrange the composite results like -dtable-
    collect layout (var) (diabetes#result)
    
    * publish your table to a supported file format
    collect export table1.html, replace

    Comment


    • #3
      Thank you so much!! This worked perfectly. :0)

      Would you be able to help modify the code to display row %s instead of column %s?

      Comment


      • #4
        Here is the above example, modified to compute row percent values for the factor variables.
        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
        Here is the resulting table.
        Code:
        ---------------------------------------------------------------------------------
                                |                      Diabetes status
                                |      Not diabetic           Diabetic              Total
        ------------------------+--------------------------------------------------------
        N                       |     9,850 (95.2%)         499 (4.8%)    10,349 (100.0%)
        Age (years)             |   46.918 (17.193)    60.687 (11.475)    47.582 (17.216)
        Weight (kg)             |   71.658 (15.220)    76.670 (17.175)    71.900 (15.357)
        Systolic blood pressure |  130.088 (22.759)   146.651 (28.387)   130.887 (23.332)
        Sex                     |
          Male                  |     4,698 (95.6%)         217 (4.4%)     4,915 (100.0%)
          Female                |     5,152 (94.8%)         282 (5.2%)     5,434 (100.0%)
        Race                    |
          White                 |     8,659 (95.5%)         404 (4.5%)     9,063 (100.0%)
          Black                 |     1,000 (92.1%)          86 (7.9%)     1,086 (100.0%)
          Other                 |       191 (95.5%)           9 (4.5%)       200 (100.0%)
        ---------------------------------------------------------------------------------

        Comment


        • #5
          Hi Jeff,

          Thank you so much! I really appreciate you help.. Worked perfectly.

          Comment


          • #6
            Hi Jeff! You very kindly helped me with extracting row %s to create my tables. I am struggling to replicate this for 6 binary outcomes in a foreach loop. I'd be most grateful for your assistance!

            Comment


            • #7
              I would be grateful for any help with my updated query.

              Comment


              • #8
                Originally posted by Sara Khan View Post
                I am struggling to replicate this for 6 binary outcomes in a foreach loop.
                What do you mean by adding 6 binary outcomes? Don't you simply add these variables to the local "fvars" in #4?



                Originally posted by Jeff Pitblado (StataCorp) View Post
                Here is the above example, modified to compute row percent values for the factor variables.
                [code]
                clear all

                webuse nhanes2l

                local cvars age weight bpsystol
                local fvars sex race

                Comment


                • #9
                  Thank you for your reply. I want to replicate the above code to get descriptive statistics across 6 binary variables. In the example above (with code snippet below), statistics are stratified by diabetes. I wish to repeat the code 6 time using a different stratifying variable.

                  Code:
                  * compute tabulations of each factor variable across levels of diabetes  foreach fvar of local fvars {  table (`fvar') (diabetes), ///

                  Comment


                  • #10
                    You can try something like this:


                    Code:
                    clear all
                    
                    webuse nhanes2l
                    
                    local cvars age weight bpsystol
                    local fvars sex race
                    local acrossvars diabetes rural hlthstat
                    
                    foreach var of local acrossvars{
                    
                        * compute the summary and continuous variable statistics
                        table () (`var'), ///
                            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') (`var'), ///
                                statistic(frequency) ///
                                statistic(percent, across(`var')) ///
                                totals(`fvar') ///
                                name(`fvar') replace
                            }
                    
                            * combine all above collections into a new one called 'all'
                            collect combine `var' = 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') (`var'#result)
                    
                    }
                    
                    collect combine all = `acrossvars'
                    collect layout (var `fvars') ((`acrossvars')#result)
                    
                    * publish your table to a supported file format
                     collect export table1.html, replace
                    Click image for larger version

Name:	Capture.PNG
Views:	1
Size:	51.2 KB
ID:	1778787

                    Comment

                    Working...
                    X