I have been trying to scroll through observations of a variable and assign
an identifying code to each occurrence of a key value.
For example, I want to scroll though the variable CoName and give each
occurrence of the company BCA, say, the code 4, say. I want to arrange it
so that the 1st company (in alpha order) receives code 1 and the last, 680
or whatever is the maximum number of companies in the database.
I have tried the following which gives me a '1' at the start of the
occurrence of a new company name and zeros for all other occurrences for
that company:
gen new=0
local count =0
bysort CoName: replace new = `count'+1 if CoName[_n] ~= CoName[_n-1]
count = `count'+1
Can anyone say where I'm going wrong. It may help to know that I do not
know 'a priori' the number of companies in the dataset or indeed how many
times each companies is observed.
I did look at the FAQs on the STATA website but could not understand much
of what was said there. Although the for command should help, I can't seem
to get it to do the above.
I have been away from this list for a number of years and so protocol may
have moved on, so if I am breaching any please excuse my ignorance.