-rangejoin- only allows one interval. If you wanted to add another variable for exact match, you could do it by adding that variable to the list in the -by()- option. But for this you need additional code. Before you do the random selection of one age-matched control, weed out any of the age-matches where the IQ falls outside the matching interval.
Note: I matched to within 1 year on age because the way my toy data worked out there were very few matches within 0.5 years. Modify your code accordingly.
Code:
clear // CREATE TOY DATA set seed 1234 set obs 100 gen id = _n gen arm = mod(_n, 2) label define arm 0 "Control" 1 "Treatment" label values arm arm gen sex = runiform() < 0.5 label define sex 0 "Female" 1 "Male" label values sex sex gen age = rgamma(8, 5) gen iq = rnormal(100, 15) // SAVE CONTROLS IN A TEMPORARY FILE preserve keep if arm == "Control":arm tempfile controls save `controls' // LOAD IN THE TREATMENT GROUP restore keep if arm == "Treatment":arm rangejoin age -1 1 using `controls', by(sex) keep if inrange(iq_U - iq, -10, 10) gen double shuffle = runiform() by id (shuffle), sort: keep if _n == 1 drop shuffle
Comment