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

st: Number cases into groups based on a shared value


From   Mike Lacy <Michael.Lacy@colostate.edu>
To   statalist@hsphsun2.harvard.edu
Subject   st: Number cases into groups based on a shared value
Date   Mon, 14 Mar 2005 11:05:48 -0700

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–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index