*! version 1.2 09dec2017 Michael D Barker Felix Pöge
help strdist

Title

strdist -- Calculate the Levenshtein distance, or edit distance, between strings.

Syntax

strdist
{1|"string1"}
{2|"string2"}
[if] [in]
[, generate(newvar) maxdist(integer)]

Description

strdist calculates the distance between strings and/or string
variables using the Levenshtein distance metric. Levenshtein distance, or edit
distance, is the smallest number of edits required to make one string
match a second string. An edit may be an insertion, deletion, or
substitution of any single letter.

strdist accepts two arguments, which may be string variables or
string scalars in any combination. String scalars must be enclosed in quotes.

Edit distances are returned in a scalar or a new variable, depending on
the type of arguments supplied. If the arguments contain one or two string
variables, edit distances are returned in a new variable with default
name strdist. If both arguments are string scalars, edit distance
is returned in r(d).

For a version supporting unicode for Stata 14 and above, see ustrdist.

Options

Main

generate(newvar) Create a new variable named newvar containing
edit distance(s). If the arguments include a string variable without
the generate() option, a new variable will be created with
default name strdist.

maxdist(integer) Calculate string distances only up to an upper bound.
If the string distance exceeds that bound, the result is set
to missing (.). Only values of 1 or higher are valid upper bounds.

Examples

. strdist "cat" "hat"

. sysuse census

. strdist state "west virginia", gen(wvdist)

Saved results

strdist saves the following in r():

Scalars

r(d) edit distance if arguments are both string scalars

Macros

r(strdist) name of new edit distance variable if created

Author

Michael Barker

Georgetown University

mdb96@georgetown.edu

Felix Pöge

Max Planck Institute for Innovation and Competition

felix.poege@ip.mpg.de

We thank Sergio Correia for his suggestions on how to improve strdist's performance.

Also see

soundex,
strgroup,
ustrdist.