program define pwqscope, rclass
*!	program pwqscope (or "pairwise" qscope)	version 1.02 10Feb2017 by Mead Over

*!	This program is a wrapper for the user written program qscope.ado

	version 11.1
	syntax [if] [in] , OUTputs(varlist) [row(real 1)  *]

	di as txt _n "Computes the economies of scope between every pair of outputs of the output list:" ///
					as res "`outputs'"

	local rawoutputs `outputs'
	tempname pwscope pwscopep diagmsng
	local rowno = 1
	local padwith 0
	local restofout `rawoutputs'
	foreach rawout1 in `rawoutputs' {
		tempname pwscope`rowno' pwscopep`rowno'
		mat `pwscope`rowno'' = (`padwith')
		mat `pwscopep`rowno'' = (`padwith')
		local restofout : list restofout - rawout1
		foreach rawout2 in `restofout' {

			qscope `if' `in', outputs(`rawout1' `rawout2')  `options'

			mat `pwscope`rowno'' = (`pwscope`rowno'', el(r(qscope),`row',4))
			mat `pwscopep`rowno'' = (`pwscopep`rowno'', el(r(qscope),`row',7))

		}
		mat `diagmsng' = (nullmat(`diagmsng'),.)
		mat `pwscope' = (nullmat(`pwscope') \ `pwscope`rowno'')
		mat `pwscopep' = (nullmat(`pwscopep') \ `pwscopep`rowno'')
		local padwith `padwith' , 0
		local rowno = `rowno' + 1
	}

	local eps = el(r(qscope),`row',1)

	mat def `pwscope' = `pwscope' + `pwscope'' + diag(`diagmsng')
		matrix rownames `pwscope' = `rawoutputs'
		matrix colnames `pwscope' = `rawoutputs'
	matrix list `pwscope', format(%9.3f) title("Pairwise economies of scope at epsilon = `eps'")
	mat def `pwscopep' = `pwscopep' + `pwscopep''  + diag(`diagmsng')
		matrix rownames `pwscopep' = `rawoutputs'
		matrix colnames `pwscopep' = `rawoutputs'
	matrix list `pwscopep', format(%9.3f) title("p-values of pairwise economies of scopeat epsilon = `eps'")

	return add
	return local firstepsilon `eps'
	return matrix pwscope `pwscope'
	return matrix pwscopep `pwscopep'

end
*	1.00 10Mar2017
*	1.01 11Mar2017	Implement -if-, -in-, -return add-
*	1.02 10Apr2017	Change name of added local macro to "firstepsilon"