*! version 1.0.1  27jul2004
program clogit_score, sortpreserve
	version 8.2

	if "`e(cmd)'" != "clogit" {
		error 301
	}
	if _by() {
		error 190
	}

	syntax anything(name=scorevars id="new varlist") [if] [in] [,  ///
		noWeight noOFFset]

	tempname b lnf g
	matrix `b' = e(b)
	local k = colsof(`b')

	local nsc : word count `scorevars'
	if `nsc' == 1 & substr("`scorevars'",-1,1) == "*" {
		local stub = substr("`scorevars'",1,length("`scorevars'")-1)
		local scorevars
		forvalues i = 1/`k' {
			local scorevars	`scorevars' `stub'`i'
		}
		local nsc `k'
	}
	if `nsc' != `k' {
		di as err "must specify `k' new score variables"
	 	exit 198
	}
	confirm new var `scorevars'

	if `"`if'"' != "" | `"`in'"' != "" {
		marksample touse
	}
	else {
		tempvar touse
		qui gen byte `touse' = e(sample)
	}

	tempvar obs obs2 group
	qui egen long `group' = group(`e(group)') if `touse'
	qui sum `group', meanonly
	local ng = r(max)

	qui gen long `obs2' = _n
	sort `group' `obs2'
	qui by `group': gen `obs' = _n if `touse'
	gsort -`touse' `group' `obs'

	if "`weight'" == "" & "`e(wtype)'" != "" {
		local wgt `"[`e(wtype)'`e(wexp)']"'
	}
	if "`offset'" == "" & "`e(offset)'" != "" {
		local offopt `"offset(`e(offset)')"'
	}

	foreach v of local scorevars {
		qui gen double `v' = .
	}

	local y `e(depvar)'
	local xs : colnames `b'

	local warn 0
	forvalues i = 1/`ng' {
		cap _clogit_lf `y' `xs' `wgt' if `group'==`i', gr(`group') ///
			beta(`b') grad(`g') lnf(`lnf') `offopt'
		if !_rc {
			forvalues j = 1/`k' {
				local var : word `j' of `scorevars'
				qui replace `var' = `g'[1,`j'] ///
					if `group'==`i' & `obs' == 1
			}
		}
		else {
			local warn 1
		}
	}
	forvalues j = 1/`k' {
		local var : word `j' of `scorevars'
		local name : word `j' of `xs'
		label var `var' "clogit score for `name'"
	}
	if `warn' {
		di as txt "note: score calculation failed for one or more " _c
		di as txt "groups"
	}
end