- Running Stata on a Unix/Linux server and getting automatic email updates
- Outputting tables from Stata
- Automatically generating pdfs of Stata tables and graphs
- Better do-file editors for Stata
Running Stata on a Unix/Linux server and getting automatic email updates
For large datasets or long calculations, Stata can run substantially faster on a server. It also frees up your computer and minimizes the impact of power failures since servers tend to have backup power. To run a Stata do-file on a Unix/Linux server you can type
stata -b 'yourdofile.do' &
The -b tells Stata to run the do-file in batch mode. The ampersand & tells the server to keep running the job even if you are logged out – you can turn off your desktop while the server keeps working. Use statamp if your server offers the multi-processor version of Stata.
Email when Stata job is completed
A useful twist is to ask the server for an email once the job is done, so you don't have to check all the time. Here one approach that puts "done subject" in the email's subject line and "done body" in the message body. Note the double ampersand && and the pipe |.
stata -b 'yourdofile.do' && echo "done body" | mail -s "done subject" [youremail @ yourhost .com] &
This can be combined with other technology. For instance, many cell phone carries allow you to send a SMS message by email. For T-mobile in the US this works by emailing [yourcellnumber @ tmomail .net]. You can use that email address to receive a text message from the server when your Stata job is done. To stop a Stata job that is already running first get a list of your current jobs under your user name and then terminate the job using its process ID number.
- If you are still logged in: type jobs to see the list and kill % followed by the job number.
- If you were disconnected: type ps ux to see the list and kill -9 followed by the job/PID number.
You can run multiple do-files either by re-running the Unix command for each file, or run them sequentially by using Unix to call a single do-file which in turn calls additional files (i.e. calling do-files from within a do-file). Unix can also attach a file when sending the email, for instance the log-file from your Stata output. Unix has various mail programs (mail, mailx etc) that are quite flexible, just search around for the correct syntax.
Email updates while Stata job is still running
You can also invoke the Unix mail program from within your Stata do file. Use the following line in your Stata code (no ampersand & needed but note the "!" shell escape):
!echo "done body" | mail -s "done subject" firstname.lastname@example.org
Now Stata will ask Unix to send you an email. This is useful for getting messages when your code reached a certain point in the Stata code but is not completely finished. You could also ask for multiple emails at various points to monitor progress, maybe by changing the body or subject line accordingly.
Outputting tables from Stata
Generating use-able tables from Stata can be a painful experience. A particularly useful user-writte programs is Ben Jann's -estout- which allows to produce tables in text, table or tex format. Check the estout website for examples and details. Aside from regression tables, estout has subroutines to produce tables of almost everything – for instance estpost can tabulate summary statistics from summarize, tabulate and tabstat.
Latex users can fully leverage estout's flexibility. You can specify your own table headers and footers, and in this way use multicolumns and additional column headers. It is very worthwhile to spend time writing the code for producing exactly the table you want. If you have to make any changes you save the manual formatting.
Automatically generating pdfs of Stata tables and graphs
Stata doesn't output pdf files directly but you can use the shell escape to compile latex files as pdf documents. Normally you would include tex files produced by Stata in your main file and then compile that file manually. But you can ask Stata to call pdflatex and do this for you. You end up with a readily pdf'ed file.
For this trick you need a functioning latex setup on your computer or server. The following example shows how to automatically compile a pdf with a tex table from Stata that was outputted with -estout-, and an eps graph. First, set up the tex file you want to compile, using \input to include your table and \includegraphics for your graph.
% Latex file myoutput.tex
Then write your Stata program to produce the table mytable.tex and the graph mygraph.eps. At end of the do-file you will call pdflatex via the shell escape "!" to compile the document. If you save your graphs as eps files you will have to convert them to pdf first via epstopdf. The option -no-shell-escape is for pdflatex, not Stata.
* Stata do-file
eststo mytab: reg mpg weight length
esttab mytab using "mytable.tex", style(tex) replace
scatter mpg length
graph export mygraph.eps, replace
!pdflatex myoutput.tex -no-shell-escape
Of course you can add anything your tex file handle and pick your favorite ways of including graphics and the like. You could even recompile your entire paper from within Stata although you might want to check your numbers carefully.
Better do-file editor
Until Stata 10 the do-file editor was just a basic text editor; from Stata 11 this has been much improved. You can also use an external editor to interact with Stata which has the additional advantage is that the editor will remain open if Stata crashes. The editor can also double for other programs (e.g. latex editing) and functions (comparing multiple versions of do-files).
There are many good editors out there. Friedrich Huebler maintains instructions for integrating various editors with Stata. I previously used WinEdt ($30 for students) and am switching to the open-source Notepad++ (NPP). Both editors have many other functions and can be programmed with macros.
Both provide syntax highlighting and automatic backup and can run Stata code straight out of the editor. NPP is free and has a number of useful plugins (most easily installed from within NPP) such as NppFTP which allows you to open remote documents directly in the editor. It also allows code-folding. On the other hand, WinEdt can automatically close pdf documents in Acrobat Reader which is useful if you're recompiling latex documents. It integrates easily with MikTEX and has a toolbar with latex symbols.