*! version 1.0.3 SV/JBC 17 Sept 1997 after Ramalheira/Gould/Cox
program define gmci
	version 5.0
	local varlist "req ex"
	local if "opt"
	local in "opt"
    	local weight "aweight fweight"
	local options "Level(int $S_level) Add(real 0) Only"
	parse "`*'"

	parse "`varlist'", parse(" ")
	if `add' < 0 {
	di in r "A negative value cannot be added."
	exit 198
	}
	if `level'<10 | `level'>99 {
	  dis in red "level() invalid"
	  exit 198
	  }
	tempvar var1 junk

	local i 0
	while "`*'"!="" {
	    qui gen `var1' = `1' + `add'
	    qui su `var1' [`weight' `exp'] `if' `in'
	    local min=_result(5)
	      if `min' <= 0 {
 		  if "`if'"=="" {
		    qui su `1' [`weight' `exp'] `in' if `1'>0
			}
	 	  else {
		    qui su `1' [`weight' `exp'] `if' & `1'>0 `in'
			}
		local minpos=_result(5)
		  if `add' > 0 {
	            di in r "Nonpositive values still encountered in variable `1'."
		    di in r "Minimum value of (`1'+`add') is " %4.1g `min' /*
		     */", minimum positive value of `1' is " %4.1g `minpos'
		          }
		  if `add' == 0 {
	            di in r "Nonpositive values encountered in variable `1'."
		    di in r "Minimum value of `1' is " %4.1g `min' /*
		     */", minimum positive value of `1' is " %4.1g `minpos'
		          }
		local i 1
	    	}
	    drop `var1'
	    mac shift
	}

	if `i' == 1 {
		exit 411
	  }

	parse "`varlist'", parse(" ")

	di " "
	di in gr "  Variable  |    Obs     Geometric Mean           [" /*
		*/ `level' "%"   /*
		*/ " Conf. Interval]"
	di in gr "------------+" _dup(59) "-"

	local msg 0

	while "`1'" ~= "" {
	qui su `1' [`weight' `exp'] `if' `in'
	  if _result(5) > 0 & "`only'"~="" {
		qui gen `var1' = `1'
		local star  ""
				}
	  else {
	   	qui gen `var1' = `1' + `add'
		local msg 1
		local star *
			}
		qui gen `junk' = ln(`var1') `if' `in'
		qui ci `junk' [`weight' `exp'] `if' `in' , /*
			*/ `options' level(`level')
		if `add'==0 { local star = "" }
		local namelen = 10 - length("`1'")
		display in ye _dup(`namelen') " " "`1'" _col(13) /*
		*/in gr "|"  in ye _col(16) %5.0f _result(1)  /*
		*/ in ye _col(27) %12.5f exp($S_3) /*
		*/ in ye "      " in ye _col(45) %12.5f exp($S_5) /*
		*/ in ye _col(59) %12.5f exp($S_6) in w " `star'"

	global S_3 = exp($S_3)
    	global S_4 = exp($S_4)
    	global S_5 = exp($S_5)
    	global S_6 = exp($S_6)
	drop `var1' `junk'
	mac shift
	}

di " "
if `msg'~=0 & `add' >0 {
	di in gr "(" in w "*" in gr ") `add' was added to the " /*
		*/ "variable(s) prior to calculating the results"
	}

end