Announcement

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

  • How to generate a table with summariy statistics by id

    Dear All,

    I have the following sample of data

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input double efi float id double fyear
    8.48  1 2000
    8.32  1 2002
    8.34  1 2003
    8.37  1 2004
    8.24  1 2005
    8.25  1 2006
    8.29  1 2007
    8.11  1 2008
    7.77  1 2009
    7.89  1 2010
     7.8  1 2011
    7.87  1 2012
    7.78  1 2013
    7.88  1 2014
    7.98  1 2015
    8.03  1 2016
    8.05  1 2017
    8.01  1 2018
    7.99  1 2019
    8.53  2 2000
    8.38  2 2003
     8.4  2 2004
    8.33  2 2005
    8.31  2 2006
    8.35  2 2007
    8.12  2 2015
    8.15  2 2016
     8.2  2 2017
    8.15  2 2018
    8.12  2 2019
    8.37  3 2002
    8.38  3 2003
     8.4  3 2004
    8.33  3 2005
    8.31  3 2006
    8.35  3 2007
    8.21  3 2008
       8  3 2010
    7.97  3 2011
    8.05  3 2012
    7.94  3 2013
    8.04  3 2014
    8.12  3 2015
     8.2  3 2017
    8.15  3 2018
    8.12  3 2019
    8.57  5 2000
     8.4  5 2001
     8.4  5 2002
    8.42  5 2003
    8.45  5 2004
    8.35  5 2005
    8.35  5 2006
    8.35  5 2007
    8.17  5 2008
    7.86  5 2009
    7.95  5 2010
    7.89  5 2011
    7.98  5 2012
    7.91  5 2013
    8.01  5 2014
    8.11  5 2015
    8.15  5 2016
    8.18  5 2017
    8.13  5 2018
    8.08  5 2019
    8.24  7 2005
    8.25  7 2006
    8.29  7 2007
    8.11  7 2008
    7.87  7 2012
    7.78  7 2013
    7.88  7 2014
    7.98  7 2015
    8.03  7 2016
    8.05  7 2017
    8.01  7 2018
    7.99  7 2019
    8.51 10 2000
    8.37 10 2001
    8.36 10 2002
    8.36 10 2003
    8.37 10 2004
    8.26 10 2005
    8.28 10 2006
    8.33 10 2007
    8.15 10 2008
    7.88 10 2009
    7.95 10 2010
    7.86 10 2011
    7.92 10 2012
    7.77 10 2013
    7.88 10 2014
    7.97 10 2015
    8.01 10 2016
    8.09 10 2017
    8.05 10 2018
    8.02 10 2019
    end
    I would like to generate to summarize the variable efi for each id. Hence, I type:

    Code:
    bysort id: sum efi
    What I would like next is to export each summary statistics in a single table Something like:
    id obs. mean s.d. min max
    1 19 8.07 0.22 7.77 8.48
    2 11 8.28 0.14 8.12 8.53

    However I cannot figure out how I can get it. Anu hint?

    Thanks in advance

    Dario

  • #2
    Code:
    help tabstat,

    Comment


    • #3
      Nick Cox thanks a lot. Much easier than I thought.

      Comment


      • #4
        If you are using Stata 17 or Stata 18, you can use the new version of the table command to build your table then export it to MS Word, Excel, Latex, HTML, or PDF.
        Here is an example using the data provided in the original post
        Code:
        * build summary table
        table id, ///
                statistic(frequency) ///
                statistic(mean efi) ///
                statistic(sd efi) ///
                statistic(min efi) ///
                statistic(max efi) ///
                nformat(%9.2f mean sd min max) ///
                nototal
        
        * use shorter labels
        collect label levels result ///
                frequency "Obs." ///
                sd "S.D." ///
                min "Min" ///
                max "Max" ///
                , modify
        collect preview
        
        * export to MS Word
        collect export mytab.docx
        Here is the resulting table
        Code:
        . collect preview
        
        ----------------------------------------
             |  Obs.   Mean   S.D.    Min    Max
        -----+----------------------------------
        id   |                                  
          1  |    19   8.08   0.22   7.77   8.48
          2  |    11   8.28   0.14   8.12   8.53
          3  |    16   8.18   0.16   7.94   8.40
          5  |    20   8.19   0.21   7.86   8.57
          7  |    12   8.04   0.16   7.78   8.29
          10 |    20   8.12   0.22   7.77   8.51
        ----------------------------------------

        Comment


        • #5
          Jeff Pitblado (StataCorp) thanks a lot. Very useful. I currently use Stata 17, but i will switch to Stata 18 in a couple of days.

          Comment

          Working...
          X