Skip to content
Tutorials Center
My learning

Topics

Lesson 3 of 36 ยท Getting Started

Your First Stata Session

Walk through a first Stata session with the built-in auto dataset: load it, describe, list, summarize, make a variable, run a regression, and save a log.

Time to type commands. In this lesson you will walk through a complete mini-analysis using auto, the practice dataset that ships with Stata (74 cars, 12 variables). You will load data, look at it, make a new variable, fit a simple model and record everything in a log. The Stata commands are read-only here; the editor below mimics what a command does with Python.

Type each command into the Command window at the bottom of Stata and press Enter. Results appear in the Results window.

Step 1: Start a log

A log is a text copy of everything you type and see. Start one first, so you never lose results.

STATA
log using first_session.log, replace text

replace overwrites an old log of the same name; text writes plain text instead of the SMCL format. Stop it later with log close.

Step 2: Load data

STATA
clear all                  // empty memory of data, graphs and programs
sysuse auto                // load the built-in dataset

sysuse loads example datasets from Stata's system folders. For your own files you will use use "file.dta" or import delimited. If a dataset is already in memory and has unsaved changes, sysuse auto refuses with no; dataset in memory has changed since last saved. Add , clear to discard the old data deliberately: sysuse auto, clear.

Step 3: Look before you analyze

Never run a model on data you have not looked at.

STATA
describe                   // variables, types, labels
list make price mpg in 1/5 // first five cars, three columns
summarize price mpg weight // count, mean, sd, min, max
codebook foreign           // details about one variable
  • describe lists each variable's name, storage type, display format and label.
  • list prints rows. in 1/5 means "observations 1 to 5". Without in or if, list prints everything, which can be long.
  • summarize reports the number of observations, mean, standard deviation, minimum and maximum for numeric variables.
  • codebook explains one variable in depth: how many unique values, missing values, labels.

The header of describe shows Observations: 74 and Variables: 12 for this dataset.

Step 4: Make a new variable

STATA
generate price_k = price / 1000
label variable price_k "Price in thousands of dollars"
summarize price_k

generate (abbreviated gen) creates a variable. Its expression is evaluated for every observation. label variable attaches a description that shows up in describe and in graphs.

The stand-in below does the same thing to a small table: it computes a new column for each row, then summarizes it, like generate followed by summarize.

Python Stand-in: generate, then summarize

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Step 5: A first regression

STATA
regress price mpg weight

This fits a linear model with price as the outcome and mpg and weight as predictors. The output has an ANOVA table at the top, model fit (R-squared, number of observations) on the right, and a coefficient table at the bottom with coefficients, standard errors, t statistics, p-values and 95 percent confidence intervals. We cover reading it fully in the regression chapter.

Stata also remembers the last result. After regress, the coefficients are stored and you can use predict to compute fitted values:

STATA
predict price_hat           // fitted values from the last regression

Step 6: Save and close

STATA
save auto_mine.dta, replace // write the dataset (with the new variables) to disk
log close                   // finish the log

Saving keeps the data; the log keeps the output. They are separate things. A very good habit is to save to a new name instead of overwriting the original source data.

The same session as a do-file

A do-file is a text file of commands that you can rerun with one click. This session as a do-file looks like this:

STATA
* first_session.do
clear all
log using first_session.log, replace text

sysuse auto
describe
summarize price mpg weight

generate price_k = price / 1000
label variable price_k "Price in thousands of dollars"

regress price mpg weight

save auto_mine.dta, replace
log close

Run it from the Command window with do first_session.do. Do-files are the foundation of reproducible work, and a full chapter is devoted to them. For now, notice the pattern: clear, log, load, inspect, create, analyze, save, close.

flowchart LR
    A["clear all"] --> B["log using"]
    B --> C["sysuse / use"]
    C --> D["describe, summarize"]
    D --> E["generate, replace"]
    E --> F["regress, graph"]
    F --> G["save, log close"]

The skeleton of nearly every Stata session.

When something goes wrong

Stata reports an error in red with a return code such as r(111):

  • variable pric not found r(111): a typo in a variable name.
  • unrecognized command: summarise r(199): Stata spells it summarize (American spelling; summ also works).
  • no observations r(2000): your if condition selected nothing.

Click the code in the message or type search rc 111 for an explanation. Errors stop a do-file at the failing line, which is a good thing: it prevents wrong results from flowing into later steps.

Python Stand-in: why a typo stops the run

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which command loads the built-in auto dataset?

    sysuse loads datasets shipped with Stata; use loads your own .dta files.
  2. 2. What does list make price in 1/5 print?

    in 1/5 restricts the command to observations 1 through 5.
  3. 3. What is the difference between saving the data and closing the log?

    The dataset and the log are separate files, so you need both save and log close.
  4. 4. A command stops with r(111). What most likely happened?

    r(111) is 'variable not found', usually a typo in a name.

Summary

  • A first session follows a pattern: log, load, inspect, create, analyze, save, close.
  • sysuse auto loads practice data; describe, list, summarize and codebook inspect it.
  • generate makes variables, regress fits a model, save and log close finish the work.
  • A do-file stores the same commands so you can rerun everything.
  • Errors show an r() code and stop the do-file at the failing line.

Next: Command Syntax and Getting Help