Stata (Level 1)

From TaDa Wiki
Jump to: navigation, search


Stata is both powerful and relatively straight-forward. By far the hardest part of learning to use Stata well is just learning the names of commands are, because if you think of something weird you want to do, there's probably already a command to do it.

Basic Vocabulary

Stata has a pretty simple organization -- a user loads a dataset (only one dataset can be open at a time), and then modifies that dataset one command at a time. Datasets have "variables" (the columns of the dataset) and "observations" (the rows of the dataset).

Important Concepts and Tricks

All languages have their own quirks -- tricks or concepts that are unique to the language, and make working with it much, much easier. All too often, people who use the language often forget that these aren't immediately obvious, so don't talk about them when they create tutorials. So here are a few of the tricks and comments that fall into the category of "things we wish someone had made us aware of when we were getting started."

Learn to use and manage dofiles

Learning to use dofiles well, and to organize the code for your project is key to successfully using Stata for real projects. I highly recommend Section 9 of the University of Wisconsin tutorial (in Tutorials below) for this!

Add-On Functions

Stata has lots of useful built-in commands, but there are also LOTS of programs that have been written by other people. For example, as discussed in detail on another page, users have written some very nice commands for exporting regression results directly to LaTeX, so if you get stuck, google may be able to point you to a custom function to fix your problem! Once you know the name of the function, you can load it with either the command: ssc install PACKAGENAME for programs on the SSC repository, or by typing findit PACKAGENAME , clicking on the right result, and then clicking "(click here to install)" to search in more places.


Once you've mastered basic syntax, macros -- discussed here in a U of Wisconsin tutorial -- are the key to writing good Stata code.

If you have any background in programming for other languages, one note about macros: Stata using macros the way other programs use variables (for example, iterators in for-loops), but they aren't quite the same as local variables. When you send a command to Stata, the first thing it does is look for any macros. There are two kinds of macros, but the most common ones are enclosed by ` and ' (for example: `myMacro' ). If Stata finds a macro, it replaces that macro with the text associated with the macro name. Then it executes the command with that new text inserted.

Matrix Utilities

Stata is NOT the tool to use if you want to manipulate lots of matrices, but if, once in a blue moon, you want to work with a matrix and don't want to change languages, Stata does have two sets of matrix utilities. The first doesn't have a formal name, but you can learn about it by typing help matrix . The second is called "mata", and it's a little more complicated but also more powerful. Again, Stata is not the tool to use for lots of matrix work, but these are available for odds and ends!

Using Multiple Instances of Stata

Ever wished you could keep working on something in Stata while a really long do-file was running? Turns out you can actually open multiple copies of Stata at once (provided you have enough RAM and processing cores on your computer).

On a Mac, just go to your applications folder, find Stata, right-click on it, and select "Duplicate". This will create a new Application called "Stata copy" (or "StataMP copy" or something like that), which you can double click to open and can run alongside your original copy of Stata.

In Windows, just go to Start > Program Files > Stata to open a second instance.

Stata 13 and Reverse Compatibility

Datasets created in Stata 13 are not backward compatible, meaning they can't be opened automatically in earlier versions of Stata (this was not true for previous versions of Stata -- a Stata 12 dataset could be opened in Stata 11). Frustratingly, if you try and open a Stata 13 dataset in Stata 12, the error you get is file FILE.dta not Stata format , which is just confusing. However, you can load a small utility to allow you to open Stata 13 datasets in earlier versions of Stata. To load this module, run either ssc install use13 , then just type use13 instead of use when opening a Stata 13 dataset.


The University of Wisconscin Intro to Stata for Researchers Tutorial is quite good, and covers not only Stata syntax, but also how to organize projects (Section 9), which is incredibly important.

Once you've read their first tutorial, also check out their guide to Programming in Stata , or the list of all Stata topics.

UCLA Resources taught many, many of us to use Stata.