Announcement

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

  • Exporting tabstat output to Excel file?

    Hi stata experts,

    I'm trying to export a tabstat table into excel sheet and have tried multiple commands including tabout, putexcel, and export but none have been successful thus far. Additionally if I simply copy/paste the output to excel it populates the table with the correct number of rows but only one column, which would require me to manually transpose the data. The tabstat command I'm using is:

    tabstat age ///
    bmi blunt iss edarrivalgcs ///
    edadmittemp ///
    hr0_basedefexc ///
    hr0_inr hr0_ptt hr0_plts hr0_fibrinogen_rlab ///
    icu_0to24h_blood_units icu_0to24h_ffp_units icu_0to24h_plt_units ///
    icu_0to6h_blood_units icu_7to12h_blood_units icu_13to24h_blood_units ///
    icu_25to48h_blood_units icu_49to72h_blood_units icu_0to6h_ffp_units ///
    icu_7to12h_ffp_units icu_13to24h_ffp_units icu_25to48h_ffp_units ///
    icu_49to72h_ffp_units icu_0to6h_plts_units icu_7to12h_plts_units ///
    icu_13to24h_plts_units icu_25to48h_plts_units icu_49to72h_plts_units ///
    totalhospitaldays icu_days_to28days ventfreedaysat28days ///
    vap ali mof mortalityat28days ///
    sample0h_ck_alpha ///
    sample0h_adp sample0h_col sample0h_trap sample0h_aspi sample0h_risto ///
    iss ///
    transfusedin24h ///
    transfusedpltsin24h ///
    delta6h_plts hr6_plts hr0_plts ///
    sample6h_ck_alpha sample0h_ck_alpha sample6h_adp sample0h_adp sample6h_col ///
    sample0h_col sample6h_trap sample0h_trap sample6h_aspi sample0h_aspi ///
    sample6h_risto sample0h_risto sample0h_adp_endavg sample0h_adp_baseavg ///
    sample0h_col_endavg sample0h_col_baseavg sample0h_trap_endavg ///
    sample0h_trap_baseavg sample0h_aspi_endavg sample0h_aspi_baseavg ///
    sample0h_risto_endavg sample0h_risto_baseavg ///
    hr0_basedefexc ///
    , s(p25 p75 n) ///
    col(stat)


    And the various "exporting" commands I have tried are:

    1. export excel using "multiplate_clean.xlsx"
    2. putexcel set "myresults.xlsx" // remember to specify the full path
    putexcel A1 = matrix(T), names
    3. tabout age ///.... hr0_basedefexc using multiplate_dataclean.txt, ///
    c(n mean sd p50 p25 p75)

    Can anyone provide some advice on ANY means to export this tabstat table to excel?

    Thank you very much.

  • #2
    If you are comfortable with copying the output table from Word to Excel, then asdoc can do it very easily. asdoc can be downloaded from SSC

    Code:
    ssc install asdoc
    
    sysuse auto, clear
    
    asdoc tabstat price mpg rep78, stat(p25 p75 N) replace
    Click image for larger version

