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

Re: st: identify group of observations


From   Richard Williams <Richard.A.Williams.5@nd.edu>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: identify group of observations
Date   Wed, 28 Apr 2004 21:47:29 -0500

At 06:08 PM 4/28/2004 -0400, MITRA PINAKI  (MAR1PXM) wrote:
Hello List,

I have a data set where I need to group observations. The identifying flag
is two character string which can be either "01" or "02". Each "01"
observation is followed by "02", so the flag for the first obs is "01" and
the very next one's is "02". But these are always not in pair; order can be
"01" and "02"; "01", "02", "02"; "01", "02", "02, "02" and so on. All the
"02" obs. followed by "01" belong to one group. Is there any way I can
generate an identifier that indicates these groups?

Any help will be greatly appreciated.
Does this do what you want? I'm assuming that every time you encounter a 1, you want the groupid increased by 1 and to stay the same until you encounter another 1. In this example there are 3 groups of varying sizes.

. list

+----+
| x |
|----|
1. | 01 |
2. | 02 |
3. | 01 |
4. | 02 |
5. | 02 |
|----|
6. | 01 |
7. | 02 |
8. | 02 |
9. | 02 |
10. | 02 |
+----+

. gen groupid = 1 in 1
(9 missing values generated)

. replace groupid = cond(x=="01",groupid[_n-1]+1,groupid[_n-1]) in 2/l
(9 real changes made)

. list

+--------------+
| x groupid |
|--------------|
1. | 01 1 |
2. | 02 1 |
3. | 01 2 |
4. | 02 2 |
5. | 02 2 |
|--------------|
6. | 01 3 |
7. | 02 3 |
8. | 02 3 |
9. | 02 3 |
10. | 02 3 |
+--------------+


-------------------------------------------
Richard Williams, Notre Dame Dept of Sociology
OFFICE: (574)631-6668, (574)631-6463
FAX: (574)288-4373
HOME: (574)289-5227
EMAIL: Richard.A.Williams.5@ND.Edu
WWW (personal): http://www.nd.edu/~rwilliam
WWW (department): http://www.nd.edu/~soc

*
* 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