Skip to content
Tutorials Center
My learning

Topics

Lesson 9 of 36 ยท Interface and Data

Missing Values and Labels in Stata

How Stata stores missing values (., .a to .z), why missing sorts as infinity in comparisons, and how to use variable labels, value labels, notes and label languages.

Two details make a Stata dataset trustworthy: knowing exactly how missing values behave, and having labels that explain what the numbers mean. Missing-value rules cause more silent bugs than anything else in Stata. Labels turn 1 and 2 into "male" and "female" in tables and graphs. This lesson covers both.

Missing numeric values

Stata has 27 numeric missing codes: the system missing value . and the extended missing values .a, .b, ..., .z. They let you record why a value is absent, for example:

Code Could mean
. Unknown for no recorded reason
.a Refused to answer
.b Does not know
.c Not applicable (question skipped)
STATA
replace income = .a if income == -9      // recode a survey code for "refused"
replace income = .  if income == 999999  // an impossible value
mvdecode income, mv(-9 = .a \ -8 = .b)   // convert many codes at once
mvencode income, mv(.a = -9)             // the reverse

Always convert survey codes such as -9, 99 or 999 into missing values, otherwise averages will include them as real data. Strings have no .: a missing string is the empty string "".

Missing values and comparisons: the big trap

In comparisons, a numeric missing value is larger than every number. The order is: all numbers < . < .a < ... < .z. So:

STATA
list if age > 60            // also lists observations where age is missing!
count if income > 50000     // includes missing incomes
generate old = (age > 60)   // missing age gets old = 1, which is WRONG

The fix is to exclude missing explicitly:

STATA
generate old = (age > 60) if !missing(age)   // old stays missing when age is missing
count if income > 50000 & !missing(income)

missing(x) is true for . and for .a to .z. Using x != . only catches the plain ., so prefer !missing(x). Also, == and != work normally on missing codes, but > and < do not do what you expect.

Other missing-value rules:

  • Arithmetic with a missing value gives missing: 3 + . = ..
  • generate z = x + y is missing whenever either is missing; egen functions such as rowmean() and rowtotal() skip missing values.
  • Most analysis commands (summarize, regress) automatically drop observations with missing values in the variables used (casewise deletion). Always check how many observations were used: look at N or the Number of obs in the output.
  • sum() inside generate (the running-sum function) treats missing as zero. This is different from egen total().
STATA
misstable summarize          // missing-value patterns of all variables
misstable patterns income age educ  // which combinations are missing together
count if missing(income)

Labels: three layers

1. The dataset label names the whole file.

STATA
label data "Household survey 2024, cleaned"

2. Variable labels describe each variable (up to 80 characters), and appear in output and graphs.

STATA
label variable income "Monthly household income (USD)"

3. Value labels attach text to the numbers in a variable. You define the label set first, then assign it to variables.

STATA
label define sex_lbl 1 "Male" 2 "Female"
label values sex sex_lbl
tabulate sex                 // shows Male and Female instead of 1 and 2
tabulate sex, nolabel        // shows the numbers

One label set can be reused for many variables: label values q1 q2 q3 agree_lbl. The number stays the underlying value; only its display changes, so if sex == 2 still works. If a label set already exists, add values with label define sex_lbl 3 "Other", add or replace them with modify.

STATA
label list sex_lbl           // show a label set
label dir                    // list all label sets in memory
numlabel sex_lbl, add        // show "2. Female" so number and text are together
decode sex, generate(sex_str) // make a string variable from the labels

Value labels are stored in the .dta file, so they travel with the data. Check them after merging datasets: a label set with the same name but different content can cause surprises.

Notes and characteristics

Notes are free-text memos kept inside the dataset:

STATA
note: Cleaned on 2024-05-01; duplicates removed.
note income: Top-coded at 200,000.
notes                         // display all notes
notes drop _dta in 1          // remove the first dataset note

Notes are a lightweight place for provenance. Use them for decisions that a person opening the dataset later would otherwise not know.

A good routine: label as you go

Add labels in the same do-file that creates the variable, so the documentation cannot drift:

STATA
generate byte female = (sex == 2) if !missing(sex)
label variable female "1 if female"
label define yesno 0 "No" 1 "Yes"
label values female yesno

For a large dataset, a block of label variable lines, one per variable, near the top of the cleaning do-file makes a data dictionary that can be read at a glance.

Stand-ins: missing values and value labels

The Python models below use None as "missing". The first demonstrates the comparison trap by giving missing a value larger than any number, as Stata does. The second models a value label set.

Python Stand-in: missing is bigger than every number

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.

Python Stand-in: label define / label values / tabulate

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. In count if age > 60, how are observations with missing age treated?

    Numeric missing values sort above all numbers, so you must add & !missing(age).
  2. 2. Which is the best test for a missing value?

    missing(x) is true for . and for .a to .z; x == . catches only the plain dot.
  3. 3. What do value labels change?

    Labels are display text; the underlying values are unchanged.
  4. 4. What is the correct order of steps for value labels?

    Define the label set first, then attach it to variables.

Summary

  • Stata has 27 numeric missing codes (., .a to .z) that sort above every number; always test with !missing(x).
  • Convert survey codes like -9 and 999 to missing with replace or mvdecode.
  • Variable labels describe columns; value labels give text to numbers; notes record decisions.
  • Define a label set once, then attach it with label values; underlying numbers stay the same.
  • misstable summarize and counting observations used show how much data a model really uses.

Next: Generating and Replacing Variables