* nearest neighbor match with two confounders

use "https://github.com/scunning1975/mixtape/raw/master/nnmatch_distance.dta", clear

* Estimate ATT using nearest neighbor match
teffects nnmatch (earnings age gpa) (treat), atet gen(osample) metric(euclidean) // ATT = 1607.50

* Step 1: Create a variable for matched outcomes
gen matched_outcome = .
gen distance = .

* Step 2: Fill matched_outcome and calculate Euclidean distances
foreach i in 1/30 {  // Iterate over all units
    if treat == 1 {
        * Find the matched control unit using osample1
        replace matched_outcome = earnings[osample1[_n]] if treat == 1
    }
}

gen diff_in_means = earnings - matched_outcome

* Step 3: Display results to verify
list unitid age gpa earnings diff_in_means treat osample1 matched_outcome  if treat == 1

su diff_in_means // ATT = 1607.50

* Estimate ATT using nearest neighbor match with Mahanalobis distance
teffects nnmatch (earnings age gpa) (treat), atet metric(maha)