*! return table of rates, upper and lower confidence limit for poisson rate 
*! JM.Lauritsen Oct1999, version 1.01 (1.02 jan 2003 minor changes and change to ref)
*! ref: Estimates are made with Stata Command cii  
program define cflpois, rclass
	version 6
	syntax varlist(min=2 max=2) [if] [in] [, By(string) N(string)  Mult(real 100000.0)]
	preserve
	if "`if'" != "" {qui keep `if'}
	if "`in'" != "" {qui keep `in'}
	tokenize `varlist'
        local count "`1'"
	local pyrs "`2'"

	if "`n'" != "" {
		confirm var `n' 
		gen N = `n' }
	else { gen N = 1}

	if "`by'" == "" { collapse (sum) N "`count'" "`pyrs'"  }
        else {qui sort `by'
		collapse (sum) N "`count'" "`pyrs'", by("`by'") }
		qui gen double rate = `mult'*(`count'/`pyrs')
qui		gen double l = 1
qui		gen double h = 1

	local i = 1
	while `i' <= _N {
qui	cii "`pyrs'[`i']" "`1'[`i']" ,poisson
qui	replace l = r(lb)*`mult' in `i'
qui	replace h = r(ub)*`mult' in `i'
	local i = `i' +1
	}

	jl_f N 3 0
	jl_f "`pyrs'" 3 0
	jl_f "`count'" 3 0
	jl_f rate 5 1
	jl_f l 5 1
	jl_f h 1 1
	gen str1 a = "-"
	format a %1s
	move a h
	tokenize `by'
	local x1 = 0
	while "`1'" != "" {
		if `x1' > 0 { local x1 = `x1' + 2 }
		local x: label (`1') maxlength
		local x1 = `x1' + `x'
		format `1' %`x'.0f
		macro shift
	}

	list, nodisplay noobs
        
   if "`by'" != "" {
	collapse (sum) N "`count'" "`pyrs'"
	qui gen float rate = `mult'*`count'/`pyrs'
qui	gen double l = 1
qui	gen double h = 1
qui	cii `pyrs' `count' ,poisson
qui	replace l = r(lb)*`mult'
qui	replace h = r(ub)*`mult' 

	jl_f N 3 0
	jl_f rate 6 1
	jl_f l 5 1
	jl_f h 1 1
	gen str`x1' t = "Total" 
	format t %`x1's
	move t N
	gen str1 a = "-"
	format a %1s
	move a h
	list, noobs nodisp
   }

   restore

end

program define jl_f
	qui sum `1'
	local x = length(string(int(r(max))))
	local x = `x' + `2'
	format `1' %`x'.`3'f
end

program define jl_pois
	qui cii `3' `1' ,poisson
*qui for var l h rate: replace X = . if `1' == .

end