DO File Documentation


Summary

A .do extension has two unrelated meanings. As a web address ending, *.do is a Java Servlet URL mapping from the Apache Struts era: a suffix the server maps to a controller servlet, not a file you can download or open. As an actual file on disk, .do is almost always a Stata do-file, a plain-text script of statistical commands (MIME text/plain) that you run in Stata or read in any text editor.

Technical details

FeatureValue
Extension.do
Meaning 1 (URL suffix)Java Servlet URL mapping (*.do) — not a real file
Meaning 1 developerApache Struts / Java Servlet convention
Meaning 2 (real file)Stata do-file — statistical command script
Meaning 2 developerStataCorp
Do-file format typePlain-text script (one Stata command per line)
Do-file MIME typetext/plain
EncodingPlain text (usually UTF-8 or ASCII)
Magic numberNone — identified by Stata syntax, not a header
Introduced1985 (Stata 1.0)
Runs inStata (execute with the do command)
Opens for editing inAny text editor (VS Code, Notepad++, Notepad)
Third meaningModelSim / QuestaSim DO macro (Tcl-based HDL simulation script)
Related extensions.ado, .dta, .smcl, .sthlp
Open standardNo (Stata is commercial software)

What is a .do file?

The .do extension is used for two things that have nothing to do with each other, and telling them apart is the whole point of this page. If you saw .do at the end of a web address, that is a Java Servlet URL mapping: a suffix the web server routes to server-side code. It is not a file sitting on your computer, and there is nothing to download, open or convert. If instead you have an actual .do file, it is almost certainly a Stata do-file: a plain-text script of statistical commands. The rest of this article covers both, starting with the URL meaning because it is the one that sends confused visitors here.

The *.do servlet URL mapping

In the Apache Struts framework and other Java servlet applications of the early 2000s, developers configured the server to hand any request whose path ended in .do to a controller servlet. A URL such as /login.do or /submitOrder.do did not name a file named login.do on the server’s disk. It was a routing convention: the deployment descriptor (web.xml) contained a <servlet-mapping> with a <url-pattern>*.do</url-pattern>, and the servlet container matched the request against it and invoked an Action class.

<servlet-mapping>
  <servlet-name>action</servlet-name>
  <url-pattern>*.do</url-pattern>
</servlet-mapping>

The suffix was chosen because it reads like a verb (“do this action”) and kept URLs framework-neutral, hiding the underlying .jsp or class names. So if your browser shows a .do address or a download dialog offers a .do, the correct answer is that there is no file format to open: the server generated a normal HTML (or JSON) response, and any downloaded .do is just that response saved with the URL’s ending. Struts’ classic *.do style is largely historical now, replaced by extensionless REST-style routes.

The Stata do-file: a reproducible-analysis script

An actual .do file on disk is a Stata do-file. StataCorp first shipped Stata in 1985, and the do-file has been its core mechanism for reproducible research ever since. The file is plain text with one Stata command per line: data cleaning, variable generation, regressions, tables and figures are all written as commands so an entire analysis can be re-run from start to finish. There is no binary header and no magic number; a do-file is recognised by its Stata syntax, not by any signature, and it commonly opens with comment lines marked * or //.

* clean.do — example Stata do-file
clear all
use "survey.dta", clear          // load a Stata dataset
gen logwage = log(wage)          // generate a new variable
regress logwage educ experience  // run an OLS regression
outreg2 using results.doc        // export the table

Do-files are the standard way analysts document and share work, which is why students and researchers meet them constantly: academic journals routinely require authors to publish a replication .do script alongside the dataset it operates on (usually a .dta file). Open the .dta in Stata, run the .do, and the published results reappear. A do-file can also call other do-files, or run installed .ado programs, which is how larger projects are organised.

Running a do-file versus reading it

Two different needs bring people to a do-file, and they need different tools. To read or edit the code, any text editor works, because the file is plain text; VS Code or Notepad++ with a Stata syntax extension adds highlighting and makes a long script legible without any Stata licence. To execute the analysis you need Stata (or a compatible engine), because the commands reference Stata’s data engine, estimators and saved results. In Stata’s Do-file Editor you run a script with Ctrl+D (Cmd+Shift+D on macOS), with do "path/script.do" in the command window, or headless from a terminal with stata -b do script.do, which writes a log instead of showing output.

Related Stata files clarify what a do-file is and is not. A .do file is a script you run for a specific analysis; a .ado file defines a reusable command that Stata loads automatically when you call it by name. A .dta is a dataset, .smcl is a formatted output log, and .sthlp is a help file. There is no reliable automatic translator from Stata syntax to R or Python; helper packages such as pystata run Stata rather than convert its code, so porting a do-file is manual work. The dataset it uses, however, imports into R or pandas directly.

Frequently asked questions

How do I open a .do file without Stata?

Any text editor opens it, because a do-file is plain text. VS Code or Notepad++ with a Stata syntax extension makes it readable. You only need Stata (or a tool with a Stata engine) to actually execute the analysis and reproduce the results.

Why did I receive a .do file together with a .dta file?

That pairing is a Stata replication package. The .dta is the dataset and the .do is the script that operates on it. Open the dataset in Stata, then run the do-file to reproduce the published tables and figures.

If it came from a web address ending in .do, there is nothing to open: that is a server-side servlet URL, not a file format. Any file your browser saved is just the page’s response with the URL’s ending. If you genuinely have a Stata script, use a text editor or Stata as above.

References