Stata Resources for Data Management

This page is the collective product of my Stata programming classes (each segment below is roughly a course day in the class). We encourage other people to use and build upon these materials. I would appreciate links to material that builds on these topics, so I can integrate links into this page.

All readings should have hotlinks that worked at the time of the course (sorry if they break over time as people move their stuff around). Email me if something is broken and I can see if I have an archive copy.

As for the books, I used Scott Long's Workflow of Data Analysis Using Stata and Newton and Cox's Seventy-Six Stata Tips. Those are the only readings below without hotlinks.



Replicability, Efficiency, and Elegance as Programming Goals
Naming Conventions for Files
Writing and Reading Documentation

Help menu and items:
PDF Documentation:
Long, Ch 1: "Introduction"
Long,  Ch 2: "Planning, organizing and documenting"

Learning resource produced in class: Explainer 1
Explainer 2

Also: Cheat sheets to help with planning, organizing, and documenting:
Cheat sheet 1
Cheat sheet 2
Cheat sheet 3
Cheat sheet 4
Worksheet to help you think about file organization

An introduction to do files
Debugging errors Stata finds
Finding errors Stata doesn’t notice

Long,  Ch 3: “Writing and Debugging Do Files”
Long, Ch 5.2-5.4 from “Names, Notes and Labels”

If you are still confused:

Learning resource produced in class:
Explainer 1
Explainer 2

Ado Files and the SSC Archives + Getting Help

Stata tip 30: “May the Source be With You (viewsource)”
Long, Appendix: “How Stata Works”
General overview for Stata beginners and relative novices:…

Learning resource produced in class: Video Explainer 1
PDF Guide from


Data Storage types
Variable Naming
Labels and Notes

Variable Types and Precision and String heading:
Long, Ch 5.5-5.10: "Names, notes, and labels"
Drop, Keep, Rename, Labels:
Describe and Labels:
Importing Data:… (from a common format like Excel)
Stata Tip 35: Detecting Whether Data Have Changed (Datasignature)
Other ways to get data into Stata: Pages 3-11 from Omnibus Stata start-up guide:…

Learning resource produced in class:
Explainer 1

Importing and summarizing data

Long, Ch 6.1-6.2 & 6.4-6.5: “Cleaning your data”
Stata tip 66: “ds- a hidden gem

Learning resource produced in class:
Additional Explainer

Basic Data Manipulation

Long, Ch 6.3: “Cleaning your data”
Stata Tip 52: Generating composite categorical variables
Stata Tip 2: Building with floors and ceilings
help egen in Stata—mostly focus on the functions available

If the Long was confusing to you, also read:
replace and recode:
Replace and indicator variables: (through Indicator variables)
Using tabulate to recode into dummies: “Answer 2 of 3: Use tabulate”… &

If egen was confusing to you, also read:…

Learning resource produced in class:
Additional Explainer


By, System Variables, Return Codes, Egenmore

By from Stata PDF Documentation (see course webpage)
System variables:
Using system variables:…
Return codes and values:
Long 4.2: “Information returned by Stata commands
Help egenmore (from Stata)
Stata tip 14: Using value labels in expressions

Learning resource produced in class:
Explainer 2

Regular Expressions

Regular expressions available in Stata:
Regular expression commands in Stata:
Example of using a regular expression:
Stata Tip 60: Making fast and easy changes to files with filefilter
help filefilter (when in Stata)

Learning resource produced in class:
Regular Expressions, year 2.pdf
Exercises using tweets as text

Manipulating Entire Datasets: Append, Merge, Collapse

Stata Tip 5: Ensuring programs preserve data sort order
help append (in Stata)
Append:… (including the Q&A below it)
Stata Tip 73: Append with care!
Long, Ch 6.6: Cleaning your data
help collapse (in Stata)
Applying merge to data cleaning: Stata Tip 64: Cleaning up user-entered string variables

If Long was unclear to you:
Merge overview:…
1-to-1 merging:… &…
One to many merging:…
Merge and append:

Learning resource produced in class:
Merge 1:1, m:1:
Merging multiple datasets in a row:
Explainer 1
Explainer 2

Reshaping Datasets and Transferring and Preserving Data

Reshape in general:
Reshape to long:
Reshape to wide:
Stata Tip 45: Getting those data into shape (reshape)
StatTransfer manual, Pp 50-78
Long, Ch 8: "Protecting your files"

Learning resource produced in class:
On Reshape:
On StatTransfer: Note: you could run the commands he shows you in the video by saving the commands into a text file with a .stcmd extension, placing the file in the directory that contains the things you want to transfer, and then double-clicking on the program in Windows-- it will work the same--but easier for you--as running it through the command window)
On Backups and Mirrors/Preserving Data: but I think the whole site is worth looking at b/c it has one of the funniest tag lines ever (see
Reshaping Datasets, year 2.pdf
Reshape Exercises and Solutions


Intro to loops

Long, Ch 4 (pp. 83-105, except 4.3.3): "Automating your work"
Macros: "Macros" on -- up to, but not including, “Nested Loops”
"B] macros" on (pp 10-14)

Learning resource produced in class:
Explainer 1
Cheat sheet

forval versus foreach, forval in detail, i & local j, nested loops

Long, Ch 4 (4.3.3 & 105-end of chapter): "Automating your work"
Forval: "For Loops & Nested Loops " on

Learning resource produced in class: & the accompanying dataset  & do file
forval loops:
foreach loops:
nested loops:
Explainer with exercises

loops continued, preventing errors (assert), and debugging loops (trace, pause, and capture)
Stata Tip 32: Do Not Stop (do nostop)
Stata Tip 41: Monitoring loop iterations
help assert in Stata
help pause in Stata
help capture in Stata

Learning resource produced in class:
Loops Continued, preventing and debugging errors, year 2.pdf
Debugging exercises


Stata tip 54: Post your results (postfile and postclose)

Learning resource produced in class:

Explainer 1
Explainer 2 with exercises

Programs versus do files: what’s the difference? And, passing arguments

Readings: “Programs” section… (read entire page, including questions and answers at the bottom of the page) , sections 18.4, 18.4.1

Learning resource produced in class: Explainer 1
Writing Programs in Stata

Gettoken and more on arguments , section 18.4.6

Learning resource produced in class:
Explainer 1

Closing Thoughts on Stata

Learning resource produced in class: Wrap-up resources


Windows batch files (which have very similar, but often more powerful analogs in Unix, and hence Macs) (skim, but read: dir, cd, copy, del)

Learning resource produced in class: Explainer 1
Video on bash commands for unix
Windows batch command video



Working with Geo-Data:……

Matrices in Stata (Intro):…

Transforming Edge Lists from Pajek:…