Bookmark and Share

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


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

st: Case-control matching_seek for help


From   Wu Yi <[email protected]>
To   "[email protected]" <[email protected]>
Subject   st: Case-control matching_seek for help
Date   Mon, 4 Apr 2011 21:15:10 +0800

Hi,

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 ualberta.ca

/*
----------------------------------------------------------------------------

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

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:

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

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


**id**
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:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/


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