<>
******
bys id code (year): gen years_code=_n-1
******
HTH
Martin
_______________________
----- Original Message -----
From: "Hugh Colaco" <[email protected]>
To: "statalist" <[email protected]>
Sent: Monday, March 23, 2009 10:08 PM
Subject: st: Counting
Dear Statalisters,
I'm including below my data as it is now and what I want to get. The
data is sorted based on "id" and "year" in ascending order. There are
115 different "id", so "id" ranges from 1-115. "year" ranges from 1974
to 2001 although data is not available from 1974 for all observations
(as you can see below). Basically, I want to create this new variable
"years_code" that captures the number of consecutive years the same
code occurs (as shown below). As you can see, "code" sometimes changes
for a given "id".
Data currently What I want
id year code id year code years_code
1 1994 6 1 1994 6 0
1 1995 6 1 1995 6 1
1 1996 6 1 1996 6 2
1 1997 6 1 1997 6 3
1 1998 6 1 1998 6 4
1 1999 6 1 1999 6 5
1 2000 6 1 2000 6 6
1 2001 6 1 2001 6 7
2 1985 6 2 1985 6 0
2 1986 6 2 1986 6 1
2 1987 6 2 1987 6 2
2 1988 6 2 1988 6 3
2 1989 6 2 1989 6 4
2 1990 6 2 1990 6 5
2 1991 6 2 1991 6 6
2 1992 6 2 1992 6 7
2 1993 6 2 1993 6 8
2 1994 6 2 1994 6 9
2 1995 6 2 1995 6 10
2 1996 6 2 1996 6 11
2 1997 6 2 1997 6 12
2 1998 6 2 1998 6 13
2 1999 6 2 1999 6 14
2 2000 6 2 2000 6 15
2 2001 6 2 2001 6 16
3 1974 2 3 1974 2 0
3 1975 2 3 1975 2 1
3 1976 2 3 1976 2 2
3 1977 2 3 1977 2 3
3 1978 2 3 1978 2 4
3 1979 2 3 1979 2 5
3 1980 2 3 1980 2 6
3 1981 2 3 1981 2 7
3 1982 2 3 1982 2 8
3 1983 2 3 1983 2 9
3 1984 2 3 1984 2 10
3 1985 2 3 1985 2 11
3 1986 2 3 1986 2 12
3 1987 2 3 1987 2 13
3 1988 2 3 1988 2 14
3 1989 4 3 1989 4 0
3 1990 4 3 1990 4 1
3 1991 4 3 1991 4 2
3 1992 4 3 1992 4 3
3 1993 4 3 1993 4 4
3 1994 4 3 1994 4 5
3 1995 4 3 1995 4 6
3 1996 4 3 1996 4 7
3 1997 4 3 1997 4 8
3 1998 4 3 1998 4 9
3 1999 4 3 1999 4 10
3 2000 4 3 2000 4 11
3 2001 4 3 2001 4 12
Thanks in advance.
--
Hugh
*
* For searches and help try:
* http://www.stata.com/help.cgi?search
* http://www.stata.com/support/statalist/faq
* http://www.ats.ucla.edu/stat/stata/
*
* For searches and help try:
* http://www.stata.com/help.cgi?search
* http://www.stata.com/support/statalist/faq
* http://www.ats.ucla.edu/stat/stata/