*! NJC 2.0.0 12 Nov 2003
* developed from quantil2
* NJC 1.3.1 21 February 2002 ranks option  (SJ4-1: gr42_2)
* NJC 1.3.0 22 February 2001 (STB-61: gr42.1)
* NJC 1.2.0 10 August 1999
* NJC 1.1.0 24 March 1999
* NJC 1.0.1 17 March 1998
* NJC 1.0.0 15 March 1998
program qplot, sort
	version 8.0

	// plot type
	gettoken plottype 0 : 0
	local plotlist "area bar connected dot dropline line scatter spike"
	if !`: list plottype in plotlist' {
		di ///
		"{p}{txt}syntax is {inp:qplot} {it:plottype varlist} " ///
		"... e.g. {inp: qplot scatter mpg} ...{p_end}"
		exit 198
	}

	syntax varlist [if] [in] [, a(real 0.5) by(varname)  ///
	sort MISSing REVerse RANKs TRSCale(str asis) MSymbol(str asis) ///
	YTItle(str) XTItle(str) PLOT(str asis) * ]
	tokenize `varlist'
	local nvars : word count `varlist'

	if "`trscale'" != "" {
		if !index("`trscale'","@") {
			di as err "trscale() does not contain @"
			exit 198
		}
	}

	if "`by'" != "" & `nvars' > 1 {
		 di as err "too many variables specified"
		 exit 103
	}

	marksample touse
	if "`missing'" == "" {
	 	if "`by'" != "" markout `touse' `by', strok
	}
	else if "`by'" == "" di as txt "missing only applies with by( )"

	qui if "`by'" == "" { /* no `by' option */
		 tempvar pp order
		 if "`ranks'" != "" {
		 	egen `pp' = rank(`1') if `touse', unique
		 	label var `pp' "rank"
		 }
		 else egen `pp' = mypp(`1') if `touse', a(`a')
		 sort `touse' `pp'
		 gen `order' = _n

		 forval i = 1 / `nvars' {
			 tempvar y`i'
			 sort `touse' ``i''
			 gen `y`i'' = ``i''[`order']
			 local lbl : variable label ``i''
			 if `"`lbl'"' != "" label var `y`i'' `"`lbl'"'
			 else label var `y`i'' "``i''"
			 local ylist "`ylist'`y`i'' "
			 local Ylist "`Ylist'``i'' "
		 }

		 if `"`ytitle'"' == "" {
			if `nvars' == 1 {
				local ytitle : variable label `varlist'
				if `"`ytitle'"' == "" local ytitle "`varlist'"
				local ytitle `"quantiles of `ytitle'"'
			 }
			 else local ytitle "quantiles of `Ylist'"
		 }
	 }

	else qui { /* by( ) */
		 tempvar pp group
		 if "`ranks'" != "" {
		 	egen `pp' = rank(`varlist') if `touse', by(`by') unique
			label var `pp' "rank"
		 }
		 else egen `pp' = mypp(`varlist') if `touse', by(`by') a(`a')
		 bysort `touse' `by': gen byte `group' = _n == 1 if `touse'
		 replace `group' = sum(`group')
		 local max = `group'[_N]
		 local bylab : value label `by'
		 local type : type `varlist'
		 local vallab : value label `varlist'

		 count if !`touse'
		 local j = 1 + r(N)
		 forval i = 1 / `max' {
			 tempvar y`i'
			 gen `type' `y`i'' = `varlist' if `group' == `i'
			 compress `y`i''
			 local ylist "`ylist' `y`i''"
			 local byval = `by'[`j']
			 if "`bylab'" != "" local byval : label `bylab' `byval'
			 label var `y`i'' `"`byval'"'
			 if "`vallab'" != "" label val `y`i'' `vallab'
			 count if `group' == `i'
			 local j = `j' + r(N)
	 	 }

		 if `"`ytitle'"' == "" {
	     	 	local ytitle : variable label `varlist'
	         	if "`ytitle'" == "" local ytitle "`varlist'"
	         	local ytitle "quantiles of `ytitle'"
	         }
	}

	qui if "`reverse'" != "" {
		if "`ranks'" != "" {
			bysort `touse' `by' (`pp') : ///
			replace `pp' = _N + 1 - `pp'
	         }
	         else replace `pp' = 1 - `pp'
	}

	if "`msymbol'" == "" {
		local msymbol "oh dh th sh smplus x O D T S + X"
	}

	qui if "`trscale'" != "" {
		local newpp : subinstr local trscale "@" "`pp'", all
		replace `pp' = `newpp'
		if `"`xtitle'"' == "" {
			local xtitle : subinstr local trscale "@" "P", all
		}
	}

	if `"`xtitle'"' != "" local xti `"xti("`xtitle'")"'

	twoway `plottype' `ylist' `pp' , yti(`"`ytitle'"') `xti' ///
	sort ms(`msymbol') `options' ///
	|| `plot'
	// blank

end