*! version 1.0.0  02/02/93 extrname <_crcexn7> utility; STB-13: dm13
program define _crcexn7 /* px lname fname mname ttl cttl */
	version 3.0
	local px    "`1'"
	local lname "`2'"
	local fname "`3'"
	local mname "`4'"
	local ttl   "`5'"
	local cttl  "`6'"

	tempvar bad new frst
	quietly {
		gen byte `bad' = `fname'=="`ttl'" & `px'==""
		capture assert `bad'==0
		if _rc==0 { exit }

		local d=length("`cttl'")
		minlen `d' `px'
		replace `px'="`cttl'" if `bad'

		local type : type `mname'
		minlen `type' `fname'
		replace `fname'=`mname' if `bad'
		replace `mname'="" if `bad'
		replace `lname'=trim(`lname')

		compress `fname' `mname' `lname'
		replace `bad'=`bad' & index(`lname'," ")
		capture assert `bad'==0
		if _rc==0 { exit }

		local type : type `lname'
		gen `type' `new'=`lname' if `bad'
		splitstr `frst' `new'
		replace `lname'=`new' if `bad'
		drop `new'

		local type : type `frst'
		minlen `type' `mname'
		replace `mname'=`frst' if `bad'
		drop `frst'
		compress `lname' `mname'
	}
end