program define kdmany
  version 6
  syntax varlist(numeric) [if] [in], [LOG DEBUG  /*
     */ BIweigh COSine EPan GAUss PARzen RECtang TRIangl /*
     */ N(integer 1) Width(real 0) AT(varname) /*
     */ ZEROES Symbol(str) Connect(str) SOrt *]
  local kernels "biweigh cosine epan gauss parzen rectang triangl"
  tokenize `kernels'
  tempname s
  scalar `s'=0
  local k=1
  while `k'<=7 {
     scalar `s'=`s'+("```k'''"~="")
     local k=`k'+1
  }
  if `s'>=2 {
    di in red "only one type of kernel may be specified"
    exit 198
  }
  tokenize `varlist'
  preserve
  local kdopt "`biweigh' `cosine' `epan' `gauss' `parzen' `rectang' `triangl'"
  if "`n'"~="" { local kdopt "`kdopt' n(`n')" }
  if "`width'"~="" { local kdopt "`kdopt' width(`width')" }
  if "`at'"~="" { local kdopt "`kdopt' at(`at')" }
  if "`log'"=="" & "`zeroes'"~="" {
     di in red "zeroes may not be specified without log"
     exit 198
  }
  qui {
    marksample touse
    keep if `touse'
    local cond
    if "`if'"~="" | "`in'"~="" { local cond `"Cond."' }
    if "`zeroes'"~="" {
       local max=-1e12
       local min=1e12
    } /* to create a kernel thing for zeroes logarithmed */
    local k=1
    while "``k''"~="" {
      tempvar y`k' x`k' d`k'
      if "`log'"~="" {
        g double `y`k''=log(``k'')
        if "`zeroes'"~="" {
           qui sum `y`k''
           if `min'>r(min) {local min=r(min)}
           if `max'<r(max) {local max=r(max)}
        }
      }
      else { g double `y`k''=``k'' }
      local k=`k'+1
      * created the working variables and accumulated mins and maxs
    }
    if "`zeroes'"~="" {
      local k=1
      while "``k''"~="" {
         replace `y`k''=1.5*`min'-0.5*`max' if ``k''==0
         local k=`k'+1
      }
    }
    * jittered zeroes
    local stacker  /* this would be the stack of the variables */
    local into "a" /* "a" would be the name of the horizontal axis var */
    tempvar mv
    g byte `mv'=.  /* a fake variable to be inserted into stack */
    local k=1
    while "``k''"~="" {
       kdensity `y`k'', generate(`x`k'' `d`k'') nograph `kdopt'
       if "`log'"~="" { replace `x`k''=exp(`x`k'') }
       local stacker "`stacker' `x`k''"
       local j=1
       while "``j''"~="" {
          if `k'==`j' { local stacker "`stacker' `d`k''" }
          else        { local stacker "`stacker' `mv'"   }
          local j=`j'+1
       }
       local into "`into' b`k'"
       local k=`k'+1
    }
    stack `stacker', into(`into') clear
    * so, we stacked all we wanted; now, to graph
    local grvar
    local sym
    local conn
    local k=1
    label var a "`varlist'"
    if "`log'"~="" {label var a "log of `varlist'" }
    while "``k''"~="" {
       lab var b`k' "`cond' density of `log' ``k''"
       local grvar "`grvar' b`k'"
       local sym "`sym'."
       local conn "`conn'l"
       local k=`k'+1
    }
    if "`log'"~="" { local xlog "xlog" }
    graph `grvar' a, s(`sym') c(`conn') `xlog' sort `options'
  }
  * done
end