Command reference
duplicates — inspect, tag, and remove repeated records
Identify observations that share the same values in a chosen set of variables. Report duplicates before deciding whether repeated records are errors or valid observations.
Syntax
duplicates report [varlist] [if condition] [in first/last]
duplicates list [varlist] [, options]
duplicates examples [varlist] [, options]
duplicates tag [varlist], generate(newvar)
duplicates drop [varlist] [, force]
varlist defines what counts as a duplicate. With id, observations sharing an ID match even if other values differ. Without a variable list, all variables define the match. The subcommands also accept if and in to examine a subset.
Subcommands and display options
Example
The sample deliberately makes a second copy of students 1 and 2. It reports repeated IDs, tags and lists them, removes the surplus copies, and reports again.
clear
import delimited "data/student_scores.csv"
expand 2 if id <= 2
duplicates report id
duplicates tag id, generate(duplicate_count)
list id math duplicate_count if duplicate_count > 0
duplicates drop id
duplicates report id
Sample data included. Opens in a new tab; click Run.
What to look for
After expansion, there are 26 observations. Two duplicate groups contain four observations in total, with two surplus observations. All four receive duplicate_count = 1. Dropping the surplus copies returns the dataset to 24 observations, and the final report finds no duplicated IDs.