*! version 1.0 march2015

Title

replacestrvar -
Replaces in one or more variables each instance of a list of texts
by the desired text

Syntax

replacestrvar varlist , replist(string) [options]

replist(string)
String or list of strings to be sought within each observation of varlist.
In the case of a list of strings, the replacement is performed
for each instance of each string in the list.
This option is required.

withstr(string)
String to replace with within each observation of varlist
where the string sought was found.
Default is "", which erases each instance of the string sought.

generate(newvarname)
Generates a new variable for each variable in varlist where the string replacement occurs,
leaving the selected varlist unmodified.
If omitted, the default is to replace on the selected varlist.
If varlist contains more than one variable,
newvarname is used as prefix for each new variable.

word
Replaces words instead of strings.
In other terms, if replacing "is" by "X" in "This is this" will return "This X this" when
using word and "ThX X thX" by default.

quietly
Prevents Stata from providing detailed output on the results of each string replacements,
which is the default.

Description

replacestrvar replaces each instance of a string (or list of strings)
within a variable (or list of variables) with a given string.

It is meant to assist the cleaning of string variables,
making it a complementary tool to matchit and freqindex
(if installed).

Please, note that replacestrvar is case-sensitive and takes into account
all other symbols (as far as Stata does).

Examples:

Removes puntuation marks from mytxtvar

. replacestrvar mytxtvar, r(". , ; : ! ?")

Removes numbers from mytxtvar

. replacestrvar mytxtvar, r("0 1 2 3 4 5 6 7 8 9")

Replaces accentuated or similar characters

. replacestrvar mytxtvar, r("á ā ä â ã") w("a")

. replacestrvar mytxtvar, r("é č ę ë") w("e")

. replacestrvar mytxtvar, r("í ė î ï") w("i")

. replacestrvar mytxtvar, r("ó ō õ ô ö ø") w("o")

. replacestrvar mytxtvar, r("ú ų û ü") w("u")

. replacestrvar mytxtvar, r("į"') w("c")

. replacestrvar mytxtvar, r("ß"') w("s")

. replacestrvar mytxtvar, r("ý"') w("y")

. replacestrvar mytxtvar, r("æ"') w("ae")

. replacestrvar mytxtvar, r(""') w("oe")

. replacestrvar mytxtvar, r("Ð"') w("D")

. replacestrvar mytxtvar, r("ņ"') w("n")

Removes a list characters

. local mylist / < > + = - _ [ ] { } | \ ' @ # $ % ^ & "*" ( ) ///

Ą ē ģ Ī Ž ž ― ū Ū þ Ŧ ŧ Ž ī ķ ð Đ ĩ ŋ  ą  â

. replacestrvar mytxtvar, r(`"`mylist'"')