*! version 6.0.1  19feb2003
program def gencc, rclass
version	 6
	syntax varlist(min=2 max=2) [if] [in] [fweight] , group(varname) [ /*
		*/ Label(string) Level(integer $S_level) exact Woolf TB ]
	marksample touse, strok

	qui {
		tab `group'
		tokenize `varlist'
		local type1: type `1'
		local type2: type `2'
		tempvar a1 a2 both  al1 al2
		if substr("`type1'",1,3)!= "str" {
		        gen str8 `al1'=string(`1')
		}
		else gen str8 `al1'= `1'
		if substr("`type2'",1,3)!= "str" {
		        gen str8 `al2'= string(`2')
		}
		else gen str8 `al2'= `2'

		gen str8 `a1'= `al1' if `al1'<=`al2'
		gen str8 `a2'= `al2' if `al1'<=`al2'
		replace  `a1'= `al2' if `a1'==""
		replace  `a2'= `al1' if `a2'==""

		tempvar cnt
		if "`weight'" != "" {
			gen double `cnt' `exp'
			compress `cnt'
		}
		else	gen byte `cnt' = 1
		tempvar GRP NGRP
		sort `group'
		qui by `group': gen long `GRP' = 1 if _n==1
		gen `NGRP' = sum(`GRP')
		local iby = `NGRP'[_N]
		if `iby'~=2 {
			noi di in red "`group' must have two levels"
			exit 198
		}

		tempfile file1
		preserve
		drop `1' `2'
		drop if `cnt'==0
		keep if `touse'
		keep `a2' `group' `cnt'
		expand `cnt'
		save "`file1'", replace
		restore, preserve
		drop if `cnt'==0
		keep if `touse'
		keep `a1' `group' `cnt'
		expand `cnt'
		rename `a1' `a2'
		append using "`file1'"
		sort `a2'
		tempvar first values var ngrp
		gen str8 `var'=""
		gen int `values'=.
		by `a2': gen `first'=1 if _n==1
		local type: type `group'
		if substr("`type'",1,3)== "str" {
                        encode `group', gen(`ngrp')
                }
		else {
			gen `ngrp'=`group'
		}
		sort `first' `a2'
		local i 1
	}
	while `first'[`i']~=. {
		qui replace `var'=`a2'[`i']
		qui replace `values'=0
		qui replace `values'=1 if `a2'==`var'
		qui count if `values'==1 & `ngrp'~=0
		local a=r(N)
		qui count if `values'==1 & `ngrp'==0
		local b=r(N)
		qui count if `values'==0 & `ngrp'~=0
		local c=r(N)
		qui count if `values'==0 & `ngrp'==0
		local d=r(N)

		local col1= `var'
		local col2="not_`col1'"
		gencci `a' `b' `c' `d', col(Allele) col1(`col1') col2(`col2') /*
		*/ `exact' `woolf' `tb'
		di ""
		local i=`i'+1
	}
end