user4662234
user4662234

Reputation:

Stata: from a bunch of duplicates, keep a particular one

In Stata, I have 3 variables: "objectid", "year", and "count". There are several duplicates in terms of "objectid" and "year". From these duplicates, I would like to keep the one with the highest value in "count".

Upvotes: 0

Views: 1589

Answers (1)

Nick Cox
Nick Cox

Reputation: 37278

This is standard stuff requiring only (1) getting the observations into a sort order where you want is identifiable and (2) working under the aegis of by:. See manual entries for by: and/or http://www.stata-journal.com/sjpdf.html?articlenum=pr0004

bysort objectid year (count) : keep if _n == _N 

Note that if count is ever missing, that value will be the one kept.

Upvotes: 1

Related Questions