The 411 on Home-Brewed Auto-Do Files: init.ado

If you know any Stata commands, then you know enough to make your own auto-do file. This walkthrough will create a simple ado file you can invoke that welcomes you to Stata and navigates to the directory you typically work from.

An ado file is a script that you invoke from Stata’s command line. One example of an ado file is “describe,” one of the first commands Stata learners use to see information about the variables in their dataset. In Stata, type “which describe,” and Stata gives you a directory path. When you invoke “describe,” Stata executes the script at that location (on my Mac, for example, “/Applications/Stata/ado/base/d/describe.ado”). If you pass parameters to an auto-do file, they are stored as local macros `0′, `1′, `2′, etc. Another post about clock.ado uses parameters.

If you open the describe.ado script in your favorite text editor, you’ll see something like:

*! version 2.1.0  25feb2010
program describe, rclass

version 9
local version : di “version ” string(_caller()) “:”
syntax [anything] [using] [, SImple REPLACE *]

… a bunch of other Stata code, just like a do-file …


Make a document called “init.ado” in any text editor, and save it in your “personal” directory you found from the sysdir command. It should be in a directory “ado.” If the directory “personal” doesn’t exist, make it inside “ado.”

For this fun ado file, we’ll use the “cd” command, which stands for “change directory.” Have Stata navigate to the folder you usually work from by typing “cd <directory path>” (eg, the path to a course’s folder). From there, you can cd into a subfolder (eg, the current problemset or project you’re working on), and use Stata’s “doedit” command to open the .do file there.

Add to your ado file any other helpful commands you tend to run, and you can even impress your friends by having Stata welcome you each time you type it.

program define init
cd /Users/Alex/Homework/Junior
set more off
di “Hello, Mr. Bell. What may I compute for you today?”

A helpful note on testing ado files is that Stata seems to cache them (it saves time not to have to look for an ado file every time you invoke it, but to store the script once it’s been searched for). That means if you write your ado file, test it, then go back to edit and re-save it, you’ll probably have to restart Stata to make it load the new version.