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

 From "Michael Blasnik" To 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:

Michael Blasnik
michael.blasnik@verizon.net

----- Original Message ----- From: "Mike Lacy" <Michael.Lacy@colostate.edu>
To: <statalist@hsphsun2.harvard.edu>
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"

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:

gen MyGroup = 1 if _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/
```