program define testallwith, rclass
*!	Version 1.1 9Apr2020 by Mead Over, CGD. Wrapper for Stata's -testparm.ado- version 3.2.5  24jan2015
*!  Adds to Stata's -testparm- the option -joint()- enabling testparm
*!  to jointly test all coefficients within -varlist- which contain one or more
*!  of the variables in the option -joint(varlist)-
	version 9.2

	local caller : display string(_caller())
	local version "version `caller':"

	syntax varlist(fv ts) [, EQuation(passthru) DF(numlist max=1 >0)  SMALL *  ///
		Joint(varlist fv ts) ]  //  Only test coefficients containing at least one of the variables in this list

	if `"`small'"'!= "" {
			if `"`e(cmd)'"'!="mixed" | `"`e(dfmethod)'"'=="" {
					di as err "{p}option {bf:small} is allowed only if " ///
					  "option {bf:dfmethod()} was used with the " ///
					  "{bf:mixed} command during estimation{p_end}"
					exit 198
			}
	}

	if `"`small'"'!="" & "`df'" != "" {
			di as err "{p}option {bf:small} may not be combined with " ///
					"option {bf:df()}"
	}

	if `"`equation'"' != "" {
	    local eqtoparse = substr(`"`equation'"',strpos(`"`equation'"',"(")+1,strpos(`"`equation'"',")")-strpos(`"`equation'"',"(")-1 )
	    tokenize `"`eqtoparse'"' , parse(" ()")
			local eqopt eq(`1')
	}
	else if "`e(cmd)'" == "mlogit" {
			if e(ibaseout) == 1 {
					local eqopt eq(#2)
			}
	}

	if `"`joint'"'~="" {
		_ms_extract_varlist `varlist', `eqopt' noomit nofatal
		local expanded `"`r(varlist)'"'

		//  Added in version 1.1
		local tmpjoint `joint'
		local joint
		foreach j in `tmpjoint' {
			local j_wo_fv_or_ts = substr("`j'",strpos("`j'",".")+1,.)
			local joint `joint' `j_wo_fv_or_ts'
		}

		foreach fv_or_ts in  `expanded'  {
			foreach j in `joint' {
				if strpos(`"`fv_or_ts'"',"`j'") {
					local jntlist `jntlist'  `fv_or_ts'
				}
			}
		}
		local jntlist : list uniq jntlist

		if "`jntlist'"=="" {
			di as err "Among the coefficients specified by the varlist:"  ///
				_n "     -`varlist'-,"  ///
				_n "none includes the variable(s)"  ///
				_n "     -`joint'-"  ///
				_n "specified by the option -joint(jvarlist)-."
			exit 198
		}

		testparm `jntlist'  , `equation'  `df' `small' `options'
	}
	else {
		testparm `varlist'  , `equation'  `df' `small' `options'

	}
	return add
	return local jvarlist `joint'
	return local varlist `expanded'
	return local allwith `jntlist'

end
*	Version history
*	1.0 6Apr2020
*	1.1 9Apr2020 Allow the joint variables to include factor and time-series variables
*		but strip them of their operators of the form xxx. (i.e. i., c., L2., D., etc.)