by Jeff Meyer
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.
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.
Let’s try copying as an HTML table. Easier to read, but still unprofessional.
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.
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.
Don’t like the font? Go back to the results window, right click on the table, and select “font”.
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)
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 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
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.
Jeff Meyer is a statistical consultant with The Analysis Factor, a stats mentor for Statistically Speaking membership, and a workshop instructor. Read more about Jeff here.