program define muxyplot
*! 1.2.0 NJC 16 June 1999
        version 6.0
        syntax varlist(min=4) [if] [in] [fweight aweight iweight] /*
        */ [ , L2title(str) BY(str) Need(str) Connect(str) Symbol(str) Link * ]

        if "`link'" != "" & "`connect'" != "" {
                di in r "connect() may not be combined with link()"
                exit 198
        }

        tokenize `varlist'
	local nvars : word count `varlist'
        if mod(`nvars', 2) != 0 {
                di in r "need even number of variables"
                exit 198
        }
        local nvarsh = `nvars' / 2

        preserve
        tempvar id Y X Xmin pair

        gen `id' = _n

        if "`exp'" != "" {
                tempvar w
                gen `w' `exp'       /* `exp' might not be a variable */
                local exp "= `w'"
        }

        local i = 1
        local j = 1 + `nvarsh'
        while `i' <= `nvarsh' {
                local stargs "`stargs' ``i'' ``j'' `id' `w' `need'"
                local Ylab`i' : variable label ``i''
                if "`Ylab`i''" == "" { local Ylab`i' "``i''" }
                local Xlab`i' : variable label ``j''
                if "`Xlab`i''" == "" { local Xlab`i' "``i''" }
                local Ylist "`Ylist' ``i''"
                local Xlist "`Xlist' ``j''"
                local i = `i' + 1
                local j = `j' + 1
        }

        stack `stargs' `if' `in', into(`Y' `X' `id' `w' `need') wide clear

        local i = 1
        local j = 1 + `nvarsh'
        while `i' <= `nvarsh' {
                label var ``i'' "`Ylab`i''"
                label var ``j'' "`Xlab`i''"
                local i = `i' + 1
        }

        label var `X' "`Xlist'"
        if `"`l2title'"' == `""' { local l2title "`Ylist'" }
        if "`by'" != "" {
                sort `by'
                local byby "by(`by')"
        }
        if "`connect'" == "" { local connect : di _dup(`nvarsh') "." }
        if "`symbol'" == "" {
                local symbol = substr("opdOTS.",1,`nvarsh') + "i"
        }
        else { local symbol "`symbol'i" }

        qui if "`link'" == "link" {
                expand 2 if _stack > 1 & _stack < `nvarsh'
                egen `Xmin' = min(`X'), by(`id')
                replace `Xmin' = -`Xmin'
                sort `by' `Xmin' `id' _stack
                by `by' `Xmin' `id' : gen byte `pair' = int((_n + 1) / 2)
                sort `by' `Xmin' `id' `pair' `X'
                local connect "`connect'L"
        }

        gra `Ylist' `Y' `X' [`weight' `exp' ], /*
        */ `byby' s(`symbol') c(`connect') l2(`"`l2title'"') `options'
end