I have a very large dataset but to cut it short I demonstrated the data with the following example:
I am trying to sample for a case-control study based on date of death. At this stage, I need to first create a variable with each date of death repeated for all the participants (hence we end up with a dataset with 20 participants) and a "pairid" equivalent to the patient id (patid) of the corresponding case.
I created a macro for one case (which works) but finding it difficult to have it repeated for all cases (where death==1) in a loop.
The successful macro is as follows:
and the loop I attempted is:
but I get
How can I do the loop?
Your help is highly appreciated.
Code:
* Example generated by -dataex-. To install: ssc install dataex clear input float(patid death dateofdeath) 1 0 . 2 0 . 3 0 . 4 0 . 5 1 15007 6 0 . 7 0 . 8 1 15526 9 0 . 10 0 . end format %d dateofdeath
I created a macro for one case (which works) but finding it difficult to have it repeated for all cases (where death==1) in a loop.
The successful macro is as follows:
Code:
local i "5" //patient id who died gen pairid= `i' gen matchedindexdate = dateofdeath replace matchedindexdate=0 if pairid != patid gsort matchedindexdate replace matchedindexdate= matchedindexdate[_N] save temp`i'
and the loop I attempted is:
Code:
forval j = 1/10 (min and max patid id) { count if patid == `j' & death==1 //to specify that I want the loop run for cases only. if r(N)=1 { gen pairid= `j' gen matchedindexdate = dateofdeath replace matchedindexdate=0 if pairid != patid gsort matchedindexdate replace matchedindexdate= matchedindexdate[_N] save temp/matched`j' } } use temp/matched1, clear forval i=2/10 { capture append using temp/matched`i' save matched, replace }
Code:
invalid syntax
How can I do the loop?
Your help is highly appreciated.
Comment