I am trying to count the number of marriages that an individual experiences. So I want to loop over the years each individual is in the data set and create a counter variable that is 0 until the year of first marriage ("marriage"), takes a value of 1 during the year when the first marriage occurs, remains 1 until (if) the next marriage occurs, and then becomes a 2, etc.. So for person 10003 below, the counter var would be 0 for the years 1956-1961, would be "1" for the years 1962-1969, and then "2" for the years 1970-2017.
The code I tried previously was this:
gen y=0
forvalues i=1/`=_N' {
replace y=`i'+1 if mar ~= .
}
But that resulted in y=1963 during the two years a marriage occurred and 0 for all other years.
thank you in advance for your help-
Christina
Code:
* Example generated by -dataex-. To install: ssc install dataex clear input float ID int year float marriage 10003 1956 . 10003 1957 . 10003 1958 . 10003 1959 . 10003 1960 . 10003 1961 . 10003 1962 1962 10003 1963 . 10003 1964 . 10003 1965 . 10003 1966 . 10003 1967 . 10003 1968 . 10003 1969 . 10003 1970 1970 10003 1971 . 10003 1972 . 10003 1973 . 10003 1974 . 10003 1975 . 10003 1976 . 10003 1977 . 10003 1978 . 10003 1979 . 10003 1980 . 10003 1981 . 10003 1982 . 10003 1983 . 10003 1984 . 10003 1985 . 10003 1986 . 10003 1987 . 10003 1988 . 10003 1989 . 10003 1990 . 10003 1991 . 10003 1992 . 10003 1993 . 10003 1994 . 10003 1995 . 10003 1996 . 10003 1997 . 10003 1998 . 10003 1999 . 10003 2000 . 10003 2001 . 10003 2002 . 10003 2003 . 10003 2004 . 10003 2005 . 10003 2006 . 10003 2007 . 10003 2008 . 10003 2009 . 10003 2010 . 10003 2011 . 10003 2012 . 10003 2013 . 10003 2014 . 10003 2015 . 10003 2016 . 10003 2017 . end
0 Response to Counting distinct values across loops
Post a Comment