Statalist The Stata Listserver


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: programming: stopping loops?


From   "Claire M. Kamp Dush" <[email protected]>
To   [email protected]
Subject   st: programming: stopping loops?
Date   Wed, 23 Aug 2006 13:52:12 -0400

Hello, I feel embarrassed to post this because I am sure the answer to this is obvious, but I have been puzzling over this issue for a few hours. I am trying to recode the family structure data in the NLSY 79 through 2004. I am trying to go back and recode the data for missing years based on reports of marital changes between interviews at follow-ups. For instance, if an individual was interviewed in 1991 and not in 1992, in 1993 they are asked to report up to 3 marital changes since the last time they were interviewed. My data is stacked, with each individual having 26 lines of data, for years 1979 through 2004. The id variable is momid and the year variable is year. change1type, change2type, and change3type are measured each year where the respondent has data, and is a categorical variable with categories including married, divorced, separated, widowed, etc. changey1_ , changey2_, and changey3_ are the years in which the each change is said to occur. Here is an example of what the data look like:

momid year change1type changey1_ change2type changey2_
1 2000 Married 2000
1 2001
1 2002 Separated 2001 Divorced 2001
1 2003
1 2004
2 1988 Married 1987
2 1989
2 1990
2 1991
2 1992
2 1993
2 1994
2 1995
2 1996 Divorced 1993
2 1997
2 1998
2 1999
2 2000 Remarried 1998

My goal is to have my data look like the following:

momid year change1type changey1_ change2type changey2_ change1misstype change2misstype
1 2000 Married 2000 Married
1 2001 Separated Divorced
1 2002 Separated 2001 Divorced 2001
1 2003
1 2004
2 1987 Married
2 1988 Married 1987
2 1989
2 1990
2 1991 Divorced 1991 Divorced Remarried
2 1992 Remarried 1991
2 1993 Divorced
2 1994
2 1995
2 1996 Divorced 1993
2 1997
2 1998 Remarried
2 1999
2 2000 Remarried 1998


My first step in doing this was to create a series of variables, change1misstype through change6misstype (note that I have through change6misstype because there can be up to six changes reported in a given year). I wrote the following two programs to cycle through my data, replacing these variables. However, my problem is that I want it to stop replacing once one of my six variables takes on the value of change1type for a given year (or change2type or change3type). I don't know how to stop it at that point. I am very very new to programming, and to Stata, so I am sure that there is a better way I could be doing this. Do I need to use else? I was trying to figure this out, but any advice would be amazing.

Sincerely,
Claire Kamp Dush



**Program 1

global w = 1979

prog define recodechange
while $w < 2005 {
by momid: replace change1misstype = change1type if year == $w & year == changey1_ & change1type != . & change1misstype == .
by momid: replace change1misstype = change2type if year == $w & year == changey2_ & change2type != . & change1misstype == .
by momid: replace change1misstype = change3type if year == $w & year == changey3_ & change3type != . & change1misstype == .
by momid: replace change2misstype = change1type if year == $w & year == changey1_ & change1type != . & change1misstype != . & change2misstype == .
by momid: replace change2misstype = change2type if year == $w & year == changey2_ & change2type != . & change1misstype != . & change2misstype == .
by momid: replace change2misstype = change3type if year == $w & year == changey3_ & change3type != . & change1misstype != . & change2misstype == .
by momid: replace change3misstype = change1type if year == $w & year == changey1_ & change1type != . & change1misstype != . & change2misstype != . & change3misstype == .
by momid: replace change3misstype = change2type if year == $w & year == changey2_ & change2type != . & change1misstype != . & change2misstype != . & change3misstype == .
by momid: replace change3misstype = change3type if year == $w & year == changey3_ & change3type != . & change1misstype != . & change2misstype != . & change3misstype == .
by momid: replace change4misstype = change1type if year == $w & year == changey1_ & change1type != . & change1misstype != . & change2misstype != . & change3misstype != . & change4misstype == .
by momid: replace change4misstype = change2type if year == $w & year == changey2_ & change2type != . & change1misstype != . & change2misstype != . & change3misstype != . & change4misstype == .
by momid: replace change4misstype = change3type if year == $w & year == changey3_ & change3type != . & change1misstype != . & change2misstype != . & change3misstype != . & change4misstype == .
by momid: replace change5misstype = change1type if year == $w & year == changey1_ & change1type != . & change1misstype != . & change2misstype != . & change3misstype != . & change4misstype != . & change5misstype == .
by momid: replace change5misstype = change2type if year == $w & year == changey2_ & change2type != . & change1misstype != . & change2misstype != . & change3misstype != . & change4misstype != . & change5misstype == .
by momid: replace change5misstype = change3type if year == $w & year == changey3_ & change3type != . & change1misstype != . & change2misstype != . & change3misstype != . & change4misstype != . & change5misstype == .
by momid: replace change6misstype = change1type if year == $w & year == changey1_ & change1type != . & change1misstype != . & change2misstype != . & change3misstype != . & change4misstype != . & change5misstype != . & change6misstype == .
by momid: replace change6misstype = change2type if year == $w & year == changey2_ & change2type != . & change1misstype != . & change2misstype != . & change3misstype != . & change4misstype != . & change5misstype != . & change6misstype == .
by momid: replace change6misstype = change3type if year == $w & year == changey3_ & change3type != . & change1misstype != . & change2misstype != . & change3misstype != . & change4misstype != . & change5misstype != . & change6misstype == .
global w = $w + 1
}
end
recodechange



