Announcement

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

  • Exporting frequency tables to Excel with tab2xl/putexcel

    Dear Statalisters,

    I am trying to export several different frequency tables from Stata to Excel (in one file).
    When using putexcel I always need to manually change the file name.

    I tried "tab2xl", but somehow I always end up only with the frequency table of the last variable and not all of them

    Could somebody help?

    My Stata Code:

    local row=1

    foreach X of varlist gen edu inc emp age inv {

    tab2xl `X' using frequency_tables, row(1) col(1)

    }


    Thank you very much in advance,
    Christina

  • #2
    Christina Freder if you can accept exporting your tabulation results to MS Word, then asdoc (from SSC) can write nicely formatted tables to a single file. See the following example:

    Code:
    * To install asdoc
    ssc install asdoc
    
    *load auto data
    sysuse auto
    
    * Write tabulation results for variables rep78, mpg and turn 
    asdoc tabulate rep78, replace 
    asdoc tab mpg , append
    asdoc tab mpg , append
    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
      I tried "tab2xl", but somehow I always end up only with the frequency table of the last variable and not all of them
      That is because each tab2xl command is writing the table into row 1 column 1 of the same worksheet, obliterating the previous results.
      Code:
      foreach X of varlist gen edu inc emp age inv {
          tab2xl `X' using frequency_tables, row(1) col(1) sheet(`X')
      }
      will create a separate worksheet for each variable.

      Comment


      • #4
        Perfect, it worked. Thank you both so much!

        Comment


        • #5
          The table2xl command doesn't seem to allow running multiple crosstab separately but export in the same Excel file under different worksheets. Can you please confirm if that's the case?

          Comment

          Working...
          X