Argggh! How Do I Output Tables and Graphs From Stata?

For my first assignment using Stata, I spent four or five hours trying to present my output in a “professional” form. The most creative method I heard about in class the next day was to copy the contents into Excel, create page breaks, and then copy into Word.

SPSS makes it so easy to copy tables and graphs into another document. Why can’t Stata be easy?

Anyone who has used Stata has gone through this and many of you still are. No worries, help is on the way!

We will look at outputting tables, graphs, and log files in this article.

Tables

Let’s first tackle the issue of copying tables into Word. Below is a frequency table of a categorical variable that I highlighted in the results window, copied, and then pasted.  There is no formatting–what a mess.

Employment
Category Freq.   Percent  Cum.
 
Clerical   363 76.58 76.58
Custodial 27 5.70 82.28
Manager 84 17.72 100.00
Total 474 100.00

Let’s try copying as an HTML table.  Easier to read, but still unprofessional.

Employment
Category Freq. Percent Cum.
Clerical 363 76.58 76.58
Custodial 27 5.70 82.28
Manager 84 17.72 100.00
Total 474 100.00

 

Copy as a picture looks much better but the border is very wide.  As a result you can’t center the graph on the page using the “center” icon in Word.

image001

What’s the solution?

In Stata, hover your mouse over the right edge of the “results” window.  When the big arrow icon of your mouse changes to “small arrows with 2 parallel lines in between” left click on your mouse and drag the window to the left.

This makes the result window smaller.  Drag the right edge of the results window over to the left side of your table. Now highlight the table, right click and “Copy as Picture”. Now you can move it to the right, left or center of the page.

image002

Don’t like the font?  Go back to the results window, right click on the table, and select “font”.

Graphs

Ever run a do-file that produces a number of graphs?  As soon as you produce a new graph the old graph disappears. How can you bring it back?

Whenever Stata produces a graph it gives it the name “Graph” (what did you expect?). So anytime you create a new graph it removes the prior graph from memory.

You have two options to get around this problem.  Name the graph and Stata will store it in memory for the rest of your session.  Or save it in your working directory and re-use it whenever you want.  Examples:

 

histogram salary, saving(salary,replace)

or

histogram salary, name(salary,replace)

To view the salary graph again the command is:

graph display salary

It’s very easy to save it as a pdf:

graph use salary
graph export salary.pdf

 

What if you want to see two or more graphs on one page?  Use the combine option.  For example if you have two graphs male_salary and female_salary.

graph combine male_salary.gph female_salary.gph
(puts the graphs side by side)

graph combine male_salary.gph female_salary.gph, col(1)
(puts one graph on top of the other)

You can save and export the combined graphs as well:

graph combine male_salary.gph female_salary.gph, col(1) saving(salary_by_sex,replace)
graph use salary_by_sex
graph export salary_by_sex.pdf

Log files

Were you ever asked to show how you got your results? Did you try copying and pasting everything in your results window?  (If I knew then what I know now I could have used the time to read War and Peace).

Before you start an analysis, start recording a log file. When you start recording a log file you have to name it.  Make sure when you name it you include the extension “.log”, or you won’t be able to read it unless you have the Stata viewer on the computer.

When you are finished, stop recording your log file.

Advanced tip: How do you get a lovely copy of a log file that includes the line numbers of your do-file?

Save your file with the extension “.smcl”.  This is the extension that needs the Stata viewer in order to read it.

If you have to give a printed copy of this to someone without the viewer, use the translate command to  convert your file into a pdf.

Below is the code I have for converting a log file of mine.

translate “Stata Log for module 2 exercises.smcl” “Stata Log for module 2 exercises.pdf”

This log file pdf has all of the results that came across my Stata results window. It saved everything but the graphs. But of course I saved them as a combined pdf.

Best of all, I can convert the pdf to a Word document and extract anything I want from it.

Every topic mentioned in this blog post will be explained in much more detail in the upcoming course “Introduction to Data Analysis with Stata” offered by The Analysis Institute.  If you’d like to start learning or improving your Stata skills, please join us.

 

Getting Started with Stata
Jeff introduces you to the consistent structure that Stata uses to run every type of statistical analysis.

Reader Interactions

Comments

  1. Meenu says

    Hi Jeff,

    I used this code to convert .smcl to pdf but gives me error. I had created a demo log.smcl first and then stopped log. Then gave following command.

    translate “demo log.smcl” “demo log.pdf”
    varlist not allowed
    r(101);

    I tried it with the .log extension as well but gives same error.
    Can you help?

    Thanks so much
    Meenu

    • Jeff Meyer says

      Here’s an example of what I do. It may be different from when I wrote that blog, which was a couple of years ago.

      At the top of my page I have:
      log using “C:\Analysis Factor Workshop\Stata Log for module 4 examples-BLR.log”,text replace

      at the bottom of my do-file:
      log close
      translate “Stata Log for module 4 examples-BLR.log” “Stata Log for module 4 examples-BLR.pdf”
      translate “Stata Log for module 4 examples-BLR.log” “Stata Log for module 4 examples-BLR.ps”, replace

  2. cherry singhal says

    Thank you so much Jeff!

    I have over 100 model outputs that needed to be saved from Stata. And I am so thankful to God I found your blog post. Your log-file method is a gem of an idea! Even the Stata technical service did not tell me that. Would you be writing an update to this post, if there have been developments ..?

  3. Jeff Meyer says

    There are two ways (and possibly more) for exporting tables which you can then reformat.

    1) With each project/analysis, create a separate do-file for your tables. Start the do-file with the command to create a log file. Add the command at the end to close your log file. Next use the translate command (shown in the blog above) to convert your file to a pdf. Open your pdf and save it as a word document. In the word document all of your table outputs will be in table format. You can click on the four-way arrow sign at the top left corner of a table to highlight the table, copy and then paste into your document. In your new document you can change your columns and rows as you like. You can also copy the tables into Excel where it might be easier to format the numbers and borders. From Excel you can copy into your document the formatted tables.

    The second method is to use the “tabout” command. If you Google “tabout” you will find a number of examples on how to use it. If you are doing regression analysis you can output your results to Excel using the “outreg2” command.

    Thanks for your questions,
    Jeff Meyer

  4. Christine says

    This is one of the main reasons I don’t use stata, thanks for these tips. I co-author many reports that require specific formatting styles and the tables cannot appear as images because other people may need to edit them and write text around them. I still haven’t found a way to neatly get tabular results into excel or word. Got any advice? I’m talking 20-30 tables per report.


Leave a Reply

Your email address will not be published. Required fields are marked *

Please note that, due to the large number of comments submitted, any questions on problems related to a personal study/project will not be answered. We suggest joining Statistically Speaking, where you have access to a private forum and more resources 24/7.