*! version 6.0.0  29apr1999
program define gencci, rclass
	version 6

	gettoken a 0 : 0, parse(" ,")
	gettoken b 0 : 0, parse(" ,")
	gettoken c 0 : 0, parse(" ,")
	gettoken d 0 : 0, parse(" ,")

	confirm integer number `a'
	confirm integer number `b'
	confirm integer number `c'
	confirm integer number `d'

	if `a'<0 | `b'<0 | `c'<0 | `d'<0 {
		di in red "negative numbers invalid"
		exit 498
	}

	syntax [, Exact Level(integer $S_level) COL(string) Woolf TB /*
		*/ COL1(string) COL2(string) ]

	if `level'<10 | `level'>99 {
		local level 95
	}
	di

	local m1=`a'+`b'
	local m0=`c'+`d'
	local n1=`a'+`c'
	local n0=`b'+`d'
	local t=`n1'+`n0'
	local iz=invnorm(1-(1-`level'/100)/2)
	if "`col'"=="" {
		local col="Allele"
	}
	if "`col1'"=="" | "`col2'"=="" {
		local col1 = "A"
		local col2 = "Not_A"
	}


	MYCrc `a' `b' `c' `d' `"`col'"' "" /*
		*/ `col1' `col2' Cases Controls /*
		*/ "" yes
	di in gr _col(18) "|" _col(43) "|"
	di in gr _col(18) /*
	*/ "|      Point estimate    |  [`level'% Conf. Interval]" _n /*
	*/ _col(18) "|" _dup(24) "-" "+" _dup(22) "-"

	_crcor `a' `b' `c' `d' `level' `woolf' `tb'
	local r  = r(nf)
	local r0 = r(lb)
	local r1 = r(ub)
	local how `r(label)'
	di in gr "      Odds ratio | " in ye /*
		*/ _col(27) %9.0g `r' in gr _col(43) "| " /*
		*/ in ye %9.0g `r0' "   " %9.0g `r1' in gr `"  `r(label)'"'

	if `r'>=1 {
		local afe=cond(`a'>0 & `b'>0 & `c'==0 & `d'>0,1,(`r'-1)/`r')
		local afe0=(`r0'-1)/`r0'
		local afe1=(`r1'-1)/`r1'
		local afp=(`a'/`m1')*`afe'
		local hdr " Attr. frac."
	}
	else {
		local hdr " Prev. frac."
		local afe=1-`r'
		local afe0=1-`r1'
		local afe1=1-`r0'
		local afp=((`a'/`m1')*`afe')/((`a'/`m1')*`afe'+`r')
	}
	di in gr `"`hdr' ex. | "' in ye /*
		*/ _col(27) %9.0g `afe' in gr _col(43) "| " /*
		*/ in ye %9.0g `afe0' "   " %9.0g `afe1' in gr `"  `how'"' _n /*
		*/ `"`hdr' pop | "' in ye /*
		*/ _col(27) %9.0g `afp' in gr _col(43) "|"

	if `"`exact'"'=="" {
		_crcchi2 `a' `b' `c' `d'
                global S_1 = r(chi2) /* double save in S_ and r() */
		global S_2 = chiprob(1,r(chi2))
		ret scalar chi2 = r(chi2)
		ret scalar p = chiprob(1,r(chi2))
		di in gr _col(18) "+" _dup(47) "-" _n /*
		*/ _col(19) "           chi2(1) =" in ye %9.2f return(chi2) /*
		*/ in gr "  Pr>chi2 =" in ye %7.4f return(p)
	}
	else {
		quietly tabi `a' `b' \ `c' `d', exact
		global S_1 = r(p1_exact)  /* double save in S_ and r() */
		global S_2 = r(p_exact)
		ret scalar p1_exact = r(p1_exact)
		ret scalar p_exact = r(p_exact)
		di in gr _col(18) "+" _dup(47) "-" _n /*
		*/ _col(33) "1-sided Fisher's exact P =" /*
		*/ in ye %7.4f return(p1_exact) _n /*
		*/ in gr _col(33) "2-sided Fisher's exact P =" /*
		*/ in ye %7.4f return(p_exact)
	}

	ret scalar or = `r'
	ret scalar lb_or = `r0'
	ret scalar ub_or = `r1'
	ret scalar afe = `afe'
	ret scalar lb_afe = `afe0'
	ret scalar ub_afe = `afe1'
	ret scalar afp = `afp'

	/* double save in S_# and r() */
	global S_3
	global S_4
	global S_5
	global S_6
	global S_7
	global S_8
	global S_9 `r'
	global S_10 `r0'
	global S_11 `r1'
	global S_12 `afe'
	global S_13 `afe0'
	global S_14 `afe1'
	global S_24 `afp'
end

program define MYCrc
	args a b c d stub1 stub2 col1 col2 row1 row2 notot addpro
	if `"`stub1'"'!="" {
		local header yes
		local s1 : piece 1 24 of `"`stub1'"'
		di _col(18) in gr `"| `s1'"' _col(43) "|" _c
		local s1 : piece 2 24 of `"`stub1'"'
		local j 2
		while `"`s1'"' != "" {
			di _n _col(18) in gr `"| `s1'"' _col(43) "|" _c
			local j = `j' + 1
			local s1 : piece `j' 24 of `"`stub1'"'
		}
	}
	local s1 = 9-length(`"`col1'"')
	local s2 = 9-length(`"`col2'"')
	if `"`addpro'"'!="" {
		if `"`header'"'=="yes" {
			di in gr _skip(14) "Proportion"
		}
		else	di in gr _col(57) "Proportion"
		local total "Total   with allele"
		local dd 24
		local cc "_c"
	}
	else {
		if `"`header'"'=="yes" { di }
		local total "Total"
		local dd 10
	}
	di in gr `"`stub2'"' _col(18) "| " /*
		*/ _skip(`s1') `"`col1'"' "   " /*
		*/ _skip(`s2') `"`col2'  |     `total'"' _n /*
		*/ _dup(17) "-" "+" _dup(24) "-" "+" _dup(`dd') "-"
	local row1=substr(`"`row1'"',1,16)
	local s1 = 16-length(`"`row1'"')
	di in gr _skip(`s1') `"`row1' | "' /*
		*/ in ye %9.0g `a' "   " %9.0g `b' /*
		*/ in gr "  | " in ye %9.0g `a'+`b' `cc'
	if `"`addpro'"'!="" {
		di %12.4f `a'/(`a'+`b')
	}
	local row2=substr(`"`row2'"',1,16)
	local s1 = 16-length(`"`row2'"')
	di in gr _skip(`s1') `"`row2' | "' /*
		*/ in ye %9.0g `c' "   " %9.0g `d' /*
		*/ in gr "  | " in ye %9.0g `c'+`d' `cc'
	if `"`addpro'"'!="" {
		di %12.4f `c'/(`c'+`d')
	}
	di in gr /*
		*/ _dup(17) "-" "+" _dup(24) "-" "+" _dup(`dd') "-"
	if `"`notot'"'!="" { exit }
	di in gr _col(12) "Total | " in ye /*
		*/ %9.0g `a'+`c' "   " %9.0g `b'+`d' /*
		*/ in gr "  | " in ye %9.0g `a'+`b'+`c'+`d' `cc'
	if `"`addpro'"'!="" {
		di %12.4f (`a'+`c')/(`a'+`b'+`c'+`d')
	}
end