Michael Blasnik, Scott Merryman and
Russell Dimond gave good answers.
Russell's can be telescoped to
bysort CoName : gen new = _n == 1
replace new = sum(new)
or to
sort CoName
gen new = sum(CoName != CoName[_n-1])
and is equivalent to Scott's
suggestion, except that -egen-
will not change the sort order.
In addition, Dr Clovis asked where (s)he is
going wrong.
Basically, Dr Clovis is expecting a
loop in circumstances where Stata won't
execute one. Consider the code
gen new=0
local count =0
bysort CoName: replace new = `count'+1 if CoName[_n] ~= CoName[_n-1]
count = `count'+1