*! soundex algorithm - vers 2.1 by M Blasnik 11oct2001
*! syntax: egen newvar=soundex(oldvar), [Length(int 4)]
program define _gsoundex
	version 7.0

	gettoken type 0 : 0
	gettoken g    0 : 0
	gettoken eqs  0 : 0

	syntax varlist(max=1) [if] [in], [Length(integer 4) BY(string)]

	if `"`by'"' != "" {
		_egennoby soundex() `"`by'"'
	}

marksample touse, strok
tempvar work res code vlen
quietly {
gen str1 `g'=substr(`varlist',1,1)
gen str1 `work'=""
gen str1 `code'=""
gen byte `vlen'=length(`varlist')
sum `vlen', meanonly
local maxvlen=r(max)
drop `vlen'
local codestr "BFPV####CGJKQSXZDT######L#######MN######R#######AEIOUWHY"
forvalues i=2/`maxvlen' {
	replace `work'=upper(substr(`varlist',`i',1))
	replace `code'=string((int((index("`codestr'",`work')-1)/8)+1)*(index("`codestr'",`work')!=0))
	replace `g'=`g'+ `code' if `work'!="" & substr(`g',-1,1)!=`code' & `code'>"0" & `code'<"8"
	if `i'==2 {replace `g'=substr(`g',1,1) if `work'==substr(`g',1,1)}
}
replace `g'=subinstr(`g',"7","",.)
replace `g'=substr(`g',1,`length')
replace `g'=`g'+substr("0000000000",1,`length'-length(`g'))
replace `g'="" if !`touse'
compress `g'
}
end