*! simuladip v1.0 JavierMarquez 22dic2009
program define simuladip
	version 10
	if e(cmd) != "estsimp sureg" {
		di as err "-simuladip- requires -estimadip- before execution"
		error 301
	}
	local prog setx simqi
	foreach p of local prog {
		capt which `p'
		if _rc {
		    di as error "-{bf:estimadip}- requires {it:Clarify} ; " _c
		    di as error `"click {stata "net install clarify,from(http://gking.harvard.edu/clarify)" :here}"' _c
		    di as error " for installation or type " _c
		    di as error `"{stata "net from http://gking.harvard.edu/clarify/"}"'
		    error 499
		}
    }
	syntax using/, GENerate(namelist min=`e(k_eq)' max=`e(k_eq)') 			///
		[ GENBase(name) set(string) ev replace ]
	foreach name of local generate {
		capture confirm variable `name'
		if _rc == 0 {
			di as error "variable `name' already exists"
			error 110
		}
	}
	capture confirm variable `genbase'
	if _rc == 0 {
		di as error "variable `genbase' already exists"
		error 110
	}
	tokenize `"`set'"'
	local j = 0
	local rhsvars `e(rhsvars)'
	while "``++j''" != "" {
		capture confirm variable ``j''
		if _rc == 0 {
			local checkindepvar : list rhsvars & `j'
			if "`checkindepvar'" == "" {
				di as error "variable ``j'' is not an {it:indepvar}" _c
				di as error " in -estimadip-"
				error 198
			}
		}
	}
	preserve
	tokenize `e(if)' `e(in)'
	local j = 0
	while "``++j''" != "" {
		capture confirm variable ``j''
		if _rc == 0 {
			local myifin `myifin' ``j''
		}
	}
	keep `e(depvar)' `e(rhsvars)' `e(allsims)' `myifin'
	if "`set'" != "" {
		mytknz `"`set'"', s(rule) parse(,) nochar
		forvalues r = 1/`s(items)' {
			quietly recode `rule`r''
		}
	}
	tempvar mytouse tempo
	mark `mytouse' `e(if)' `e(in)'
	markout `mytouse' `e(rhsvars)' `e(depvar)'
	qui gen `tempo' = _n if `mytouse' == 1
	qui levelsof `tempo', local(mydto)
	di
	di "{txt}{hline 78}"
	di as text "Executing {bf:simuladip}"
	di "{txt}{hline 78}"
	di
	di as text "Number of districts{col 24}=  " as result e(N)
	di as text "Number of simulations{col 24}=  " as result e(sims)
	if "ev" == "`ev'" {
			local typeval ev
			di as text "Quantities{col 24}=  " as result "Expected values"
	}
	else {
			local typeval pv
			di as text "Quantities{col 24}=  " as result "Predicted values"
	}
	di
	di as text "Progress:"
	di
	local i = 0
	foreach dto of local mydto {
		di as text "." _continue
		setx [`dto']
		local genlist
		foreach name of local generate {
			local genlist `genlist' `name'`dto'
		}
		quietly simqi, gen`typeval'(`genlist') tfunc(logiti)
		local progress = round((`++i'/`e(N)')*100,.0001)
		capture confirm integer number `progress'
		if !_rc {
			di as text `progress' "%" _continue
		}
	}
	foreach name of local generate {
		local keeplist `keeplist' `name'*
	}
	keep `keeplist'
	generate _IDsim = _n
	di
	di
	di as text "Saving file..."
	qui reshape long `generate', i(_IDsim) j(_IDdto)
	qui sum
	qui keep in 1/`r(N)'
	if "`genbase'" == "" {
		local genbase party_J
	}
	qui generate `genbase' = 1
	foreach name of local generate {
		qui replace `genbase' = `genbase' - `name'
	}
	label variable _IDsim "Simulation ID"
	label variable _IDdto "District ID"
	label variable `genbase' "Party_J"
	if "`replace'" == "" {
		save `using'
	}
	else {
		save `using', replace
	}
	di
	di as text "{bf:simuladip} saved file " as result "`using'" _c
	di as result ".dta " as text "in path " as result c(pwd)
	restore
end

*! mytknz v1.0 JavierMarquez 22dic2009
//This is a modified version of tknz (equalok option in syntax)
program mytknz, sclass
   version 8
   syntax anything(name=list everything id="list to parse required" equalok) 	///
   [, Stub(str) noCHAR Parse(str) ]
   if "`parse'" == "" {
       local parse " "
       }
   tokenize `list' , parse(`parse')
   local i = 0
   local j = 0
   while "``++j''" != "" {
       c_local `stub'`++i' `"``j''"'
       if "`char'" == "nochar" & `"`parse'"' != " " {
           local ++j
           }
       }
	sreturn local items = `i'
end