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

st: Re: Number cases into groups based on a shared value


From   "Michael Blasnik" <[email protected]>
To   <[email protected]>
Subject   st: Re: Number cases into groups based on a shared value
Date   Mon, 14 Mar 2005 13:40:57 -0500

You can do this in Stata with:

sort SomeNum
gen MyGroup=sum(SomeNum!=SomeNum[_n-1])


Michael Blasnik
[email protected]

----- Original Message ----- From: "Mike Lacy" <[email protected]>
To: <[email protected]>
Sent: Monday, March 14, 2005 1:05 PM
Subject: st: Number cases into groups based on a shared value



I'm wanting to learn about a "do it yourself" way to do what is accomplished by the -group- function in the -egen- command in the following:

set obs 100
gen SomeNum = 10 + int(7 * uniform())
* Attach a sequential group number to all the
* cases with the same value for "SomeNum"
egen GroupNum = group(SomeNum)


This works fine at accomplishing the task. My interest in the DIY approach is that the kind of algorithm I am accustomed to using for this task does not fit with the inner nature <grin> of Stata. I'm accustomed (in SPSS or lower level languages) something like:

sort SomeNum
gen MyGroup = 1 if _n ==1
gen Same = (Somenum = Somenum[_n-1])
gen MyGroup = MyGroup[_n-1] if Same
gen MyGroup = 1+ MyGroup[_n-1] if ! Same

This doesn't fit with how Stat does -if-, as near as I
understand. So, what would the Stata DIY approach to this kind of algorithm be? All I could come up with was to put SomeNum into a matrix so that I could loop through it, but that hardly seems like a desirable way to do things.

Thanks,


=-=-=-=-=-=-=-=-=-=-=-=-=
Mike Lacy
Fort Collins CO USA
(970) 491-6721 office
*
*   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/



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