* Genotype Tools Kit: Merge * Daniel E. Cook 2011 program define ccmatch version 11.0 syntax varlist [if] [in] [, cc(name) id(name)] tempvar match randorder ucc quietly generate `match' = "" foreach var in `varlist' { capture confirm numeric variable `var' if !_rc { quietly replace `match' = `match' + string(`var') + "_" } else { quietly replace `match' = `match' + `var' + "_" } } // Generate a random variable for sorting purposes. generate `randorder' = runiform() local max = _N // Sorts data randomly. sort `match' `cc' `randorder' // Generate variables if we don't have them already. capture confirm variable match_id if !_rc { drop match_id } // Generate Match ID var. based on type of id (numeric or string) capture confirm string variable `id' if !_rc { quietly generate match_id = "" noisily di "string created" } else { quietly generate match_id = . noisily di "numeric created" } // Generate variables if we don't have them already. capture confirm variable matched_pair if !_rc { drop matched_pair } // Generate matched pair generate matched_pair = . // Generate a numerical variable for case/control if need be. capture confirm numeric variable `cc' if _rc { quietly encode `cc', generate(`ucc') quietly replace `ucc' = `ucc' - 1 } else { quietly generate `ucc' = `cc' } foreach obs of numlist 1/`max' { if `ucc'[`obs'] == 0 { quietly count if `match' == `match'[`obs'] local traverse_amount =`r(N)' foreach moveup of numlist 1/`traverse_amount' { local match_obs = `obs' + `moveup' - 1 if (`match'[`obs'] == `match'[`match_obs']) & `ucc'[`match_obs'] == 1 & matched_pair[`obs'] == . & matched_pair[`match_obs'] == .{ quietly { local pair = `pair' + 1 // The Pair Counter local pair_obs = `obs'+`moveup' - 1 replace match_id in `obs' = `id'[`match_obs'] replace match_id in `match_obs' = `id'[`obs'] replace matched_pair in `obs' = `pair' replace matched_pair in `pair_obs' = `pair' } } } } } sort matched_pair order match* di di "Total Matches: `pair'" end