My data has a country variable using three digit ISO-3166 codes. The underlying codes aren't quite sequential anyway, and then because I'm using a subset of countries there are gaps. To make some loops run smoothly, I would like to have a variable where countries are 1, 2, 3 etc. I think the backbone of the code I need is something like:
Code:
//sort country wave id
// gen newvar=1 if obs==1
//gen newvar=newvar(_n-1) if country(_n-1)=country
//replace newvar=newvar(_n-1)+1 if country(_n-1)!=country
I expect there are obvious pitfalls to that approach that I'm missing, and this feels like something egen should be able to do in a single line. Any guidance appreciated.