Stata Runner
This skill only tells you how to execute Stata code. It does not contain Stata programming reference material.
Running Stata from the Command Line
You can execute Stata code by running .do files in batch mode from the terminal.
Finding the Stata Binary
Stata on macOS is a .app bundle. The actual binary is inside it. Common locations:
# Stata 18 / StataNow (most common)
/Applications/Stata/StataMP.app/Contents/MacOS/stata-mp
/Applications/StataNow/StataMP.app/Contents/MacOS/stata-mp
# Other editions (SE, BE)
/Applications/Stata/StataSE.app/Contents/MacOS/stata-se
/Applications/Stata/StataBE.app/Contents/MacOS/stata-be
If Stata isn't on $PATH, find it with: mdfind -name "stata-mp" | grep MacOS
Batch Mode (-b)
# Run a .do file in batch mode — output goes to <filename>.log
/Applications/Stata/StataMP.app/Contents/MacOS/stata-mp -b do analysis.do
# If stata-mp is on PATH (e.g., via symlink or alias):
stata-mp -b do analysis.do
-b= batch mode (non-interactive, no GUI)- Output (everything Stata would display) is written to
analysis.login the working directory - Exit code is 0 on success, non-zero on error
- The log file contains all output, including error messages — check it after execution
Running Inline Stata Code
To run a quick Stata snippet without creating a .do file:
# Write a temp .do file and run it
cat > /tmp/stata_run.do << 'EOF'
sysuse auto, clear
summarize price mpg
EOF
stata-mp -b do /tmp/stata_run.do
cat /tmp/stata_run.log
Checking Results
# Check if it succeeded
stata-mp -b do tests/run_tests.do && echo "SUCCESS" || echo "FAILED"
# Search the log for pass/fail
grep -E "PASS|FAIL|error|r\([0-9]+\)" run_tests.log
Tips
clear allat the top of batch scripts — batch mode starts with a fresh Stata session, butclear allensures no stale state.set more off— prevents Stata from pausing for--more--prompts (fatal in batch mode).- Log files overwrite silently —
analysis.doalways writes toanalysis.login the current directory. - Working directory — Stata's working directory is wherever you run the command from, not where the
.dofile lives.