Announcement

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

  • Summary statistic using tabstat, by : Statistics next to each other in columns

    Hi,

    I would like to summarize my data in a way so that the means of my variables of interest are calculated by industry.

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input float A byte(B Industry) double C float D
    .13366325 0  6   24759 .14703006
    .12301464 2 10  273023 4.5886574
    .09777707 0  6  231424  8.948502
     .1706112 3  6 1853901  2.511649
    .10899235 0 11  262964 4.2845984
    .10899235 0 11  262964 4.2845984
    .10216609 0 11       0         0
    .07947874 0 11       0         0
    .10216609 0 11       0         0
    .10216609 0 11       0         0
    .14309178 1 11   61908 2.0505333
    .09777707 0  6  231424  8.948502
     .1706112 3  6 1853901  2.511649
    .10216609 0 11       0         0
     .1706112 3  6 1853901  2.511649
    .10216609 0 11       0         0
    .09777707 0  6  231424  8.948502
     .1706112 4  6 1853901  2.511649
    .10899235 0 11  262964 4.2845984
     .1706112 3  6 1853901  2.511649
    end
    label values Industry Acq_SIC_FF12
    label def Acq_SIC_FF12 6 "Business Equipment -- Computers, Software, and Electronic Equipment", modify
    label def Acq_SIC_FF12 10 "Healthcare, Medical Equipment, and Drugs", modify
    label def Acq_SIC_FF12 11 "Finance", modify
    order Industry
    The following code brings me close to what I want
    Code:
    tabstat A B C D, by(Industry) stat(mean n) nototal
    However, instead of having the number of observations per industry in rows I would like to have a column next to variable A so that the table takes on the following form:
    Click image for larger version

Name:	Industry.jpg
Views:	1
Size:	20.1 KB
ID:	1667266



    Any help is greatly appreciated

  • #2
    Mark:
    you may find some interesting tricks about unconventional layouts in -table- related entries in Stata 17 .pdf manual.
    Kind regards,
    Carlo
    (Stata 19.0)

    Comment


    • #3
      Dear Carlo,
      thank you.
      As the number of observations within a specific industry is the same for each of my four variables, the following solution works for me:
      Code:
      table Industry, statistic(count A) statistic(mean A B C D)
      
      
      
      . table Industry, statistic(count A) statistic(mean A B C D)
      
      ------------------------------------------------------------------------------------------------------------------------------------------------
                                                                            |  Number of nonmissing values                      Mean                  
                                                                            |                            A          A          B          C          D
      ----------------------------------------------------------------------+-------------------------------------------------------------------------
      Industry                                                              |                                                                         
        Business Equipment -- Computers, Software, and Electronic Equipment |                            9   .1422278   1.777778    1109837   4.394531
        Healthcare, Medical Equipment, and Drugs                            |                            1   .1230146          2     273023   4.588657
        Finance                                                             |                           10   .1060378         .1      85080   1.490433
        Total                                                               |                           20   .1231722        .95   555617.9   2.952188
      ------------------------------------------------------------------------------------------------------------------------------------------------

      However, I experience some problems when exporting the file to word using asdoc.

      Code:
      asdoc table Industry, statistic(count A) statistic(mean A B C D)
      option statistic() not allowed
      r(198);
      Any ideas how to solve this problem in order to export the table to word?
      Last edited by Marc Pelow; 01 Jun 2022, 10:44.

      Comment


      • #4
        asdoc is from SSC and the last question is almost surely (small mathematical joke there) something Attaullah Shah will want to address.

        FWIW, tabdisp is among the most neglected official commands there is, in my view. Here is a dirty but quick first start:

        Code:
        foreach v in A B C D { 
            egen mean_`v' = mean(`v'), by(Industry)
        }
        
        bysort Industry : gen count = _N 
        
        tabdisp Industry, c(count mean*)
        
        
        -----------------------------------------------------------------------------------------------------
                                        Industry |      count      mean_A      mean_B      mean_C      mean_D
        -----------------------------------------+-----------------------------------------------------------
        Business Equipment -- Computers, Softwar |          9    .1422278    1.777778     1109837    4.394531
        Healthcare, Medical Equipment, and Drugs |          1    .1230146           2      273023    4.588657
                                         Finance |         10    .1060378          .1       85080    1.490433
        -----------------------------------------------------------------------------------------------------
        There is plenty of scope to assign display formats in advice and to vary the stub width.

        Comment


        • #5
          Nick Cox Really helpful. I didn't even know that this command exists. Thank you.

          Comment


          • #6
            I keep on pushing tabdisp -- without much discernible effect.

            https://www.stata.com/meeting/german...many19_Cox.pdf



            Code:
            . search tabdisp, sj
            
            Search of official help files, FAQs, Examples, and Stata Journals
            
            SJ-12-1 pr0053  . . . . . . . . . . . . . . .  Speaking Stata: Output to order
                    . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  N. J. Cox
                    Q1/12   SJ 12(1):147--158                                (no commands)
                    shows basic tips and tricks for presenting Stata output
                    in a different way
            
            SJ-3-4  pr0011  . . . . . . . .  Speaking Stata: Problems with tables, Part II
                    . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  N. J. Cox
                    Q4/03   SJ 3(4):420--439                                 (no commands)
                    reviews three user-written commands (tabcount, makematrix,
                    and groups) as different approaches to tabulation problems
            
            SJ-3-3  pr0010  . . . . . . . .  Speaking Stata:  Problems with tables, Part I
                    . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  N. J. Cox
                    Q3/03   SJ 3(3):309--324                                 (no commands)
                    presents ways of preparing material for tabulation

            Comment


            • #7
              I added tabdisp to asdocx as part of my plan to add all important -yet missing - features in asdoc. Addition of tabdisp adds more flexibility to asdocx in creating summary statistics over groups and displaying them. I shall add a comprehensive web page that enumerates details and examples of the tabdisp command in asdocx. For time being, let's see how asdocx exports the table generated by Nick Cox code in #4.

              Code:
              asdocx tabdisp Industry, c(count mean*) replace
              Click image for larger version

Name:	Capture.PNG
Views:	1
Size:	22.3 KB
ID:	1667974


              or send to Excel

              Code:
              asdocx tabdisp Industry, c(count mean*) save(Myfile.xlsx) replace
              Click image for larger version

Name:	Capture.PNG
Views:	1
Size:	34.4 KB
ID:	1667975


              More about asdocx can be found here https://fintechprofessor.com/asdocx/
              Regards
              --------------------------------------------------
              Attaullah Shah, PhD.
              Professor of Finance, Institute of Management Sciences Peshawar, Pakistan
              FinTechProfessor.com
              https://asdocx.com
              Check out my asdoc program, which sends outputs to MS Word.
              For more flexibility, consider using asdocx which can send Stata outputs to MS Word, Excel, LaTeX, or HTML.

              Comment

              Working...
              X