Hello,
I am trying to sample for a case-control study based on date of death and matching on GP (pracid). I was able to achieve the commands for what I need per case (a variable with each date of death repeated for all the participants and a variable with a pair ID) but I couldn't match on GP.
The dataset is as follows (a made up one for demonstration):
ps- pracid variable stands for general practice ID


Code:
* Example generated by -dataex-. To install: ssc install dataex
clear
input float(patid death dateofdeath pracid)
 4 0     . 1
 6 0     . 1
 7 0     . 1
 3 0     . 1
 1 0     . 1
10 0     . 1
 8 1 15526 1
 2 0     . 1
 9 0     . 1
 5 1 15007 1
11 0     . 2
12 0     . 2
13 0     . 2
14 0     . 2
15 0     . 2
16 0     . 2
17 0     . 2
18 0     . 2
19 1 18145 2
20 0     . 2
end
format %d dateofdeath

The successful commands for each case are:

forval j = 1/20 { //patient ID
use miniset,clear
count if patid == `j' & death==1
if r(N)==1 {
gen pairid= `j'
gen matchedindexdate = dateofdeath
replace matchedindexdate=0 if pairid != patid
gsort matchedindexdate
replace matchedindexdate= matchedindexdate[_N]
save temp/eligibles`j'

}

}


use temp/eligibles5, clear
forval i=6/20 {
capture append using temp/eligibles`i'
save eligibles, replace
}

but I don't know how and where to do the GP matching within the loop!

I thought of the following, but didn't know how to fit it into the loop described above:

forval i=1/2 { //practice ID
use miniset, clear
keep if pracid==`i'
qui tab death
if r(r)==2 {

NB- with GP matching, I should end up with a dataset of 30 patients for the data I provided here.

Your help is much appreciated.