| name | running-stata |
| description | Run Stata batch jobs on the Yale SOM HPC cluster with logs, scratch temp, CPU matching, and license courtesy. TRIGGER when writing Stata sbatch scripts for the Yale SOM HPC cluster, choosing Stata/MP cores there, or diagnosing Stata batch jobs/licenses on the cluster. |
| related | ["managing-jobs","using-the-filesystem","accelerating-python","self-diagnosing-resource-use"] |
| updated | 2026-06-10T00:00:00.000Z |
Running Stata
Rule: run Stata in batch jobs, write logs, put temp files on scratch, and request only the cores Stata will use.
What you get: the stata module is a complete MP install — no package-bootstrap step (unlike R, whose base module ships nothing). ssc install packages land in your personal ado directory.
Slurm template
#!/bin/bash
set -euo pipefail
module purge
module load stata
export OMP_NUM_THREADS=${SLURM_CPUS_PER_TASK:-1}
export OPENBLAS_NUM_THREADS=${SLURM_CPUS_PER_TASK:-1}
export STATATMP=/gpfs/scratch60/$USER/stata-tmp/${SLURM_JOB_ID}
mkdir -p "$STATATMP" logs
trap 'rm -rf "$STATATMP"' EXIT
cd /gpfs/project/myproject/code
stata-mp -b do src/main.do
Two log gotchas, both verified on the cluster:
- Read the
.log, not the .out. Under stata-mp -b, Stata sends all output to its own log file; the Slurm --output .out file ends up empty. The log using inside your do-file (below) is what you read.
stata-mp -b do src/main.do also writes main.log in the working directory (named after the do-file), in addition to your log using. Either expect/clean up that stray file or name your log using differently.
- Check the log for errors — batch mode can exit 0 on a Stata error. Grep the
.log for an r(###) return code; set -e will not catch a do-file error on its own.
Do-file preamble
capture log close _all
local jobid : env SLURM_JOB_ID
if "`jobid'" == "" local jobid local
log using "logs/main_`jobid'.log", replace text
local ncpus : env SLURM_CPUS_PER_TASK
if "`ncpus'" == "" local ncpus 1
set processors `ncpus'
local statatmp : env STATATMP
di "STATATMP=`statatmp'"
set more off
version 19
Memory hygiene
compress
describe
memory report
Use frames, tempfile, and preserve/restore deliberately. Drop unneeded variables before merges.
Job arrays
local task_id : env SLURM_ARRAY_TASK_ID
if "`task_id'" == "" local task_id 1
di "Running task `task_id'"
Each array task should write a separate output file.
License courtesy
Stata MP licenses are shared. Do not leave interactive Stata sessions idle. Do not request more CPUs than set processors uses.
Checklist
Further reading