Stata .do file template

✍️
Last updated 2026-07-12

Having a template for new .do files is an easy time saver. This template assumes you're running everything in one file, but this can be easily modified if you break your workflow into multiple do files such as 01_cleaning.do and 02_analysis.do etc with a primary .do file that runs all project .do files if needed. (For more on that type of work flow, check out Scott Long's The Workflow of Data Analysis Using Stata or see a slightly more involved example from Glenn Hoetker here).

Here's the template. Feel free to use/modify for your needs.

* WORKING DIRECTORY
// use -pwd- and -cd- to access the project-level folder

cd XXXX/XXXX/				

**# LOG START

capture log close

global VERS = strofreal(date("$S_DATE","DMY"), "%tdCY!-N!-D")
log using "log/log-${VERS}", replace

**# INFO

/*
  PROJECT: 	XXXX
  AUTHOR: 	XXXX
  DATE:		XXXX-XX-XX
  NOTE: 	XXXX
*/

**# BOILERPLATE

version 19.5            // Set version for compatibility
set more off            // Disable partitioned output
clear all              	// Start with clean slate
set varabbrev off       // Do not allow abbreviations of variable names
* set scheme stmono2	// Set graphs to black and white if desired

**# LOAD DATA


**# CLEAN DATA


**# ANALYSIS


**# LOG END

capture log close
translate "log/log-${VERS}.smcl" "log/log-${VERS}.log", replace

There are many ways to break up sections in text files. In Stata, using **# creates a section bookmark available in the .do file editor equivalent to an H1. This can come in handy when you .do files start getting long. A sub head can be created with **## and so on.

My project folder setup typically looks something like: