**Setgen**
capture program drop setgen
version 9.0
program define setgen
	gettoken togen 0 : 0, parse(" =(")
	gettoken eqsign 0 : 0, parse(" =(")
		if `"`eqsign'"' != "=" {
  		error 198
  		}
	confirm new variable `togen'
  	gettoken gentyp 0 : 0, parse(" =(")
	gettoken oldset 0 : 0, parse(" ,") match(par)
	if `"`par'"' != "(" {
	  exit 198
  	}
syntax [if] [in] [, *]
`gentyp' `oldset', `options'
capture rename `gentyp'`oldset' `togen'
end

capture program drop stdrank
program define stdrank
version 8.0
syntax varlist [, options ]
local ivs `varlist'

qui foreach v of local ivs {
	egen stdrank`v' = rank(`v')
	su stdrank`v'
	replace stdrank`v' = (stdrank`v' - r(min))/(r(max)-r(min))
}
end

capture program drop drect
program define drect
version 8.0
syntax varlist, anchors(numlist min=3 max=3)
local ivs `varlist'

local lb: word 1 of `anchors'
local co: word 2 of `anchors'
local ub: word 3 of `anchors'

if `lb'>`co' | `co'>`ub' | `lb'>`ub' {
	di as err "Error: Anchor Points Not Entered in Ascending Order"
	exit
}

qui foreach v of local ivs {
   gen drect`v' = .
   replace drect`v' = (`v'-`co')*((3/(`ub'-`co'))) if `v'>=`co'
   replace drect`v' = (`v'-`co')*((-3*(`lb'-`co'))) if `v'<`co'
   replace drect`v' = exp(drect`v')/(1+exp(drect`v'))
}
end

capture program drop ndrect
program define ndrect
version 8.0
syntax varlist, grpdvar(varname)
local v `varlist'

qui tab `v'
if `r(r)'<10 {
	di as err "Error: Original Variable is Not Continuous"
	exit
	}

fracpoly glm `grpdvar' `v', family(bin) link(logit)
predict ndrect`v'

end


capture program drop crisp
program define crisp, rclass
syntax varlist [if] [, cutpt(numlist max=1)]
local v `varlist'

foreach var of local v {
	qui gen crisp`var' = `var'
	if "`cutpt'" == "" {
		qui su `var', det
		local med `r(p50)'
		if `r(p50)'< `r(max)' {
			qui recode crisp`var' (min/`r(p50)'=0)
			qui recode crisp`var' (.0001/max=1)
		}
		else {
			qui recode crisp`var' (`r(p50)'/max=1)
			qui recode crisp`var' (min/.9999=0)
		}
	di in yell %-10s "`var' " in green "Crispy Split At `med'"
	}
	if "`cutpt'" != "" {
		qui su crisp`var'
		qui recode crisp`var' (min/`cutpt'=0) (nonmis=1)
	}

}

end