Announcement

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

  • Problem with adding mean of a variable when building table with dtable

    Dear community/Jeff Pitblado (StataCorp)

    I'm having problems with displaying the mean of a variable when building a table with dtable. After many tries I still get nowhere and dtable only add the number of observations to the table. Is there a way to solve this? (Stata 18.0)

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input int doseepti1 byte(epagat2c09 epagacec09 epagcabln0)
    100 .a .a .a
    100 .a .a .a
    100 .a .a .a
    100 .a .a .a
    100 .a .a .a
    100 .a .a .a
    100 .a .a .a
    100 .a .a .a
    100 .a .a .a
    100  1 .a .a
    100 .a .a .a
    100  1 .a .a
    100 .a .a .a
    100 .a .a .a
    100 .a .a .a
    100 .a .a .a
    100 .a .a .a
    100 .a .a .a
    100 .a .a .a
    100 .a .a .a
    100 .a .a .a
    100 .a .a .a
    100 .a .a .a
    100 .a .a .a
    100  1 .a .a
    100 .a .a .a
    100 .a .a .a
    100 .a .a .a
    100 .a .a .a
    100 .a .a .a
    100 .a .a .a
    100 .a .a .a
    100 .a .a .a
    100 .a .a .a
    100 .a .a .a
    100 .a .a .a
    100 .a .a .a
    100 .a .a .a
    100 .a .a .a
    end
    Here is my code:
    Code:
        collect clear
        dtable 1.epagbeta1c 1.epagbeta2c 1.epagbeta3c, ///
        factor(, statistic(fvfrequency fvpercent)) ///
        sample("N", statistic(frequency) place(seplabels)) ///
        sformat("(N=%s)" frequency) ///
        nformat(%6.1f mean sd) name(del1)    
            
        
    dtable doseepti1, name(del2)
          
        collect combine all = del1 del2
    collect label levels collection del1 "Current preventives at baseline"
    collect label levels collection del2 "Eptinezumab dose first infusion"
    
    collect style header epagbeta1c epagbeta2c epagbeta3c, level(hide)
    
        
    collect layout (del2) (result[mean sd])
    collect layout (collection#var) (result)
    The resulting table:
    Eptinezumab dose first infusion
    N (N=39)
    I want the mean (100) and not the N=39.


  • #2
    Please provide a better data extract. At least two issues:
    • some variables you use in the code are not present in the extract, e.g. epagbeta1c, epagbeta2c, epagbeta3c. So the code doesn't run.
    • almost all the values of the epa* variables in your extract are just missing values (.a). I doubt any interesting table can be produced with that.
    Once you have created your data extract, please run your code on it -- that's what we will get, so it's worth understanding what you are providing to us.

    Further, it will be helpful to manually create the table you want to generate, so we have a better idea of exactly what you want to achieve.
    Last edited by Hemanshu Kumar; 21 Aug 2025, 06:30.

    Comment


    • #3
      Dear Hemanshu, yes the example are a copy/paste error, here is the correct example. The missing values are not the point, it runs just fine. Here are the correct data and the corresponding table:

      Code:
      * Example generated by -dataex-. For more info, type help dataex
      clear
      input int doseepti1 byte(epagbeta1c epagbeta2c epagbeta3c)
      100 .a .a .a
      100 .a .a .a
      100 .a .a .a
      100 .a .a .a
      100 .a .a .a
      100 .a .a .a
      100 .a .a .a
      100 .a .a .a
      100 .a .a .a
      100 .a .a .a
      100 .a .a .a
      100 .a .a .a
      100 .a .a .a
      100 .a .a .a
      100 .a .a .a
      100 .a .a .a
      100 .a .a .a
      100 .a .a .a
      100 .a .a .a
      100 .a .a .a
      100 .a .a .a
      100 .a .a .a
      100 .a .a .a
      100 .a .a .a
      100 .a .a .a
      100 .a .a .a
      100 .a .a .a
      100 .a .a .a
      100 .a .a .a
      100  1 .a .a
      100 .a .a .a
      100 .a .a .a
      100 .a .a .a
      100 .a .a .a
      100 .a .a .a
      100 .a .a .a
      100 .a .a .a
      100 .a .a .a
      100 .a .a .a
      end
      Code:
      . collect layout (del2) (result[mean sd])
      (dimension del2 not found)
      
      Collection: all
            Rows: del2
         Columns: result[mean sd]
         Table 1: 1 x 2
      
      ------------------------
       Mean Standard deviation
      ------------------------
      100.0              (0.0)
      ------------------------
      
      . collect layout (collection#var) (result)
      
      Collection: all
            Rows: collection#var
         Columns: result
         Table 1: 6 x 1
      
      ----------------------------------------------------------
                                                        Summary 
      ----------------------------------------------------------
      Current preventives at baseline                           
        07AB02                            metoprolol  1 (100.0%)
        07AB03                            atenolol        0 (.%)
        07AA05                            propranolol     0 (.%)
      Eptinezumab dose first infusion                           
        N                                                 (N=39)
      ----------------------------------------------------------
      
      .         
      . 
      end of do-file

      Comment

      Working...
      X