duplicates: report, tag, and inspect repeated records

Measure duplicate groups and expose their rows before making any irreversible decision about which records to keep.

Tested 2026-08-06 with the current stats.camp development build · View the do-file on GitHub

Open this example View supported commands

Independent software. Not affiliated with, sponsored by, or endorsed by StataCorp LLC.

Complete example

The example deliberately copies records with IDs 1 and 2 so the duplicate checks have a visible result.

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

Output from stats.camp

Duplicates in terms of id

groups with duplicates: 2
observations in duplicate groups: 4
surplus observations: 2

(2) duplicate groups tagged in variable duplicate_count

+-------------------------------------+
|  #    id    math   duplicate_count  |
|-------------------------------------|
|   1     1     45                 1  |
|   2     1     45                 1  |
|   3     2     51                 1  |
|   4     2     51                 1  |
+-------------------------------------+

Duplicates depend on the key

The report treats rows as duplicates in terms of id, so two groups contain four observations and two are surplus copies. A different varlist can produce a different answer: records can repeat an identifier while differing on another field.

The tag is zero for unique rows and reports the number of extra copies for rows in a duplicate group. It supports further inspection without deleting anything.

Common subcommands and options

See the current duplicates entry and Stata's duplicates manual for an external reference.

Related guides

Run this exact example

Clone the example repository in stats.camp and open the exact do-file shown on this page. No stats.camp account is required.

Open this example