Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

st: SV: Case-control matching_seek for help

From   "Jesper Lindhardsen" <>
To   <>
Subject   st: SV: Case-control matching_seek for help
Date   Tue, 5 Apr 2011 08:49:50 +0200

Hi Wu,

I think I would look into some of the programs written for this purpose AND are also documented; for example 


(use -findit- to install)
Also, if matching are to be exact(ie sex birthyear ect), you could use a "merging" strategy instead.

However, the problem with insufficient controls means that ypu would either accept it or use controls more than once - I am not sure how to do this, but you could make a 'fake' stset and then use the stata command -sttocc-



-----Oprindelig meddelelse-----
Fra: [] På vegne af Wu Yi
Sendt: 4. april 2011 15:15
Emne: st: Case-control matching_seek for help


I am a research assistant in the Department of Public Health, National University of Singapore. I want to do a case-control matching.

I found the following program by Stefan Kuhle using Google search.

However, the program does not work if there are not enough matching controls. It just goes to a dead loop and never exits.
Any suggestions to solve the problem? Many thanks in advance.

Best regards,
Wu Yi

* Case-control matching
(C) Stefan Kuhle, University of Alberta, 2007
E-mail: myfirstname.mylastname at


This syntax will perform a 1:2 case-control matching on one matching 

If you want to do a 1:1 or 1:3 etc matching, please change 
the while-loop condition below as indicated.

If you want to match on two 
or more variables, then please uncomment the statement

'& matchvar2[`i'] == matchvar2[`rndobs']'

in the while-loop. To match on three or more variables, append more of above 
statements (using matchvar3, matchvar4, etc) to the if..then condition.


You will need the following variables in your dataset:

Variable on which cases and controls are matched. For obvious reasons, 
this variable should be binary or categorical.

Binary variable that specifies if the observation is a case (=1) or a 
potential control (=0).

Unique observation ID


gsort - case id

gen match = _n if case == 1

replace match = 99999 if match == .

quietly count if case == 1
local casecount r(N)

local matched `casecount'
local total _N
local i 1

set seed 65979

quietly while `i' <= `casecount' {
        local assigned 0

while `assigned' < 2 { /* If you want to do a 1:1 match, change this number to 1 (or to 3 for a 1:3 match etc) */

                local rndobs =  `matched' + int(uniform() * (`total' - `matched')) + 1

/* Un-comment inserted statement below to match on two variables */
/* Add more statements to match on three or more variables */

                if matchvar[`i'] == matchvar[`rndobs'] /* & matchvar2[`i'] == matchvar2[`rndobs'] */ & match[`rndobs'] == 99999 {

                        replace match = `i' in `rndobs'
                        local matched = `matched' + 1
                        gsort - case match id
                        local assigned = `assigned' + 1
        local i = `i' + 1

drop if match == 99999 /* This will drop all unmatched observations */

tab case matchvar /* There should now be exactly twice as many controls as cases */

*   For searches and help try:

*   For searches and help try:

© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index