*! version 1.0.1 PR 06jul2006.
program define nscore	/* Normal scores */
version 8
syntax newvarname [= /exp] [if] [in], [ BY(varlist) Freq(string) noTies ]
tempvar touse GRV
if "`freq'"!="" confirm new var `freq'
quietly {
	marksample touse, novarlist
	if "`exp'"!="" {
		gen double `GRV' = (`exp') if `touse'==1
	}
	else {	/* take ordering of scores from existing observations */
		gen long `GRV'=_n
	}
	markout `touse' `GRV' `by'
	replace `touse'=. if `touse'==0
	sort `touse' `by' `GRV'
	by `touse' `by': gen double `varlist' = invnorm((_n-.375)/(_N+.25)) if `touse'==1
	if "`ties'"!="noties" {
/*
	Replace Normal scores by their means in tie groups
*/
		by `touse' `by' `GRV': replace `varlist'=sum(`varlist') if `touse'==1
		by `touse' `by' `GRV': replace `varlist'=`varlist'[_N]/_N if `touse'==1
		if "`freq'"!="" by `touse' `by' `GRV': gen long `freq'=_N if `touse'==1
	}
}
end