*! name change NJC 3 February 2003
*! 2.0.0 NJC 25 August 1999
* 1.0.2  NJC 16 March 1998
* 1.0.1  NJC 3 April 1996
* 1.0.0  NJC 19 August 1994
program define skewplot6
	version 6.0
    	syntax varlist [if] [in] /*
*/ [ , BY(varname) MONO MISSing L1title(str) L2title(str) Gap(int 4) * SKEW ]
	tokenize `varlist'
	local nvars : word count `varlist'

	tempvar touse
	mark `touse' `if' `in'
	if "`missing'" == "" {
        	if "`by'" != "" { markout `touse' `by', strok }
        }
        else {
        	if "`by'" == "" { di in bl "missing only applies with by( )" }
    	}

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

	if "`by'" != "" {
        	if "`in'" != "" {
	        	di in red "in may not be combined with by"
           	        exit 190
	        }
        	if `nvars' > 1 {
                	di in r "too many variables specified"
                	exit 103
        	}
        }

        if `nvars' > 1 {
        	preserve
        	tempvar data
        	local i 1
        	while `i' <= `nvars' {
                	local stargs "`stargs' ``i'' `touse'"
			local lbls "`lbls' ``i''"
                        local lbl`i' "``i''"
		        local i = `i' + 1
        	}
        	stack `stargs', into(`data' `touse') clear
	        local i 1
        	while `i' <= `nvars' {
                	label def _stack `i' "`lbl`i''", modify
            		local i = `i' + 1
        	}
        	label val _stack _stack
	        local varlist "`data'"
        	label var `varlist' "`lbls'"
	        local by "_stack"
        }

	markout `touse' `varlist'

        tempvar mid spread median
	quietly {
		sort `touse' `by' `varlist'
		by `touse' `by' : gen `spread' =  /*
		*/ `varlist'[_N  + 1 - _n] - `varlist' if `touse'
                by `touse' `by' : gen `mid' =  /*
		*/ 0.5 * (`varlist'[_N + 1 - _n] + `varlist') if `touse'

		if "`skew'" != "" {
			#delimit ;
			by `touse' `by' : gen `median' =
		        cond(mod(_N, 2) == 0,
			(`varlist'[_N / 2] + `varlist'[(_N / 2) + 1]) / 2,
			`varlist'[(_N + 1) / 2]) ;
			#delimit cr
			replace `mid' = `mid' - `median'
			replace `spread' = `spread' / 2
			label var `mid' "Skewness"
		}
		else label var `mid' "Midsummary"

		label var `spread' "Spread"
		local w : variable label `varlist'
		if "`w'" == "" { local w "`varlist'" }
	}

	qui if "`by'" != "" & "`mono'" == "" {
        	tempvar group
                by `touse' `by' : gen byte `group' = _n == 1 if `touse'
                replace `group' = sum(`group')
                local max = `group'[_N]
                local bylab : value label `by'
                local i = 1
                count if !`touse'
                local j = 1 + r(N)
               	while `i' <= `max' {
	                tempvar mid`i'
               		gen `mid`i'' = `mid' if `group' == `i'
	                local byval = `by'[`j']
               		if "`bylab'" != "" {
				local byval : label `bylab' `byval'
			}
	                label var `mid`i'' "`byval'"
	                local midlist "`midlist' `mid`i''"
	                count if `group' == `i'
               		local j = `j' + r(N)
	                local i = `i' + 1
	        }
		local mid "`midlist'"
	}
	else if "`by'" != "" {
                tempvar bymin
                qui by `touse' `by' : gen `bymin' = -`spread'[1]
                sort `bymin' `by' `spread'
        }

	if "`l1title'" == "" { local l1title "`w'" }
	if "`l2title'" == "" {
		local l2title = cond("`skew'" == "", "Midsummary", "Skewness")
	}

	graph `mid' `spread' if `touse' & `spread' >= 0, /*
	*/ `options' l1("`l1title'") l2("`l2title'") gap(`gap')
end