Sounds like
gen id = _n
bysort var1 (id) : gen var2 = sum(id != id[_n-1] + 1)
sort id
Nick
[email protected]
kelly johnson
> I have a variable (var1) like this:
>
> var1
> a
> a
> a
> a
> a
> b
> a
> a
> a
> b
> a
> ... (there are several thousand of these)
>
> I want to creat a unique number to identify each set from a-b. e.g.:
> var1 var2
> a 1
> a 1
> a 1
> a 1
> a 1
> b 1
> a 2
> a 2
> a 2
> b 2
> a3
> ...
> I know how to use the _n command, but am unsure of how to
> employ it in this
> case (i can do it in the opposite order). any help would be
> appreciated.
*
* For searches and help try:
* http://www.stata.com/support/faqs/res/findit.html
* http://www.stata.com/support/statalist/faq
* http://www.ats.ucla.edu/stat/stata/