Stata is a statistical package which is:
Highly optimized for common econometric methods, both in syntax and computation
Easy to use
Well-supported by an active user base
Commonly installed in secure data labs
Stata is not:
As flexible as R and Python (Good luck applying machine learning in Stata!…)
As widely used in the private sector as it is in academia and think tanks
While R and Python are also valuable to learn, Stata remains the most-common programming language in empirical economics and is an excellent tool for learning and applying econometric methods. The majority of papers in applied economics include analysis using Stata and the majority of applied economists are comfortable using it.
The Stata Interface consists of the following windows:
The roles of each element are:
The command window is where you can type in commands instructing Stata to perform calculations
The results window shows the output from any commands you run
The review window gives previous commands. This lets you keep track of what you’ve asked Stata to do.
The variables window gives information about variables. Note that each variable has a name and a label, along with some other properties
There are two ways to run code in Stata:
Typing directly into the command window then pressing enter
Writing then running a .do file
While the first option is easy for one-off commands, as you become familiar with the software it is best to use .do files.
.do files are opened in the do-file editor, which can be opened through File -> New. They are run either by clicking ‘do’ or by pressing CMD+SHIFT+d
(OSX) or CTRL+d
(Windows).
As a rule, your projects should be contained in one or more .do files which contain all code needed to produce your analysis. The .do file should provide everything necessary to reproduce your results, from the raw data to the final output.
Perhaps the most important Stata command to know is help
, which brings up Stata documentation. For example, typing help regress
into the command window then hitting enter will bring up all documentation on the regress
command in Stata:
See the end of this tutorial for further resources.
Our application will be a classic paper in Labor economics, LaLonde (1986). In this paper, LaLonde investigates the impact of a randomized employment program on earnings.
** note that the snippets below contain both code and output. All code lines are indicated by either .
or >
. Anything else is output. **
.do files are the building blocks of Stata projects, so before launching into commands we need to first think about how to set them up.
We can now start looking at an example .do file. A typical project consists of several (often several dozen) .do files each with hundreds of lines of code which are interlinked, but here we will just work from a single file.
You should think about the .do file as a series of text instructions which tell Stata what to do.
A .do file should start with a title and short description. My .do file for example starts with the following:
. /* Lalonde analysis > Jack Blundell > Worked example of analysis of lalonde 1986 data. > Structure: > 0. Setup > 1. Import data > 2. Rename and relabel variables > */
The /*
and */
symbols tell Stata that the enclosed text is not to be read as Stata commands, but instead provides information and context to the researcher reading the code. It is good practice when writing code to include some non-command text. These might be section titles, or small annotations describing code. Any text following *
or //
, or enclosed between /*
and */
will not be read as a command by Stata, so can be used to convey this information.
Here’s an example of a comment, followed by a line of code (don’t worry about the code for now):
// regress earnings on treatment for adult sub-sample
reg y t if age >= 18
While your code might seem self-explanatory to you at first, oftentimes this is not the case for your co-authors, a grader or indeed your future self.
(To avoid clutter, I exclude all comments in the code included below, but you’ll see them in the full .do file later in this tutorial.)
Next up in my .do file is the following:
. clear
. set more off
These two commands (clear
and set more off
) are standard to include at the top of .do files. They clear the system and allow Stata to print large amounts of output to the screen.
Next I set the working directory. For this example, we will work in a single folder which will be our only directory, though more complex projects will require further structure. For a small project, this should be where your .do file is saved, and where the data file you will use is saved. Unless otherwise specified, any output you generate will be saved here.
. cd "~/git_repos/metricsinstata/docs/part1" /Users/jack/git_repos/metricsinstata/docs/part1
The following two commands relate to log files. A log is a record of your code and the output your code generates. While it is possible for small projects to run all your code each time and simply inspect the results in the Stata results window, for larger projects it is useful to have a record of all your output. First I close any existing log file:
capture log close
The code above will close any existing log file you have open, so that a new one can be opened. The capture
command here is useful but we will not go into detail of how it works quite yet.
The next command starts a new log:
log using "lecture1log", replace
The command tells stata to log
(create a log file containing results) and to save the file (using
) as lecture1log
. Any results generated will now end up in a .smcl
log file in the main working directory. The replace
option which follows will crop up several times in this example attached to numerous commands. It tells Stata to overwrite any existing file as required. Excluding replace
will result in an error message if an overwrite is attempted.
Stata datasets are denoted by “.dta”.
The dataset containing the LaLonde data is called “nsw.dta” and is available at http://www.nber.org/~rdehejia/data/nsw.dta.
You need to download this file and place it into your working directory, which is the folder you set earlier using the cd
command.
You then load the data into Stata by using the use
command
. use "nsw.dta"
To see what the data looks like now it is in Stata, typing the command browse
into the command window brings up the data in a familiar spreadsheet-style format:
Here we see what looks like an Excel spreadsheet. Each row represents an observation and each column a variable. For example, we can see that for the first observation, variable treat
is 1 and variable age
is 37.
“There are only two hard things in Computer Science: cache invalidation and naming things.” (Phil Karlton)
While using browse
can be useful to take a look at the data, a more useful command to show what a dataset contains is describe
.
. describe Contains data from nsw.dta obs: 722 vars: 10 18 May 2012 09:35 size: 20,938 ────────────────────────────────────────────────────────────────────────────────────────────────────── storage display value variable name type format label variable label ────────────────────────────────────────────────────────────────────────────────────────────────────── data_id str14 %14s treat byte %8.0g age byte %8.0g education byte %8.0g black byte %8.0g hispanic byte %8.0g married byte %8.0g nodegree byte %8.0g re75 float %9.0g re78 float %9.0g ────────────────────────────────────────────────────────────────────────────────────────────────────── Sorted by:
Here, we can now see that we have 722 observations and 10 variables. Each variable is named, but the dataset does not yet contain any variable labels.
It is possible to rename variables as follows:
. rename treat banana
This command renames variable treat
, assigning it the new name banana
. As banana
is not a very informative variable name, we might like to change it back as follows:
. rename banana treat
In general, your variable names should be short, informative and easy to type. You can include further information via variable labels:
. label variable treat "Treatment indicator"
A few example variable names:
earn
sales
treatment
Use variable labels to fully specify what each variable represents, for example:
“Earnings in 1,000$”
“Sales in $ per day”
“=1 if received employment training”
Lets check out our data again:
. des Contains data from nsw.dta obs: 722 vars: 10 18 May 2012 09:35 size: 20,938 ────────────────────────────────────────────────────────────────────────────────────────────────────── storage display value variable name type format label variable label ────────────────────────────────────────────────────────────────────────────────────────────────────── data_id str14 %14s treat byte %8.0g Treatment indicator age byte %8.0g education byte %8.0g black byte %8.0g hispanic byte %8.0g married byte %8.0g nodegree byte %8.0g re75 float %9.0g re78 float %9.0g ────────────────────────────────────────────────────────────────────────────────────────────────────── Sorted by: Note: Dataset has changed since last saved.
You may have noticed that rather than typing describe
, the above code snippet achieves the same outcome with des
.
This is a key feature of Stata code. Commands (and variables) can be abbreviated. In the rest of these tutorials, I will use full command names when first using a command, then follow standard abbreviations if the command comes up again.
The very final thing to do is to close the log file:
log close
The log file you opened at the start of the do-file will now be closed and saved in your directory.
Now lets take a look at what the complete .do file looks like:
/* Lalonde analysis
Jack Blundell
Worked example of analysis of lalonde 1986 data.
Structure:
0. Setup
1. Load data
2. Rename and relabel variables
*/
*** 0. Setup
clear
set more off
// set working directory
cd "~/git_repos/metricsinstata/docs/part1"
// open log
capture log close
log using "lecture1log", replace
*** 1. Load data
use "nsw.dta"
*** 2. Rename and relabel variables
describe
rename treat banana
rename banana treat
label variable treat "Treatment indicator"
des
// close log
log close
The following two sections of code (purely for illustration) contain exactly the same commands:
use "fruitdat.dta"
rename v1 fruit
label variable fruit "Type of fruit"
rename v2 sales
rename v3 price
label var sales "Sales (units)"
label var price "Price ($)"
sum price if fruit == "apple", det
// load dataset
use "fruitdat.dta"
// name variables
rename v1 fruit
rename v2 sales
rename v3 price
// label variables
label variable fruit "Type of fruit"
label var sales "Sales (units, weekly)"
label var price "Price ($)"
// average price of apples
sum price if fruit == "apple", det
The second output is structured logically, uses space to distinguish different tasks and provides comments. You should aim for this.
I’m running my .do file but nothing is coming up in the main Stata window. The problem here is usually that your .do file editor window is associated with a different main interface window. It is possible to have multiple instances of Stata open simultaneously, each with their own associated .do file editor window. The solution is either to find the correct interface window, or to save the .do file and open it in an alternative editor.
When I try to change into my main working directory, I get the error unable to change to /XXX
. This is usually because there is a small mistake in the file path. You can get around this by manually choosing the directory using “File->Change working directory”. This will change the directory and print the code to do so into the results window, which can then be copied into a .do file.
There’s too much on the screen to have my .do file editor and main interface window open side-by-side. This is usually the case when working on a laptop. You might like to restrict the main interface window to just the results and command windows. These are the only essential parts of the main interface.
pageup/pagedown
/ fn + up/down arrows
(OSX/WINDOWS) when in the command window. This retreives previously-run Stata commands
SHIFT+CMD+D
(OSX) / CTRL+D
(WINDOWS) when in .do file editor. This runs the do file. If nothing is highlighted, the entire .do file will run. If a section of code is highlighted, only that section will be run.
SHIFT+up/down arrows
(OSX/WINDOWS) when in .do file editor. This is how you can quickly highlight sections of code to run. You can then use the above command to run this in part of the .do file in isolation
TAB
(OSX/WINDOWS) when in the command window. You can use this to autocomplete variable names and directories.
There exist a large number of comprehensive Stata resources available:
User discussions at https://www.stata.com/statalist tackle many common problems
Princeton’s introductory Stata course: https://data.princeton.edu/stata
Harvard’s introductory Stata course: https://tutorials.iq.harvard.edu/Stata/StataIntro/StataIntro.html
Stata’s own manual is verbose but useful: https://www.stata.com/manuals/gsm.pdf
Cheatsheets can be very handy. If working a lot in Stata, its nice to have some printed copies of these next to your desk: https://geocenter.github.io/StataTraining/portfolio/01_resource/
For those that want to learn more about how to structure large-scale social science projects and best coding practice, the definitive guide is Gentzkow and Shapiro (2014). This is particularly useful for those working as Research Assistants. It is found here: https://web.stanford.edu/~gentzkow/research/CodeAndData.pdf
References
LaLonde, Robert J. “Evaluating the Econometric Evaluations of Training Programs with Experimental Data.” The American Economic Review, 1986