Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

Re: st: scrolling over observations

From   Russell Dimond <[email protected]>
To   [email protected]
Subject   Re: st: scrolling over observations
Date   Mon, 31 Jan 2005 14:14:01 -0600

You're almost there.

Keep in mind that each command is executed for all observations before the next command executes at all (I detect some SAS is your past). So you aren't actually incrementing count until after new has been set for all observations.

But that's okay--you don't need count at all. The sum function (running sum) will turn your 1's and 0's into exactly what you want. Here's a slightly simpler version of your program, though you could just add the last line to what you've got and you'd be set.

sort CoName
gen new= (CoName!=CoName[_n-1])
replace new=sum(new)

Russell Dimond
Research Computing Specialist
Social Science Computing Cooperative

Dr. J. Clovis, Economics wrote:

Dear listers,

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.

Thanks in advance

Kind regards

J.S. Clovis,

* For searches and help try:
*   For searches and help try:

© Copyright 1996–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index