This shouldn't be too difficult, unless if you data is structured in an odd way.
You can use lag or retain to check if the encounter is to be counted, and retain to keep track of the number of qualifying encounter per patient. When reading the last record for each patient, just out the ones that has 3 or more qualifying encounters.
Good luck!
/Linus
Data never sleeps