Hi

I have a matched panel data set of treated and control firms, matched on industry (sic2), and 50% brackets around ROA and TotalAssets. The matching code is below:

Code:
preserve
keep if DealYear==2004 & Year==2003  
drop Year
tempfile 2004treated
save `2004treated'
 
restore
keep if pe==0 & Year==2003
drop Year
tempfile 2004controls
save `2004controls'

use `2004treated', clear
gen lower = TotalAssets*0.5
gen higher = TotalAssets*1.5

rangejoin TotalAssets lower higher using `2004controls', by(sic2)
keep if inrange(ROA1_w/ROA1_w_U, 1/1.50, 1.50)

gen long obs_no = _n
set seed 1234 //this keeps five random controls per treated firm//
gen double shuffle = runiform()
duplicates drop
by BvDIDnumber (shuffle), sort: keep if _n <= 5
drop shuffle



However, I would like to keep the 5 closest matches (where more than 5 are generated for a given match), not just a random 5 matches. Ideally I would like to keep the closest 5 based on distance where distance is defined by the sum of the squares of the difference between the target and the control firm's ROA and the target's and the control firm's TotalAssets. (or if anyone is aware of any other way of keeping the 5 closest matches, I'd be happy to hear any suggestion).

Appreciate any advice
Paul