Dear all,
I am working with balanced panel data and have some trouble with loops. The data tracks individuals over time (years and quarters). The task is to fill/fix missing/negative values in the educational attainment variable.
Below I provide a sample of my data with only 3 individuals and 4 years (original data consist on 8000 individuals and 17 years for each). The negative values in the variable "grade" for id=1 and id=2 can be easily fixed (to 11 and 10, respectively). I am particularly interested in id=3 : there is nothing I can do with the negative values since it is clearly a data inconsistency. So, I don’t want the code to change the negative values for id=3. Please note, I am not displaying the quarter because it is irrelevant for this particular task.
I have :
The code runs but does not do anything. It seems it is able to do well in the first loop (identifies the year with the first value to fix), but it is not able to work in the second part of the code. I know while tells stata to look only on the first observation but I can' find a different way to get what I want.
I appreciate your help.
Celia
I am working with balanced panel data and have some trouble with loops. The data tracks individuals over time (years and quarters). The task is to fill/fix missing/negative values in the educational attainment variable.
Below I provide a sample of my data with only 3 individuals and 4 years (original data consist on 8000 individuals and 17 years for each). The negative values in the variable "grade" for id=1 and id=2 can be easily fixed (to 11 and 10, respectively). I am particularly interested in id=3 : there is nothing I can do with the negative values since it is clearly a data inconsistency. So, I don’t want the code to change the negative values for id=3. Please note, I am not displaying the quarter because it is irrelevant for this particular task.
Code:
clear input byte id int year float grade 1 1997 10 1 1997 10 1 1997 10 1 1997 10 1 1998 -5 1 1998 -5 1 1998 -5 1 1998 -5 1 1999 12 1 1999 12 1 1999 12 1 1999 12 1 2000 13 1 2000 13 1 2000 13 1 2000 13 2 1997 8 2 1997 8 2 1997 8 2 1997 8 2 1998 9 2 1998 9 2 1998 9 2 1998 9 2 1999 -5 2 1999 -5 2 1999 -5 2 1999 -5 2 2000 11 2 2000 11 2 2000 11 2 2000 11 3 1997 10 3 1997 10 3 1997 10 3 1997 10 3 1998 -5 3 1998 -5 3 1998 -5 3 1998 -5 3 1999 13 3 1999 13 3 1999 13 3 1999 13 3 2000 14 3 2000 14 3 2000 14 3 2000 14 end
Code:
forvalues ii = 1/3 { // opens loop 1 local x= 1997 while grade>0 & grade!=. & id==`ii' & year==`x' { // opens loop 2 ( loops until it finds the first year with a negative value in education local x = `x' + 1 } // closes loop 2 forvalues j = `x'(1)2000 { // opens loop 3 (starting with year x, the looks for values to fix while (grade<0 | grade==.) & year==`j' & id==`ii'{ local i = 4 while grade[_n+`i'] <0 | grade[_n+`i']==. { //loops until if finds a valid value (positive value) in the upcoming years local i = `i'+ 4 } replace grade= grade[_n-4] +1 if year[_n+`i’]-year[_n-4]==grade[_n+`i’]-grade[_n-4]) // this condition restrains the code from making changes for id=3 } } // closes loop 3 } // closes loop 1
I appreciate your help.
Celia
Comment