Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • loop for propensity score matching

    Hello community,

    I have unbalanced panel data.
    I need to do propensity score matching for each year separately.

    I want to do two nearest neighbor matching with replacement.

    I want to do it using a loop in Stata 14, how can I do that?

    I tried using this code but didn't work

    Code:
     levelsof year, local(years)
    
    foreach year in `years' {
       qui keep if year = `year'
       qui psmatch2  vc_backing wtoas age i.ctryiso_num i.main_industry, logit  outcome(matched_data`year'  pre_vc) neighbor(2)
       
    }
    Could you please give me suggestions on how I can do this in an efficient way? otherwise, i will have to do it manually and divide the data file into smaller files by year and do it manually for each year, which could take long because I have around 30 years in my data.

    Thank you.

  • #2
    You don't provide example data so its a little difficult to test and evaluate your code. That said, it seems like the problem is that you delete all observations not in the current year with:

    Code:
    qui keep if year = `year'
    The data will still be deleted on the next iteration of the loop. You could potentially implement the algorithm you think you want with the -preserve- and -restore- command, but I think its a bit more straightforward (and certainly more efficient) to just use an if statement at the end of psmatch2.

    Code:
    levelsof year, local(years)
    foreach year in `years' {
        qui psmatch2 vc_backing wtoas age i.ctryiso_num i.main_industry if year == `year', logit outcome(matched_data`year' pre_vc) neighbor(2)
    }
    Untested of course, but this should get you started on a working solution. Not sure why you want to do this -quietly- though.

    Comment

    Working...
    X