Skip to content

Stata Tips

Meredith Welch edited this page May 8, 2020 · 45 revisions

Tips and tricks for working with other people's Stata programs

Configuring Stata programs

If at all possible, include a config.do file, based on template-config.do.

A more elaborate example is https://gist.github.com/larsvilhuber/6bcf4ff820285a1f1b9cfff2c81ca02b

Running different versions of Stata

You can change the version of Stata that runs by including "version ##" as a command. Sometimes authors will specify this, but generally, you should be able to run code in many different versions of Stata. If this comes up you can simply change the number to the version that you have on your computer or on CISER, or just comment this command out.

Running Stata from the command line

It sometimes is more convenient to run Stata from the command line. How to do this varies by operating system. In almost all cases, you need to know where Stata is installed.

  • Windows: See this Stata page. In essence, on CISER nodes, navigate to the directory where the Stata program you want to run is located (here: master.do). Then, in the address bar of the file browser, type powershell. In the command line window, type
& 'C:\Program Files\Stata16\StataMP-64.exe' /b do .\master.do

will run the program, and create a logfile master.log (and any configured log files).

  • Linux: Similar to the Windows instructions, in a terminal, navigate (cd /path/to/directory) to the directory with the program you want to run. Most Linux systems will have the Stata command in the "path", meaning it will be automatically found.
stata-mp -b do master.do

will run the program.

PERSONAL ADO DIRECTORY

Ado files not found

Issue: When an error message such as xml_tab command not recognized or outreg2 command not recognized appears...

Solution it usually means that you need to first install the command before running the .do file. Try

ssc install outreg2

or possibly

search this_great_command

followed by

net install  this_great_command, from(https://this_great_command.com/stata)

Great tip: When searching for a package, the Stata help browser will show in the address bar "net describe this_great_command, from(https://this_great_command.com/stata). Simply copy and paste into your config.do and change describe to install.

NOTE Please be sure to make a note of this installation in the REPLICATION.md!!

Issue: When trying to install some extension Stata shows an error message such as: _file c:\ado\plus\next.trk already exists_

This issue usually occurs when running on a university system where you do not have rights to install to C:

Solution: Be sure you are including config.do, which has lines like

global basepath "/path/to/your/project/directory"
sysdir set PERSONAL "$basepath/ado/personal"
sysdir set PLUS "$basepath/ado/plus"

where you would create a "ado" directory in the "project" folder you are currently using for the replication (i.e., ..../10.1257/mac.12345/replication_netid/ado). See the template-config.do.

Stata will then install the new extension into that directory instead of c:\ado\plus (source )

I want to run my program in an older version of Stata?

Use the version command to emulate running the program on an older version. For example, to emulate Stata 12.1, use the following line:

version 12.1 

This fix may be relevant when running into problems with the tobit regression.

ADO FILES

If the programs include .ado files, these contain Stata commands that are presumably called from the main .do files.

Capturing ADO install commands

If packages need to be installed, the ideal way is to capture them all in a setup program, e.g., 00_setup.do that is then run before any other .do files.

For an example, see https://gist.github.com/larsvilhuber/8ead0ba85119e4085e71ab3062760190

MAIN DO FILE

Log files:

  • Always create log files for the main .do files (unless the .do files already contain code creating them). Oftentimes authors will not create separate output files for the results so you’ll be required to scroll through the log file to find them. Log files are also useful for debugging when the code is very long and doesn’t fit into the Stata results window.

  • The code for creating log files can be found in the template-config file. If you need to create more than one log file make sure that you change the name first (adjust "logfile_cdate'.log"`).

Running other do files

Some code has a main "replication.do" or "main.do" etc., which will have lines such as

do tables.do

This is a problem, because any parameters from main.do, such as changing where you install Stata packages (sysdir PERSONAL "/path/to/somewhere") are lost...

Replace the above with

include "tables.do"

and you will be fine.

The difference is that the first one executes the tables.do in a somewhat fresh environment. The second one simply includes the code (as if copying-and-pasting) in the main.do, by reference, and thus inherits everything that is transient (such as tempfiles, etc.)

This may not always work - sometimes authors rely on that reset that happens when you use the do tables.do version.

Always use include config.do for config.do files however.

Stata Errors

Varlist required error:

  • May occur if the author calls upon a varlist that s/he forgets to define. Check the other .do files to see if it has been defined elsewhere. If the definition is obvious (based on the article & data) insert a line of code defining it yourself.

Too many values error:

(SOLUTION?)

Varlist not allowed error:

(SOLUTION?)

Maxvar too small error:

  • Run .do file using Stata-MP and add set maxvar 32767 (maximum number of variables) to the config.do.

File ___ could not be opened error:

  • May appear after the esttab command when Unix and Windows paths are mixed.
esttab using "$res/Cage_Rueda_Table_4.tex"

Fix this by inserting cd $res before the esttab command. Then delete the $res/ part from the file name.

cd $res
esttab using "Cage_Rueda_Table_4.tex"

Options IK, CCT, and CV have been deprecated error:

May occur when using the bwselect() option after the rdrobust command. To fix this an older version of the rdrobust command must be installed. Install this by typing

net install st0366.pkg, from("http://www.stata-journal.com/software/sj14-4/") 

into the config.do. Don't forget to make a note of this in the Replication.txt!!

File ___.tmp not found error:

Problems with "xi" command:

The outdated command xi may sometimes cause errors that cannot be remedied by using an older version of Stata. To update the syntax follow this example:

xi: gen i.year i.adjustedmsc

This line of code currently produces the error message "invalid name". Correct this by dividing up the command into:

xi i.year
xi i.adjustedmsc

Problems with the "kdens" command:

May require the installation of kdens and possibly moremata.

Clone this wiki locally