**Program 2

global w = 1979
global q = 1
global z = 26

prog define recodechange2
while $w < 2005 {
while $q < $z {
by momid: replace change1misstype = change1type[_n+$q] if year == $w & year == changey1_[_n+$q] & change1type[_n+$q] != . & change1misstype == .
by momid: replace change1misstype = change2type[_n+$q] if year == $w & year == changey2_[_n+$q] & change2type[_n+$q] != . & change1misstype == .
by momid: replace change1misstype = change3type[_n+$q] if year == $w & year == changey3_[_n+$q] & change3type[_n+$q] != . & change1misstype == .
by momid: replace change2misstype = change1type[_n+$q] if year == $w & year == changey1_[_n+$q] & change1type[_n+$q] != . & change1misstype != . & change2misstype == .
by momid: replace change2misstype = change2type[_n+$q] if year == $w & year == changey2_[_n+$q] & change2type[_n+$q] != . & change1misstype != . & change2misstype == .
by momid: replace change2misstype = change3type[_n+$q] if year == $w & year == changey3_[_n+$q] & change3type[_n+$q] != . & change1misstype != . & change2misstype == .
by momid: replace change3misstype = change1type[_n+$q] if year == $w & year == changey1_[_n+$q] & change1type[_n+$q] != . & change1misstype != . & change2misstype != . & change3misstype == .
by momid: replace change3misstype = change2type[_n+$q] if year == $w & year == changey2_[_n+$q] & change2type[_n+$q] != . & change1misstype != . & change2misstype != . & change3misstype == .
by momid: replace change3misstype = change3type[_n+$q] if year == $w & year == changey3_[_n+$q] & change3type[_n+$q] != . & change1misstype != . & change2misstype != . & change3misstype == .
by momid: replace change4misstype = change1type[_n+$q] if year == $w & year == changey1_[_n+$q] & change1type[_n+$q] != . & change1misstype != . & change2misstype != . & change3misstype != . & change4misstype == .
by momid: replace change4misstype = change2type[_n+$q] if year == $w & year == changey2_[_n+$q] & change2type[_n+$q] != . & change1misstype != . & change2misstype != . & change3misstype != . & change4misstype == .
by momid: replace change4misstype = change3type[_n+$q] if year == $w & year == changey3_[_n+$q] & change3type[_n+$q] != . & change1misstype != . & change2misstype != . & change3misstype != . & change4misstype == .
by momid: replace change5misstype = change1type[_n+$q] if year == $w & year == changey1_[_n+$q] & change1type[_n+$q] != . & change1misstype != . & change2misstype != . & change3misstype != . & change4misstype != . & change5misstype == .
by momid: replace change5misstype = change2type[_n+$q] if year == $w & year == changey2_[_n+$q] & change2type[_n+$q] != . & change1misstype != . & change2misstype != . & change3misstype != . & change4misstype != . & change5misstype == .
by momid: replace change5misstype = change3type[_n+$q] if year == $w & year == changey3_[_n+$q] & change3type[_n+$q] != . & change1misstype != . & change2misstype != . & change3misstype != . & change4misstype != . & change5misstype == .
by momid: replace change6misstype = change1type[_n+$q] if year == $w & year == changey1_[_n+$q] & change1type[_n+$q] != . & change1misstype != . & change2misstype != . & change3misstype != . & change4misstype != . & change5misstype != . & change6misstype == .
by momid: replace change6misstype = change2type[_n+$q] if year == $w & year == changey2_[_n+$q] & change2type[_n+$q] != . & change1misstype != . & change2misstype != . & change3misstype != . & change4misstype != . & change5misstype != . & change6misstype == .
by momid: replace change6misstype = change3type[_n+$q] if year == $w & year == changey3_[_n+$q] & change3type[_n+$q] != . & change1misstype != . & change2misstype != . & change3misstype != . & change4misstype != . & change5misstype != . & change6misstype == .
global q = $q + 1
}
global z = $z - 1
global w = $w + 1
}
end
recodechange2





Claire M. Kamp Dush, Ph.D.
Postdoctoral Fellow, Evolving Family Theme Project
Cornell University
Bronfenbrenner Life Course Center
Bebee Hall
Ithaca, NY 14853
607-255-9908
http://www.socialsciences.cornell.edu/0407/evolv_fam_desc.html

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