Name:	Capture.PNG
Views:	1
Size:	10.6 KB
ID:	1519499



    asdoc can be used with almost all Stata commands. Here is a short blog post that shows how asdoc can be used with any Stata command http://fintechprofessor.com/2018/02/...basic-example/. You can also watch several YouTube videos that show the use of asdoc https://www.youtube.com/watch?v=zdI6...LwodAk2oqLYhr-

    Code:
    * For installation of the stable version
    ssc install asdoc
    
    * For installation of the new beta version
    net install asdoc, from(http://fintechprofessor.com) replace
    help asdoc
    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


    • #3
      Note that you can also collapse to a new dataset with such statistics and then just export excel the dataset.

      Comment


      • #4
        Hi Nichole, and welcome to Statalist!

        So, one of two ways will probably (hopefully!) work:

        1) Once you've run the tabstat, when you right click on the table, select "Copy Table" (CTRL + SHIFT + C), not "Copy". This will tell Excel to put things in different columns.

        2) I use logout (SSC) to automate exporting descriptive statistics to Excel.

        Code:
        ssc install logout
        logout, save("Filename & path of output") excel replace: tabstat age ///.... hr0_basedefexc, stats(n mean median p25 p75 min max) col(stats)
        Also, if you are going to have to type in that many right-hand-side variables repeatedly, you will probably want to look up "local macros" for Stata.
        That way, you could do something like this:

        Code:
        local  rhsvars "bmi blunt iss edarrivalgcs edadmittemp hr0_basedefexc hr0_inr hr0_ptt hr0_plts hr0_fibrinogen_rlab"
        /* To add more variables to `rhsvars'  */
        local  `rhsvars' "`rhsvars' icu_0to24h_blood_units icu_0to24h_ffp_units icu_0to24h_plt_units"
        display "`rhsvars'"
        
        tabstat `rhsvars'  /* To Stata, you've typed "tabstat bmi blunt iss edarrivalgcs...."  */
        
        *** You can also use it for filenames & directories
        local tab_table "C:\data\This project\Tables\Table 1 - Tabstat Table"
        
        *** Then the logout command in the 1st code block becomes:
        logout, save(`tab_table') excel replace: tabstat `rhsvars' , stats(n mean median p25 p75 min max) col(stats)
        Just note that the ` at the beginning of local macros (`rhsvars') is the left single quote ("backtick"). It is found in the upper left corner of the keyboard, under the tilde (~). The quote after the `rhsvars' is the right single quote. It is found under the double quotation mark (") on the right side of the keyboard.

        Hope this helps!

        Comment


        • #5
          Originally posted by David Benson View Post
          Hi Nichole, and welcome to Statalist!

          So, one of two ways will probably (hopefully!) work:

          1) Once you've run the tabstat, when you right click on the table, select "Copy Table" (CTRL + SHIFT + C), not "Copy". This will tell Excel to put things in different columns.

          2) I use logout (SSC) to automate exporting descriptive statistics to Excel.

          Code:
          ssc install logout
          logout, save("Filename & path of output") excel replace: tabstat age ///.... hr0_basedefexc, stats(n mean median p25 p75 min max) col(stats)
          Also, if you are going to have to type in that many right-hand-side variables repeatedly, you will probably want to look up "local macros" for Stata.
          That way, you could do something like this:

          Code:
          local rhsvars "bmi blunt iss edarrivalgcs edadmittemp hr0_basedefexc hr0_inr hr0_ptt hr0_plts hr0_fibrinogen_rlab"
          /* To add more variables to `rhsvars' */
          local `rhsvars' "`rhsvars' icu_0to24h_blood_units icu_0to24h_ffp_units icu_0to24h_plt_units"
          display "`rhsvars'"
          
          tabstat `rhsvars'  /* To Stata, you've typed "tabstat bmi blunt iss edarrivalgcs...." */
          
          *** You can also use it for filenames & directories
          local tab_table "C:\data\This project\Tables\Table 1 - Tabstat Table"
          
          *** Then the logout command in the 1st code block becomes:
          logout, save(`tab_table') excel replace: tabstat `rhsvars' , stats(n mean median p25 p75 min max) col(stats)
          Just note that the ` at the beginning of local macros (`rhsvars') is the left single quote ("backtick"). It is found in the upper left corner of the keyboard, under the tilde (~). The quote after the `rhsvars' is the right single quote. It is found under the double quotation mark (") on the right side of the keyboard.

          Hope this helps!
          can I loop over logout command?

          Code:
          input str10 fruit
          apple
          orange
          pear
          end
          levelsof fruit, local(fruit)
          
          foreach a of local fruit {
          logout, save(`a') excel replace: tab buyer if `a'==1
          }

          Comment

          Working...
          X