*! version 1.0.1  02/02/93  STB-13: dm13 updated 21sep1999
* extrname varname, prefix(vn) first(vn) middle(vn) last(vn) suffix(vn)
*        affill(vn) odd(vn)
program define extrname
	version 3.0
	local varlist "req ex max(1)"
	local if "opt"
	local in "opt"
	local options /*
*/ "AFfil(string) ALL First(string) Last(string) Middle(string) Odd(string) Prefix(string)  Suffix(string)"
	parse "`*'"

	confirm string variable `varlist'

	if "`all'"!="" {
		local prefix prefix
		local first first
		local middle middle
		local last last
		local suffix suffix
		local affil affil
		local odd odd
	}

	capture confirm new var /*
		*/ `prefix' `first' `middle' `last' `suffix' `affil' `odd'
	if _rc==7 { error 198 }
	if _rc {
		confirm new var /*
		*/ `prefix' `first' `middle' `last' `suffix' `affil' `odd'
		/*NOTREACHED*/
	}

/*
	quietly {
		describe
		local cwidth=_result(3)
		local swidth=_result(6)
		local type : type `varlist'
		local vwidth=real(substr("`type'",4,.))
		if `cwidth'+1.25*`vwidth'>`swidth' | /*
		*/ `vwidth'*6>`swidth' {
			di in red "no room to add more variables due to width"
			exit 902
		}
		local cwidth
		local swidth
		local type
		local vwidth
	}
*/




	tempfile USER NEW
	tempvar id
	tempvar px f m l sx af od
	quietly {
		gen long `id'=_n
		compress `id'
		sort `id'

		local sfn "$S_FN"
		save `USER'
	}
	capture {
		if "`if'"!="" & "`in'"!="" {
			keep `if' `in'
		}
		keep `id' `varlist'
		replace `varlist'=trim(`varlist')
		compress `varlist'

		noi di in gr _n _col(41) "------ lengths ------" _n /*
			*/ _col(17) "defined" /*
			*/ _col(31) "blank" /*
			*/ _col(41) "shortest" /*
			*/ _col(55) "longest" _n /*
			*/ _dup(61) "-"
		noi _crcexnt Original `varlist'

		gen byte `od'=0
		gen str1 `px'=""
		gen str1  `f'=""
		gen str1  `m'=""
		gen str1  `l'=""
		gen str1 `sx'=""
		gen str1 `af'=""
		_crcexn1 `varlist' `px' `f' `m' `l' `sx' `af' `od'
		noisily {
			_crcexnt Prefix   `px'
			_crcexnt "First name" `f'
			_crcexnt "Middle name" `m'
			_crcexnt "Last name" `l'
			_crcexnt "Suffix" `sx'
			_crcexnt "Affil" `af'
		}
		drop `varlist'
		order `px' `f' `m' `l' `sx' `af'
		sort `id'
		save `NEW'
		use `USER'
		merge `id' using `NEW'
		drop _merge
		capture rename `px' `prefix'
		capture rename `f' `first'
		capture rename `m' `middle'
		capture rename `l' `last'
		capture rename `sx' `suffix'
		capture rename `af' `affil'
		capture rename `od' `odd'
	}
	local rc=_rc
	if _rc {
		quietly use `USER', clear
	}
	mac def S_FN "`sfn'"
	capture erase `USER'
	capture erase `NEW'
	error `rc'
end
exit

/*
         1         2         3         4         5         6
1234567890123456789012345678901234567890123456789012345678901234567890

	                                ------ lengths ------
                defined       blank     shortest      longest
-------------------------------------------------------------
Original
Prefix        123456789   123456789    123456789    123456789
First Name
Middle name
Last name
Suffix
Affil