*! NJC 1.0.1 30 October 2002
*! NJC 1.0.0 26 October 2002
program def matrixof 
	version 7 
	gettoken cmd 0 : 0 
	syntax [varlist] [if] [in] [aweight fweight iweight pweight] /* 
	*/ [ , * Eclass(str) Rclass(str) Global(str) MATname(str) LISTWISE /* 
	*/ cols(varlist)/*
	*/ SAving(str asis) margin(str) TItle(str asis) /* 
	*/ noBlank noHAlf noHeader noNames Format(str)  ] 
	
	local nopts = /* 
	*/ ("`eclass'" != "") + ("`rclass'" != "") + ("`global'" != "")
	if `nopts' >= 2 {
		di as err "must specify one of eclass(), rclass() or global()"
		exit 198 
	}
	else if `nopts' == 0 { 
		local graph "graph" 
	}	

	if "`graph'" != "" { 
		if "`matname'" != "" { 
			di as err "matname() incompatible with graph"
			exit 198 
		}	
	}
	else { 
		local nargs : word count `eclass' `rclass' `global' 
		if `nargs' != 1 {
			foreach o in eclass rclass global { 
				if "``o''" != "" { 
					local which "`o'" 
				}
			} 	
			di as err "specify one result in `which'()"
			exit 198 
		} 
	} 	

	if "`listwise'" != "" { 
		marksample touse, novarlist 
	} 
	else { 
		marksample touse
		if "`cols'" != "" { 
			markout `touse' `cols' 
		}
	} 	

	qui count if `touse' 
	if r(N) == 0 { 
		di as err "no observations" 
		exit 2000 
	}

	local rows "`varlist'" 
	if "`cols'" == "" { 
		local cols "`varlist'" 
	} 	

	if "`graph'" != "" { 
		local onoff : set graphics
		if `"`title'"' != "" { 
			local t "title(`"`title'"')"
		}	

		noisily capture { 
			set graphics off 
			
			foreach v of local rows { 
				foreach w of local cols { 
					tempfile g 
					`cmd' `v' `w' [`weight' `exp'] /* 
					*/ if `touse', `options' `t' saving(`"`g'"') 
					local G `"`G' "`g'""' 
				}
			} 
			
			set graphics on 
			
			if "`margin'" != "" { 
				local m "margin(`margin')"
			}
			if `"`saving'"' != "" { 
				local s "saving(`"`saving'"')" 
			}
			
			gra using `G', `m' `s'
		}
		
		local rc = max(_rc, 0) 

		if "`onoff'" == "on" { 
			set graphics on 
		}
		else set graphics off

		if `rc' { 
			error `rc' 
		}	
	}
	else { 
		local nr : word count `rows' 
		local nc : word count `cols' 

		if "`matname'" == "" { 
			tempname matname 
			local header "noheader" 
		} 
		
		mat `matname' = J(`nr',`nc',1e309) 
		if `"`options'"' != ""	{ 
			local options `", `options'"' 
		} 	
		
		tokenize `rows' 
		
		if "`global'" != "" { 
			forval i = 1 / `nr' { 
				forval j = 1 / `nc' { 
					local vj : word `j' of `cols' 
					qui `cmd' ``i'' `vj' [`weight' `exp'] /* 
					*/ if `touse' `options'  
					capture mat `matname'[`i',`j'] = $`global'
				}
			}
		} 	
		else { 
			local t = cond("`eclass'" != "", "e", "r") 

			forval i = 1 / `nr' { 
				forval j = 1 / `nc' { 
					local vj : word `j' of `cols' 
					qui `cmd' ``i'' `vj' [`weight' `exp'] /*
					*/ if `touse' `options'  
					capture mat `matname'[`i',`j'] = /* 
					*/ `t'(`eclass'`rclass')
				}
			} 
		}	

		mat rownames `matname' = `rows'
		mat colnames `matname' = `cols'
		if "`format'" != "" { 
			local format "format(`format')" 
		} 
		if `"`title'"' != "" { 
			local title `"title(`title')"' 
		} 	
			
		matrix li `matname', /* 
		*/ `blank' `header' `names' `half' `format' `title' 
	}
end