I am running propsensity score matching for an evaluation project, but due to confidentiality, do not have access to the raw data. I am looking at finding matches for the treatment and then creating a variable that indicates the treatment observations and the matched controls. The code works fine on a simulated data set, but the person running the code indicated that an error occurred in a place that I was not expecting. For a quick quality control check, the code below creates the same matches as psmatch2 on the simulated dataset. One reason we are not using psmatch2 is that it can be a time intensive to get 3rd party ado files approved for the work environment. The sample size of the real data is about 9900 observations.

Code:
Code:
teffects psmatch (mexp) (treat mrq mexporter mimp mhcnt mfor divcode*, logit), nneighbor(5) generate(nn5_)
gen matched = treat == 1
local sampsize = _N
// Nearest Neighbour = 5  Generate when an observation is in the match
quietly forvalues i = 1/`sampsize' {
    if treatTailoredEver[`i'] == 1 {
        local match1 = nn5_1[`i']
        local match2 = nn5_2[`i']
        local match3 = nn5_3[`i']
        local match4 = nn5_4[`i']
        local match5 = nn5_5[`i']
        replace matched = 1 in `match1'
        replace matched = 1 in `match2'
        replace matched = 1 in `match3'
        replace matched = 1 in `match4'
        replace matched = 1 in `match5'
    }
}
The teffects function executed. The error message that occurred after running the forvalues loop was:
Code:
'.' invalid observation number
The debugging on that is relatively obvious in that some value in one of the 'match*' was equal to '.' and so I can easily put in a conditional statement to not replace the values in that scenario.

Yet the underlying issue, which is likely due to my inexperience on teffects, is that I do not know under what situations teffects will fail to find all 5 neighbours on a propensity score matching, but yet itself does not fail due to a failure in the overlap assumption. I would be grateful for any insights anyone has on this.

As an aside, the forvalues loop executes fairly quickly, but if anyone has suggestions on a more elegant solution to the loop, I'm all ears. Thanks!