Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

stacy run

Execute a Stata script with error detection

Synopsis

stacy run [SCRIPT] [OPTIONS]

Description

Executes Stata scripts in batch mode and parses log files for errors. Unlike stata-mp -b, returns proper exit codes that reflect whether the script succeeded or failed—enabling integration with Make, Snakemake, and CI/CD.

The command runs Stata with -b -q, parses the log for error patterns, and returns an appropriate exit code (0 for success, 1-10 for various errors).

By default, clean output is shown after execution (boilerplate stripped). On failure, error details with official Stata documentation links and the log file path are displayed. Use -v to stream the raw log in real-time instead, or -q to suppress all output.

Multiple scripts can be run sequentially (default, fail-fast) or in parallel (--parallel). Parallel mode runs all scripts regardless of failures.

For interactive use where you want to quickly check a result, see stacy eval.

Arguments

ArgumentDescription
<SCRIPT>Script to execute

Options

OptionDescription
--cdChange to script’s parent directory
-c, --codeInline Stata code
-C, --directoryRun Stata in this directory
--profileInclude execution metrics
-q, --quietSuppress output
--traceEnable execution tracing at given depth
--verboseExtra output

Examples

Run a script

stacy run analysis.do

Multiple scripts (sequential)

Runs in order, stops on first failure

stacy run clean.do analyze.do report.do

Parallel execution

Run all scripts concurrently for faster execution

stacy run --parallel *.do
stacy run --parallel -j4 a.do b.do c.do

Inline code

Execute Stata code without creating a file

stacy run -c 'display 2+2'
stacy run -c 'sysuse auto, clear
summarize price'

Working directory

Run in a specific directory (script paths resolved before cd)

stacy run -C reports/pilot/ table.do
stacy run --cd reports/pilot/table.do

Verbose output

Stream log file in real-time

stacy run -v long_analysis.do

JSON output

Machine-readable output for CI/CD

stacy run --format json analysis.do

Execution tracing

Enable Stata’s set trace on for debugging

stt run --trace 2 analysis.do
stt run --trace 2 -v analysis.do

Exit Codes

CodeMeaning
0Success
1Stata error (r() code detected)
2Syntax error
3File error (not found, permission denied)
4Memory error
5Internal stacy error
10Environment error (Stata not found)

See Exit Codes Reference for details.

See Also