Number of Grades Repeated or Skipped - Appendix 1

National Longitudinal Survey of Youth - 1997 Cohort

Number of Grades Repeated or Skipped - Appendix 1

These variables were last created in round 11.

Variables Created

  • CV_GRADES_REPEAT_EVER (NUMREP)
  • CV_GRADES_REPEAT_YR (NUMREPj)
  • CV_GRADE_SKIPPED_EVER (NUMSKIP)
  • CV_GRADE_SKIPPED_YR (NUMSKIPj)
  • CVC_GRADES_REPEAT_EVER_2007 (NUMRP_CVC)
  • CVC_GRADE_SKIPPED_EVER_2007 (NUMSK_CVC)

This program creates six variables relating to grade progression in school. The first four listed above are single-round variables, providing information about grades repeated/skipped since the date of last interview and ever. The final two are cross-round variables, created for all respondents regardless of round 11 interview status, showing the number of grades ever repeated or skipped.

Variables Used

/**Variable names in program       Variable names in Gator**/

   e3500r1                         YSCH-3500
   e3900r1                         YSCH-3900
   e4000r1                         YSCH-4000
   e5000r1                         YSCH-5000
   intmr1                          CV_INTERVIEW_DATE 1997
   intyr1                          CV_INTERVIEW_DATE 1997
   NUMREPr1                        CV_GRADES_REPEAT_EVER
   NUMSKIPr1                       CV_GRADE_SKIPPED_EVER
   HGCr1_symbol                    COMPLETED
   e3104r2                         YSCH-3104
   e3061r2                         YSCH-3061
   intmr2                          CV_INTERVIEW_DATE 1998
   intyr2                          CV_INTERVIEW_DATE 1998
   NUMREPr2                        CV_GRADES_REPEAT_EVER
   NUMREPjr2                       CV_GRADES_REPEAT_YR
   NUMSKIPr2                       CV_GRADE_SKIPPED_EVER
   NUMSKIPjr2                      CV_GRADE_SKIPPED_YR
   e3104r3                         YSCH-3104
   e3061r3                         YSCH-3061
   intmr3                          CV_INTERVIEW_DATE 1999
   intyr3                          CV_INTERVIEW_DATE 1999
   NUMREPr3                        CV_GRADES_REPEAT_EVER
   NUMREPjr3                       CV_GRADES_REPEAT_YR
   NUMSKIPr3                       CV_GRADE_SKIPPED_EVER
   NUMSKIPjr3                      CV_GRADE_SKIPPED_YR
   e3104r4                         YSCH-3104
   e3061r4                         YSCH-3061
   intmr4                          CV_INTERVIEW_DATE 2000
   intyr4                          CV_INTERVIEW_DATE 2000
   NUMREPr4                        CV_GRADES_REPEAT_EVER
   NUMREPjr4                       CV_GRADES_REPEAT_YR
   NUMSKIPr4                       CV_GRADE_SKIPPED_EVER
   NUMSKIPjr4                      CV_GRADE_SKIPPED_YR
   e3104r5                         YSCH-3104
   e3061r5                         YSCH-3061
   intmr5                          CV_INTERVIEW_DATE 2001
   intyr5                          CV_INTERVIEW_DATE 2001
   NUMREPr5                        CV_GRADES_REPEAT_EVER
   NUMREPjr5                       CV_GRADES_REPEAT_YR
   NUMSKIPr5                       CV_GRADE_SKIPPED_EVER
   NUMSKIPjr5                      CV_GRADE_SKIPPED_YR
   e3104r6                         YSCH-3104
   e3061r6                         YSCH-3061
   intmr6                          CV_INTERVIEW_DATE 2002
   intyr6                          CV_INTERVIEW_DATE 2002
   NUMREPr6                        CV_GRADES_REPEAT_EVER
   NUMREPjr6                       CV_GRADES_REPEAT_YR
   NUMSKIPr6                       CV_GRADE_SKIPPED_EVER
   NUMSKIPjr6                      CV_GRADE_SKIPPED_YR
   e3104r7                         YSCH-3104
   e3061r7                         YSCH-3061
   intmr7                          CV_INTERVIEW_DATE 2003
   intyr7                          CV_INTERVIEW_DATE 2003
   NUMREPr7                        CV_GRADES_REPEAT_EVER
   NUMREPjr7                       CV_GRADES_REPEAT_YR
   NUMSKIPr7                       CV_GRADE_SKIPPED_EVER
   NUMSKIPjr7                      CV_GRADE_SKIPPED_YR
   e3104r8                         YSCH-3104
   e3061r8                         YSCH-3061
   intmr8                          CV_INTERVIEW_DATE 2004
   intyr8                          CV_INTERVIEW_DATE 2004
   NUMREPr8                        CV_GRADES_REPEAT_EVER
   NUMREPjr8                       CV_GRADES_REPEAT_YR
   NUMSKIPr8                       CV_GRADE_SKIPPED_EVER
   NUMSKIPjr8                      CV_GRADE_SKIPPED_YR
   e3104r9                         YSCH-3104
   e3061r9                         YSCH-3061
   intmr9                          CV_INTERVIEW_DATE 2005
   intyr9                          CV_INTERVIEW_DATE 2005
   NUMREPr9                        CV_GRADES_REPEAT_EVER
   NUMREPjr9                       CV_GRADES_REPEAT_YR
   NUMSKIPr9                       CV_GRADE_SKIPPED_EVER
   NUMSKIPjr9                      CV_GRADE_SKIPPED_YR
   numrpr10_cv                     CV_GRADES_REPEAT_EVER_2006
   numrpr10_yr                     CV_GRADES_REPEAT_YR_2006
   numskr10_cv                     CV_GRADE_SKIPPED_EVER_2006
   numskr10_yr                     CV_GRADE_SKIPPED_YR_2006
   e3061r10                        YSCH-3061_2006
   numrpR10_cvc                    CVC_GRADES_REPEAT_EVER_2006_XRND
   numskR10_cvc                    CVC_GRADE_SKIPPED_EVER_2006_XRND
   e3103r11                        YSCH-3103_2007
   e3104r11                        YSCH-3104_2007
   e2857r11                        YSCH-2857_2007
   e3010r11                        YSCH-3010_2007
   e3061r11                        YSCH-3061_2007
   e3112r11                        YSCH-3112_2007
   e3113r11                        YSCH-3113_2007
   e3878r11                        YSCH-3878_2007
   e8416r11_11                     YSCH-8416.01.01_2007
   e8416r11_21                     YSCH-8416.02.01_2007
   e8416r11_31                     YSCH-8416.03.01_2007
   e8722r11_11                     YSCH-8722.01.01_2007
   e8722r11_21                     YSCH-8722.02.01_2007
   e8722r11_31                     YSCH-8722.03.01_2007
   e8774Mr11_11                    YSCH-8774.01.01~M_2007
   e8774Yr11_11                    YSCH-8774.01.01~Y_2007
   e8977r11_11                     YSCH-8977.01.01_2007
   e8977r11_31                     YSCH-8977.03.01_2007
   e9131r11_11                     YSCH-9131.01.01_2007
   e9131r11_21                     YSCH-9131.02.01_2007
   e9131r11_31                     YSCH-9131.03.01_2007
   e9335Mr11_11                    YSCH-9335.01.01~M_2007
   e9335Yr11_11                    YSCH-9335.01.01~Y_2007
   e9335Mr11_21                    YSCH-9335.02.01~M_2007
   e9335Yr11_21                    YSCH-9335.02.01~Y_2007
   e9335Mr11_31                    YSCH-9335.03.01~M_2007
   e9335Yr11_31                    YSCH-9335.03.01~Y_2007
   e9589r11_111                    YSCH-9589.01.01.01_2007
   e9589r11_112                    YSCH-9589.01.01.02_2007
   e9589r11_113                    YSCH-9589.01.01.03_2007
   e9589r11_211                    YSCH-9589.02.01.01_2007
   e9589r11_311                    YSCH-9589.03.01.01_2007
   e9589r11_312                    YSCH-9589.03.01.02_2007
   e9589r11_313                    YSCH-9589.03.01.03_2007
   e9691Mr11_111                   YSCH-9691.01.01.01~M_2007
   e9691Yr11_111                   YSCH-9691.01.01.01~Y_2007
   e9691Mr11_112                   YSCH-9691.01.01.02~M_2007
   e9691Yr11_112                   YSCH-9691.01.01.02~Y_2007
   e9691Mr11_113                   YSCH-9691.01.01.03~M_2007
   e9691Yr11_113                   YSCH-9691.01.01.03~Y_2007
   e9691Mr11_211                   YSCH-9691.02.01.01~M_2007
   e9691Yr11_211                   YSCH-9691.02.01.01~Y_2007
   e9691Mr11_311                   YSCH-9691.03.01.01~M_2007
   e9691Yr11_311                   YSCH-9691.03.01.01~Y_2007
   e9691Mr11_312                   YSCH-9691.03.01.02~M_2007
   e9691Yr11_312                   YSCH-9691.03.01.02~Y_2007
   e9691Mr11_313                   YSCH-9691.03.01.03~M_2007
   e9691Yr11_313                   YSCH-9691.03.01.03~Y_2007
   e9946r11_111                    YSCH-9946.01.01.01_2007
   e9946r11_112                    YSCH-9946.01.01.02_2007
   e9946r11_113                    YSCH-9946.01.01.03_2007
   e9946r11_211                    YSCH-9946.02.01.01_2007
   e9946r11_311                    YSCH-9946.03.01.01_2007
   e9946r11_312                    YSCH-9946.03.01.02_2007
   e9946r11_313                    YSCH-9946.03.01.03_2007
   e10099Mr11_111                  YSCH-10099.01.01.01~M_2007
   e10099Yr11_111                  YSCH-10099.01.01.01~Y_2007
   e10099Mr11_112                  YSCH-10099.01.01.02~M_2007
   e10099Yr11_112                  YSCH-10099.01.01.02~Y_2007
   e10099Mr11_113                  YSCH-10099.01.01.03~M_2007
   e10099Yr11_113                  YSCH-10099.01.01.03~Y_2007
   e10099Mr11_211                  YSCH-10099.02.01.01~M_2007
   e10099Yr11_211                  YSCH-10099.02.01.01~Y_2007
   e10099Mr11_311                  YSCH-10099.03.01.01~M_2007
   e10099Yr11_311                  YSCH-10099.03.01.01~Y_2007
   e10099Mr11_312                  YSCH-10099.03.01.02~M_2007
   e10099Yr11_312                  YSCH-10099.03.01.02~Y_2007
   e10099Mr11_313                  YSCH-10099.03.01.03~M_2007
   e10099Yr11_313                  YSCH-10099.03.01.03~Y_2007
   pubid                           pubid_2007

SAS Code for Variable Creation

 **   Intermediate variables created in current round:                                           **
 **   -----------------------------------------------------                                           **
 **     ENROLL               enrolled at date of interview in current round -- created                **
 **                                                                                                   **
 **   Intermediate variables created in previous round:                                          **
 **   ------------------------------------------------------                                          **
 **     r10bfrpev             Total number of grades repeated ever up to previous round                **
 **     r10bfskev             Total number of grades skipped ever up to previous round                 **
 **     r10bf2857             HGA reported up to previous round                                        **
 **     r10bfint              The most recent interview date up to previous round (in continuous       **
 **                           month scheme)                                                            **
 **     r10bfat1-r10bfat15     The grade attended up to previous round in order                         **
 **     r10bfst1-r10bfst15     The starting date for each grade attended up to previous round (in       **
 **                            continuous month scheme)                                                 **
 **     r10bfed1-r10bfed15     The ending date for each grade attended up to previous round (in         **
 **                            continuous month scheme)                                                 **
 **     r10bfcp1-r10bfcp15     The dummy that each grade is completed or not up to previous round       **

data one; set xin11.in_num_grdR11;
if pubid=1625 then do;
  numrpr10_cvc=-3;
  numrpr10_cv=-3;
  numrpr10_yr=-3;
end;

** Enroll or not at date of interview for the current round;
data two; set xin11.out_hgcR11;
keep pubid ENROLL ENCAT GRSURV DEGSURV encatP grsurvP HSD GED hsr1
            e3500r1 e5000r1 e2857r11 e3103r11 e3104r11 e3010r11 e3112r11 e3878r11 e3061r11
            doicmr: scoder11:   strtmr11:   strtyr11:   
            stopmr11:   stopyr11:;                    
proc sort; by pubid;

** The data set created from the previous round program;
data thr; set xin.allgrr10_corr;
array vars10 r10bfat1-r10bfat15 r10bfst1-r10bfst15 r10bfed1-r10bfed15
            r10bfcp1-r10bfcp15 r10bfrpev r10bfskev r10bf2857 r10bfint;
array varsP PRbfat1-PRbfat15 PRbfst1-PRbfst15 PRbfed1-PRbfed15
            PRbfcp1-PRbfcp15 PRbfrpev PRbfskev PRbf2857 PRbfint;

do i=1 to dim(varsP);
   varsP(i)=vars10(i);
end;

array varsP1 (*) PRbfat1-PRbfat15;
array varsP2 (*) PRbfcp1-PRbfcp15;

PRHGA=-5; PRHGC=-5;
do i=1 to dim(varsP1);
   if varsP1(i)>PRHGA then PRHGA=varsP1(i);
   if varsP1(i)>PRHGC & varsP2(i)=1 then PRHGC=varsP1(i);
end;

drop r10:;
data thr; set thr; proc sort; by pubid;

data merged; merge one two thr; by pubid;
PRbfrpev=numrpr10_cvc;
PRbfskev=numskr10_cvc;

***** PROGRAM STARTS;

if e3500r1>=0 then HGAr1=e3500r1;
if e4000r1>=0 then HGAr1=e4000r1;
if e3104r2>-1 then HGAr1=e3104r2;

if e5000r1>=0         then HGCr1=e5000r1;
if HGCr1_symbol>=0    then HGCr1=HGCr1_symbol;

if e3061r2>=0 then HGCr1=e3061r2;      
if e3061r2=-5 then do;
   if e3061r3>=0 then HGCr1=e3061r3;
   if e3061r3=-5 then do;
      if e3061r4>=0 then HGCr1=e3061r4;
      if e3061r4=-5 then do;
         if e3061r5>=0 then HGCr1=e3061r5;
         if e3061r5=-5 then do;
            if e3061r6>=0 then HGCr1=e3061r6;
            if e3061r6=-5 then do;
               if e3061r7>=0 then HGCr1=e3061r7;
               if e3061r7=-5 then do;
                  if e3061r8>=0 then HGCr1=e3061r8;
                  if e3061r8=-5 then do;
                     if e3061r9>=0 then HGCr1=e3061r9;
                     if e3061r9=-5 then do;
                        if e3061r10>=0 then HGCr1=e3061r10;
                        if e3061r10=-5 then do;
                           if e3061r11>=0 then HGCr1=e3061r11;
                        end;
                     end;
                  end;
               end;
            end;
         end;
      end;
   end;
end;            
if hsr1>-4 & hgcr1<12 then HGCr1=12;

** Interview dates in cumulative months;
doicm=doicmr11;
   
** Create new array of start dates for first enrollment in a school depending on whether R corrected
   roster data;

array strtm  (*) strtmr11_11  strtmr11_12  strtmr11_13  strtmr11_14  strtmr11_15 ; * Start mo from roster;
array strty  (*) strtyr11_11  strtyr11_12  strtyr11_13  strtyr11_14  strtyr11_15 ; * Start yr from roster;
array e8722  (*) e8722r11_11  e8722r11_21  e8722r11_31  e8722r11_41  e8722r11_51 ; * Verify start date;
array e8774m (*) e8774mr11_11 e8774mr11_21 e8774mr11_31 e8774mr11_41 e8774mr11_51; * Corrected start mo;
array e8774y (*) e8774yr11_11 e8774yr11_21 e8774yr11_31 e8774yr11_41 e8774yr11_51; * Corrected start yr;
array startm (*) start1m     start2m     start3m     start4m     start5m    ; * Correct start mo, created;
array starty (*) start1y     start2y     start3y     start4y     start5y    ; * Correct start yr, created;

numstart=0;
do i=1 to dim(strtm);
   if e8774m(i)>0 & e8774y(i)>0 then do;
      startm(i)=e8774m(i);
      starty(i)=e8774y(i);
   end;
   else if e8774m(i)<=0|e8774y(i)<=0|e8722(i)=1 then do;
      startm(i)=strtm(i);
      starty(i)=strty(i);
      numstart=numstart+1;
   end;
end;

** Create arrays of grades attended, whether they were completed, and start and completion dates;
 
* Grade attended;
array gr_attd (*) e8416r11_11   e8416r11_21     e8416r11_31
                  e9589r11_111  e9589r11_112    e9589r11_113
                  e9589r11_211  
                  e9589r11_311  e9589r11_312    e9589r11_313;

* Completed - yes/no;
array gr_comp (*) e9131r11_11   e9131r11_21    e9131r11_31
                  e9946r11_111  e9946r11_112    e9946r11_113
                  e9946r11_211  
                  e9946r11_311  e9946r11_312    e9946r11_313;

* Date of grade enrollment/start - month;
array sdate_m (*) start1m      start2m       start3m
                  e9691mr11_111 e9691mr11_112   e9691mr11_113  
                  e9691mr11_211
                  e9691mr11_311 e9691mr11_312   e9691mr11_313;

* Date of grade enrollment/start - year;
array sdate_y (*) start1y      start2y       start3y
                  e9691yr11_111 e9691yr11_112   e9691yr11_113
                  e9691yr11_211
                  e9691yr11_311 e9691yr11_312   e9691yr11_313;

* Date of grade completion - month;
array cdate_m (*) e9335mr11_11   e9335mr11_21   e9335mr11_31
                  e10099mr11_111 e10099mr11_112 e10099mr11_113
                  e10099mr11_211
                  e10099mr11_311 e10099mr11_312 e10099mr11_313;

* Date of grade completion - year;
array cdate_y (*) e9335yr11_11   e9335yr11_21   e9335yr11_31
                  e10099yr11_111 e10099yr11_112 e10099yr11_113
                  e10099yr11_211
                  e10099yr11_311 e10099yr11_312 e10099yr11_313;

** Create arrays of all grades attended and completed and their start and complete dates;
   
* Assign GRA the number of elements in the new grades array to be created.;
GRA=9;

* Grade attended;
array attd (9) attd1-attd9;

* Grade completed;
array comp (9) comp1-comp9;

* Date of grade enrollment/start - month;
array stmo (9) stmo1-stmo9;

* Date of grade enrollment/start - year;
array styr (9) styr1-styr9;

* Date of grade completion - month;
array cpmo (9) cpmo1-cpmo9;

* Date of grade completion - year;
array cpyr (9) cpyr1-cpyr9;

** Set all new array vars to -4;
do i=1 to GRA;
   stmo(i)=-4;
   styr(i)=-4;
   cpmo(i)=-4;
   cpyr(i)=-4;
   comp(i)=-4;
   attd(i)=-4;
end;

** Record all grades completed and their completion dates;
j=1;
do i= 1 to dim(gr_attd);
   if gr_comp(i)=1 & gr_attd(i) not in (-4,-5) then do;
      comp(j)=gr_attd(i);
      cpmo(j)=cdate_m(i);
      cpyr(j)=cdate_y(i);
      j=j+1;
   end;
end;

** Record all grades attended and their start dates;
k=1;
flag1=0;
if e9131r11_11 not in (-4,-5) then do;
   if e8416r11_11 not in (-4,-5) then do;
      attd(k)=e8416r11_11;
      if start1m>0 then stmo(k)=start1m;
      if start1y>0 then styr(k)=start1y;
      if e8774mr11_11=-4 & e8774yr11_11=-4 & strtmr11_11=-4 & strtyr11_11=-4 then flag1=1;
      k=k+1;
   end;
   do i=2 to dim(gr_attd);
      if gr_comp(i)~=-4 & gr_comp(i)~=. then do;
         attd(k)=gr_attd(i);
         stmo(k)=sdate_m(i);
         styr(k)=sdate_y(i);
         k=k+1;
      end;
   end;
end;

** Create date in continuous months for each start and complete date in arrays;

* Date of grade completion in continuous month scheme;
array cpcm (9) cpcm1-cpcm9;

* Date of grade enrollment/start in continuous month scheme;
array stcm (9) stcm1-stcm9;

do i=1 to GRA;
   if cpyr(i)>0 & cpmo(i)>0 then cpcm(i)=((cpyr(i)-1980)*12)+cpmo(i);
   else if cpyr(i) in (-1, -2, -3)|cpmo(i) in (-1, -2, -3) then cpcm(i)=-3;
   else if cpyr(i)=-4|cpmo(i)=-4 then cpcm(i)=-4;
   if styr(i)>0 & stmo(i)>0 then stcm(i)=((styr(i)-1980)*12)+stmo(i);
   else if styr(i) in (-1, -2, -3)|stmo(i) in (-1, -2, -3)  then stcm(i)=-3;
   else if styr(i)=-4|stmo(i)=-4 then stcm(i)=-4;
end;

** Create new arrays of started and completed grades that have grades listed only once and in order
   - report latest complete date;

* Completed grade and date of grade completion when grades have been listed only once and in order;
array NCOMP (9) NCOMP1-NCOMP9;
array NCDAT (9) NCDAT1-NCDAT9;

NCOMP1=comp1;
NCDAT1=cpcm1;
reorder=0;
   
** First make sure that the first element in the array is the lowest grade and the latest date for that
   grade;

do i=2 to GRA;
   if comp(i)>0 then do;
      if comp(i)=NCOMP1 & cpcm(i)>NCDAT1 then NCDAT1=cpcm(i);
      else if comp(i)<NCOMP1 then do;
         NCOMP1=comp(i);
         NCDAT1=cpcm(i);
         reorder=reorder+1;
      end;
   end;
end;

do j=2 to GRA;
   do i=1 to GRA;
      if NCOMP(j-1)~=. then do;
         if comp(i)=NCOMP(j-1) & cpcm(i)>NCDAT(j-1) then NCDAT(j-1)=cpcm(i);
         else if comp(i)=NCOMP(j-1)+1 then do;
            NCOMP(j)=comp(i);
            NCDAT(j)=cpcm(i);
         end;
         else if NCOMP(j)=. & comp(i)>NCOMP(j-1) then do;
            NCOMP(j)=comp(i);
            NCDAT(j)=cpcm(i);
            reorder=reorder+1;
         end;
      end;
   end;
end;
   
** Create a new array with all grades attended in order and an array with the earliest date each grade
   was started;

* Intermediate variables;
array tmpattd (9) tmpattd1-tmpattd9;
array tmpadat (9) tmpadat1-tmpadat9;

* Grades attended and the start dates for enrollment in these grades in proper order;
array NATTD (9) NATTD1-NATTD9;
array NADAT (9) NADAT1-NADAT9;

do i=1 to GRA;
   tmpattd(i)=attd(i);
   tmpadat(i)=stcm(i);
end;

i=1;
k=1;
do i=1 to GRA;
   do j=i+1 to GRA;
      if tmpattd(j)~=-4 then do;
         if 0<tmpattd(j)<tmpattd(i) then do;
            att_temp=tmpattd(i);
            dat_temp=tmpadat(i);
            tmpattd(i)=tmpattd(j);
            tmpadat(i)=tmpadat(j);
            tmpattd(j)=att_temp;
            tmpadat(j)=dat_temp;
            reorder=reorder+1;
         end;
      end;
   end;
   if i=1 then do;
      NATTD(k)=tmpattd(i);
      NADAT(k)=tmpadat(i);
   end;
   if tmpattd(i)=NATTD(k) then do;
      if (0<tmpadat(i)<NADAT(k))|(NADAT(k)=-4 & tmpadat(i)>0) then do;
         NADAT(k)=tmpadat(i);
      end;
      else if tmpadat(i) in (-1,-2,-3)| NADAT(k) in (-1,-2,-3) then NADAT(k)=-3;
   end;
   else if tmpattd(i)>NATTD(k) then do;
      k=k+1;
      NATTD(k)=tmpattd(i);
      NADAT(k)=tmpadat(i);
   end;
end;
      
** Compute the last enrollment date - stopdate;
   
* School stop date - month;
array stopm (*) stopmr11_11 stopmr11_21
                stopmr11_12
                stopmr11_13 stopmr11_23
                stopmr11_14
                stopmr11_15;

* School stop date - year;
array stopy (*) stopyr11_11 stopyr11_21
                stopyr11_12
                stopyr11_13 stopyr11_23
                stopyr11_14
                stopyr11_15;

* School stop date in continuous month scheme;
array stopd (*) stopd11 stopd21
                stopd12
                stopd13 stopd23
                stopd14
                stopd15;

* School code;
array scode (*) scoder11_1 scoder11_1
                scoder11_2
                scoder11_3 scoder11_3
                scoder11_4
                scoder11_5;

do i=1 to dim(stopm);
   if stopy(i)>0 & stopm(i)>0 then stopd(i)=((stopy(i)-1980)*12)+stopm(i);
   else if stopy(i)=-5 then stopd(i)=-5;
   else if stopy(i) in (-1,-2,-3)|stopm(i) in (-1,-2,-3) then stopd(i)=-3;
end;

if ENROLL=0 then do;
   if scoder11_1 in (1,2,3) then stopdate=stopd11;
   do i=2 to dim(stopm);
      if scode(i) in (1,2,3) & stopd(i)>stopdate then stopdate=stopd(i);
   end;
end;
else if ENROLL=1 then stopdate=-4;

** Compute end dates for attended grades. Find the end date for each grade attended
   - if R doesn't give a completion date then:
   (a) choose the month before they started the next grade,
   (b) if there is not another grade and currently enrolled then take the interview date,
   (c) if there is not another grade and not currently enrolled then take the last stop date - stopdate,
   (d) if the enrollment date for the grade is the last interview date then take the next june.;

** Date of completion/stop attending a grade;
array NEDAT (9) NEDAT1-NEDAT9;

** Flags of grade completion;
array ISCOM (9) ISCOM1-ISCOM9;

do i=1 to GRA;
   if NATTD(i)~=. then ISCOM(i)=0;
end;

endimp=0;

do i=1 to GRA;
   do j=1 to GRA;
      if NATTD(i)=NCOMP(j) & NATTD(i)>0 then do;
         NEDAT(i)=NCDAT(j);
         ISCOM(i)=1;
      end;
   end;
   if NATTD(i)~=. & NEDAT(i)=. & NADAT(i)~=PRbfint & NADAT(i)~=NADAT(i+1) & NADAT(i+1)~=. then do;
      NEDAT(i)=NADAT(i+1)-1;
      endimp=1;
   end;
   else if NATTD(i)~=. & NEDAT(i)=. & NATTD(i+1)=. & ENROLL=1 then do;
      NEDAT(i)=doicm;
      endimp=2;
   end;
   else if NATTD(i)~=. & NEDAT(i)=. & NATTD(i+1)=. & ENROLL=0 then do;
      NEDAT(i)=stopdate;
      endimp=3;
   end;
   else if NEDAT(i)=. & NADAT(i)=PRbfint then do;
      endimp=4;
      if NADAT(i)<=210 then NEDAT(i)=210;
      else if 211<=NADAT(i)<=222 then NEDAT(i)=222;
      else if 223<=NADAT(i)<=234 then NEDAT(i)=234;
      else if 235<=NADAT(i)<=246 then NEDAT(i)=246;
      else if 247<=NADAT(i)<=258 then NEDAT(i)=258;
      else if 259<=NADAT(i)<=270 then NEDAT(i)=270;
      else if 271<=NADAT(i)<=282 then NEDAT(i)=282;
      else if 283<=NADAT(i)<=294 then NEDAT(i)=294;
      else if 295<=NADAT(i)<=306 then NEDAT(i)=306;
      else if 307<=NADAT(i)<=318 then NEDAT(i)=318;
      else if 319<=NADAT(i)<=330 then NEDAT(i)=330;
      else if 331<=NADAT(i)<=342 then NEDAT(i)=342;
      else if 343<=NADAT(i)<=354 then NEDAT(i)=354;
   end;
end;

** Delete the grades whose information is repeatedly reported in the current round: If a grade is
   reported in the previous rounds and was completed, we don't repeat this grade's info. in this round;
   
* Assign GRB the number of elements in this array of grades.;
GRB=15;

* The grade attended up to the previous round in order;
array PRbfat (15) PRbfat1-PRbfat15;

* Enrollment dates for grade attended up to the previous round in order;
array PRbfst (15) PRbfst1-PRbfst15;

* Stop dates for grade attended up to the previous round in order;
array PRbfed (15) PRbfed1-PRbfed15;

* Flag to indicate whether the grade was completed or not for grades attended up to the previous round
  in order;
array PRbfcp (15) PRbfcp1-PRbfcp15;

* Intermediate variables;
array tmp2att (9) tmp2att1-tmp2att9;

do i=1 to GRA;
   tmp2att(i)=NATTD(i);
end;

backcut=0;
ncut=0;
backrep=0;

do i=1 to GRA;
   do j=1 to GRB;
      if 0<tmp2att(i)<13 & tmp2att(i)=PRbfat(j) & PRbfcp(j)=1 then do;
         j=GRB;
         backcut=1;
         ncut=ncut+1;
         do k=i+1-ncut to GRA-ncut;
            if NATTD(k+1)~=. then do;
               NATTD(k)=NATTD(k+1);
               NADAT(k)=NADAT(k+1);
               NEDAT(k)=NEDAT(k+1);
               ISCOM(k)=ISCOM(k+1);
            end;
            else do;
               NATTD(k)=.;
               NADAT(k)=.;
               NEDAT(k)=.;
               ISCOM(k)=0;
               k=GRA-ncut;
            end;
         end;
      end;
   end;
end;

if 0<NATTD1<13 then do;
   do i=1 to GRB;
      if PRbfat(i)>=NATTD1 then do;
         if PRbfat(i+1)~=. then backrep=1;
         if PRbfcp(i)=1 then backrep=2;
      end;
   end;
end;

** Flag the case where the reported grades are jumping;

badpre=0;
replace=0;
jumprep1=0;
jumprep2=0;
jumpge21=0;
jumpge22=0;

if PRbfat1~=. then do;
   do i=1 to (GRB-1);
      if PRbfat(i)~=. & PRbfat(i+1)=. then do;
         lphigh=PRbfat(i);
         lphighst=PRbfst(i);
         lphighed=PRbfed(i);
         lphighcp=PRbfcp(i);
         lpnum=i;
         i=GRB;
      end;
   end;
end;
else lphigh=-5;

if 0<NATTD1<13 then do;
   if (lphigh~=-5 & ((NATTD1=lphigh+1 & lphighcp~=1)|NATTD1>lphigh+1)) then jumprep1=1;
   if (lphigh=-5 & ((HGCr1>0 & NATTD1>=HGCr1+2)|(HGAr1>0 & NATTD1>=HGAr1+2))) then jumprep2=1;
   if (lphigh~=-5 & NATTD1>=lphigh+2) then jumpge21=1;
   if (lphigh=-5 & HGCr1>0 & NATTD1>=HGCr1+3) then jumpge22=1;
end;

** Substitute start dates in the previous round for first grade in loops;

if 0<NATTD1<13 & PRbfat1~=. then do;
   do i=1 to (GRB-1);
      if (PRbfst(i)>PRbfed(i) & PRbfed(i)>0)
      or (PRbfed(i)>PRbfst(i+1) & PRbfst(i+1)>0)
      or (PRbfst(i+1)>PRbfed(i+1) & PRbfed(i+1)>0) then do;
         NADAT1=-3;
         badpre=1;
      end;
      else if PRbfat(i)=NATTD1 & PRbfat(i+1)=. & PRbfcp(i)=0 & badpre~=1 then do;
         NADAT1=PRbfst(i);
         replace=1;
      end;
   end;
end;
else if -4<NATTD1<=0 then NADAT1=-3;

***** CREATE THE FINAL VARIABLES;

** Measure the length that a respondent was in each completed grade and count the number of grades
   skipped and repeated;

array lengtha (9) length1-length9;

REPEAT=0;
REPEATj=0;
skipleng=0;
skipl_j=0;
SKIP=-16;
SKIPj=-16;
oldstrt=0;
nocomp=0;

do i=1 to (GRA-1);
   if 0<NATTD(i)<13 & NEDAT(i)>0 & NADAT(i)>0 then lengtha(i)=NEDAT(i)-NADAT(i);
   if 0<NATTD(i)<13 & NADAT(i)<=NEDAT(i)<=NADAT(i+1) & lengtha(i)>13
   & NATTD(i+1)-NATTD(i)=1 & REPEAT~=-3 then do;
      REPEAT=REPEAT+1;
      if i=1 & NADAT(i)<PRbfint then oldstrt=1;
      if NEDAT(i)<=330 then REPEATj=REPEATj+1;
   end;
   else if 0<NATTD(i)<13 & NADAT(i)<=NEDAT(i) & NATTD(i+1)=. & lengtha(i)>13
   & REPEAT~=-3 & (ISCOM(i)=1|e2857r11=NATTD(i)) then do;
      REPEAT=REPEAT+1;
      if i=1 & NADAT(i)<PRbfint then oldstrt=1;
      if NEDAT(i)<=330 then REPEATj=REPEATj+1;
   end;
   else if 0<NATTD(i)<13 & NADAT(i)<=NEDAT(i) & NATTD(i+1)=. & lengtha(i)>13
   & REPEAT~=-3 & (ISCOM(i)=0 & e2857r11~=NATTD(i)) then do;
      if i=1 & NADAT(i)<PRbfint then oldstrt=1;
      REPEAT=-3;
      REPEATj=-3;
      reason=1;
   end;
end;

do i=1 to (GRA-2);
   if 0<NATTD(i)<13 & NADAT(i)<NEDAT(i)<NADAT(i+1)<NEDAT(i+1) then do;
      if NADAT(i) in (212,213,224,225,236,237,248,249,260,261,272,273,284,285,296,297,308,309,320,321,332,333,344,345)
      & NADAT(i)~=PRbfint & NATTD(i+1)-NATTD(i)=1 & ISCOM(i+1)=1
      & NEDAT(i+1)-NADAT(i)<=12 & NADAT(i+2)-NEDAT(i+1)<=9 then do;
         if (i=1|(i~=1 & NADAT(i)-NEDAT(i-1)<=9)) & skipleng~=-3 then do;
            skipleng=skipleng+1;
            if NEDAT(i+1)<=330 & skipl_j~=-3 then skipl_j=skipl_j+1;
         end;
      end;
   end;
end;

** Take care of connection between previous round and current round;
connect=0;
if 0<NATTD1<13 & lphigh>0 & NATTD1~=lphigh then do;
   if NATTD1-lphigh>1 then connect=1;
   if lphighst in (212,213,224,225,236,237,248,249,260,261,272,273,284,285,296,297,308,309,320,321,332,333,344,345)
   & NEDAT1>=lphighst & NEDAT1-lphighst<=12 then connect=2;
end;
else if 0<NATTD1<13 & lphigh>0 & NATTD1=lphigh & ISCOM1=1 & lphighcp~=1
& PRbfst(lpnum)>0 then do;
   if PRbfst(lpnum) in (212,213,224,225,236,237,248,249,260,261,272,273,284,285,296,297,308,309,320,321,332,333,344,345)
   & NEDAT1>=PRbfst(lpnum) & NEDAT1-PRbfst(lpnum)<=12 then connect=3;
end;

** Check for respondents who completely skip (do not attend) a grade;
skipgr=0;
skipgrj=0;
skipgt2=0;

do i=1 to (GRA-1);
   if 0<NATTD(i)<13 & NATTD(i+1)-NATTD(i)=2 & NEDAT(i)>0 & NADAT(i+1)>0
   & NADAT(i+1)-NEDAT(i)<=9 & skipgr~=-3 then do;
      skipgr=skipgr+1;
      if NEDAT(i+1)<=330 then skipgrj=skipgrj+1;
   end;
   else if 0<NATTD(i)<13 & NATTD(i+1)-NATTD(i)=3 & NEDAT(i)>0 & NADAT(i+1)>0
   & NADAT(i+1)-NEDAT(i)<=9 then do;
      skipgr=skipgr+2;
      if NEDAT(i+1)<=330 then skipgrj=skipgrj+2;
   end;
   else if 0<NATTD(i)<13 & NATTD(i+1)-NATTD(i)>3 & NEDAT(i)>0 & NADAT(i+1)>0
   & NADAT(i+1)-NEDAT(i)<=9 then do;
      skipgt2=1;
   end;
end;

** Set REPEAT and SKIP to -3 if allmess=1 or backrep>0 or jumprep=1 or zerograd=1 or negdate=1;
allmess=0;
do i=1 to (GRA-1);
   if 0<NATTD(i)<13 & (
     (NADAT(i)>NEDAT(i) & NEDAT(i)>0)
    |(NEDAT(i)>NADAT(i+1) & NADAT(i+1)>0)
    |(NADAT(i+1)>NEDAT(i+1) & NEDAT(i+1)>0)
    ) then do;
      allmess=1;
   end;
end;

zerograd=0;
do i=1 to GRA;
   if NATTD(i)=0 then zerogrd=1;
end;

negdate=0;
do i=1 to GRA;
   if 0<NATTD(i)<13 & (NEDAT(i) in (-1,-2,-3)|NADAT(i) in (-1,-2,-3)) then negdate=1;
end;

if allmess=1|backrep>0|zerograd=1|jumprep1=1|jumprep2=1|negdate=1 then do;
   REPEAT=-3;
   REPEATj=-3;
   SKIP=-3;
   SKIPj=-3;
end;

if allmess=1  then reason=2.1;
if backrep>0  then reason=2.2;
if zerograd=1 then reason=2.3;
if jumprep1=1 then reason=2.4;
if jumprep2=1 then reason=2.5;
if negdate=1  then reason=2.6;

** Flag respondents who reported skipping a grade and those who say they did not skip grade;
array whyskip (*) e8977r11;

skipques=0;
do i=1 to dim(whyskip);
   if whyskip(i)=1 then skipques=1;
end;

notskip=0;
do i=1 to dim(whyskip);
   if whyskip(i) in (-1,-2,2,999) then notskip=1;
end;

double=0;
if skipgr>0 & skipleng>0 then double=1;

if SKIP~=-3 then do;
   if (skipques=0 & skipleng=0 & skipgr=0)|(notskip>=1 & skipques=0) then SKIP=0;
   else if skipleng=-3 & (skipgr=0|skipques=0) then do;
        SKIP=-3;
        reason=3;
   end;
   else if skipgr=-3 & skipleng=0 then do;
        SKIP=-3;
        reason=4;
   end;
   else if skipleng>=1 then SKIP=skipleng;
   else if skipques>=1 & skipgr>=1 then SKIP=skipgr;
   
   else if skipques>=1 & skipgr=0 then do;
        SKIP=-3;
        reason=5;
   end;
   else if skipques=0 & skipgr>=1 then do;
        SKIP=-3;
        reason=6;
   end;
   if skipques=-3|skipgr=-3|skipleng=-3 then do;
      SKIP=-3;
      reason=7;
   end;
end;

if SKIPj~=-3 then do;
   if (skipl_j=0 & skipgrj=0)|(notskip>=1 & skipques=0) then SKIPj=0;
   else if skipl_j=-3 & (skipgrj=0|skipques=0) then do;
        SKIPj=-3;
        reason=8;
   end;
   else if skipgrj=-3 & skipl_j=0 then do;
        SKIPj=-3;
        reason=9;
   end;
   else if skipl_j>=1 then SKIPj=skipl_j;
   else if skipques>=1 & skipgrj>=1 then SKIPj=skipgrj;
   
   else if skipques>=1 & skipgrj=0 then do;
      SKIPj=-3;
      reason=10;
   end;
   else if skipques=0 & skipgrj>=1 then do;
      SKIPj=-3;
      reason=11;
   end;
   if SKIP=-3 then do;
      SKIPj=-3;
      reason=12;
   end;
end;

** Take care of the cases whose "repeated grade" could be double counted;

if oldstrt=1 & REPEAT>0 & PRbfrpev>0 & PRbf2857~=-5 then do;
   do i=1 to (GRB-1);
      if 0<NATTD1<13 & PRbfat(i)=NATTD1 & PRbfat(i+1)=. & (PRbfed(i)-PRbfst(i))>13 &
      (PRbfcp(i)=1|PRbf2857=PRbfat(i)) then do;
         REPEAT=REPEAT-1;
         if NEDAT1<=330 then REPEATj=REPEATj-1;
         double_repeat=1;
      end;
   end;
end;

* Combine previous and current round information.;
if PRbfrpev>=0 & REPEAT>=0 then NUMREP=PRbfrpev+REPEAT;
else if PRbfrpev<0 then NUMREP=PRbfrpev;
else if PRbfrpev>=0 & REPEAT<0 then NUMREP=REPEAT;

if PRbfrpev>=0 & REPEATj>=0 then NUMREPj=PRbfrpev+REPEATj;
else if PRbfrpev<0 then NUMREPj=PRbfrpev;
else if PRbfrpev>=0 & REPEATj<0 then NUMREPj=REPEATj;

if PRbfskev>=0 & SKIP>=0 then NUMSKIP=PRbfskev+SKIP;
else if PRbfskev<0 then NUMSKIP=PRbfskev;
else if PRbfskev>=0 & SKIP<0 then NUMSKIP=SKIP;

if PRbfskev>=0 & SKIPj>=0 then NUMSKIPj=PRbfskev+SKIPj;
else if PRbfskev<0 then NUMSKIPj=PRbfskev;
else if PRbfskev>=0 & SKIPj<0 then NUMSKIPj=SKIPj;

if NUMREP in (-1,-2) then NUMREP=-3;
if NUMREPj in (-1,-2) then NUMREPj=-3;
if NUMSKIP in (-1,-2) then NUMSKIP=-3;
if NUMSKIPj in (-1,-2) then NUMSKIPj=-3;

***** HE;
if pubid=8443 then do;
 numrep=-3;
 numrepj=-3;
 reason=99;
end;

dontbother=1;
if NUMREP   not in (-3,-4) | NUMSKIP  not in (-3,-4)
 | PRbfrpev not in (-3,-4) | PRbfskev not in (-3,-4) then dontbother=0;
numsk_cvc=numskip;
numrp_cvc=numrep;

if e3104r11=-5 then do;
   NUMSKIP=-5;
   NUMREP=-5;
   NUMSKIPj=-5;
   NUMREPj=-5;
   numsk_cvc=numskr10_cvc;
   numrp_cvc=numrpr10_cvc;
end;
***** GENERATE THE DATA SET containing the carry on variables for the current round - CRbf....;
data carryon; set merged;

** For single variables;
CRbfrpev=NUMREP;
if e3104r11=-5 then CRbfrpev=PRbfrpev;

CRbfskev=NUMSKIP;
if e3104r11=-5 then CRbfskev=PRbfskev;

CRbf2857=e2857r11;
if e3104r11=-5 then CRbf2857=PRbf2857;

CRbfint=doicm;
if e3104r11=-5 then CRbfint=PRbfint;

** For array variables;
array alattd (*) PRbfat1-PRbfat15 NATTD1-NATTD9;
array alstdt (*) PRbfst1-PRbfst15 NADAT1-NADAT9;
array aleddt (*) PRbfed1-PRbfed15 NEDAT1-NEDAT9;
array alcomp (*) PRbfcp1-PRbfcp15 ISCOM1-ISCOM9;

array CRbfat (*) CRbfat1-CRbfat15;
array CRbfst (*) CRbfst1-CRbfst15;
array CRbfed (*) CRbfed1-CRbfed15;
array CRbfcp (*) CRbfcp1-CRbfcp15;

array PRtpat (*) PRtpat1-PRtpat15;
array PRtpst (*) PRtpst1-PRtpst15;
array PRtped (*) PRtped1-PRtped15;
array PRtpcp (*) PRtpcp1-PRtpcp15;

GRC=(GRA+GRB);
j=0;
do i=1 to GRC;
   if alattd[i] not in (-4, -5, .) then do;
      j=j+1;
      CRbfat[j]=alattd[i];
      CRbfst[j]=alstdt[i];
      CRbfed[j]=aleddt[i];
      CRbfcp[j]=alcomp[i];
   end;
end;

do i=1 to GRB;
   PRtpat[i]=CRbfat[i];
   PRtpst[i]=CRbfst[i];
   PRtped[i]=CRbfed[i];
   PRtpcp[i]=CRbfcp[i];
end;

**  If two grades are same with the same start date, we delete the info. with earlier stopping date;
**  We also delete the 0 grade info;

cut=0;
numcut=0;
samegrd=0;
do i=1 to (GRB-1);
   if (0<PRtpat[i]<13 & PRtpat[i]=PRtpat[i+1] & PRtpst[i]=PRtpst[i+1] & PRtped[i]<=PRtped[i+1]
   & (PRtpst[i]~=-3|PRtpst[i+1]~=-3))|PRtpat[i]=0 then do;
      cut=1;
      numcut=numcut+1;
      do k=i+1-numcut to GRB-numcut;
         CRbfat[k]=CRbfat[k+1];
         CRbfst[k]=CRbfst[k+1];
         CRbfed[k]=CRbfed[k+1];
         CRbfcp[k]=CRbfcp[k+1];
      end;
   end;
   else if 0<PRtpat[i]<13 & PRtpat[i]=PRtpat[i+1] & PRtpst[i]=PRtpst[i+1] & PRtped[i]>=PRtped[i+1]
   & (PRtpst[i]~=-3|PRtpst[i+1]~=-3) then do;
      cut=2;
      numcut=numcut+1;
      do k=i+2-numcut to GRB-numcut;
         CRbfat[k]=CRbfat[k+1];
         CRbfst[k]=CRbfst[k+1];
         CRbfed[k]=CRbfed[k+1];
         CRbfcp[k]=CRbfcp[k+1];
      end;
   end;
   else if 0<PRtpat[i]<13 & PRtpat[i]=PRtpat[i+1] & PRtpst[i]~=PRtpst[i+1] then samegrd=1;
end;

** Flag the special cases;
strtneg=0; endneg=0;
do i=1 to GRB;
   if 0<CRbfat[i]<13 & CRbfst[i]=-3 then strtneg=1;
   if 0<CRbfat[i]<13 & CRbfed[i]=-3 then endneg=1;
   if 12<CRbfat[i]<95 then clg=1;
end;

neg3or4=0;
if (CRbfrpev in (-1,-2,-3) & CRbfskev in (-1,-2,-3))|(CRbfrpev=-4 & CRbfskev=-4) then neg3or4=1;

gapbteq=0;
do i=1 to (GRB-2);
   do k=i+2 to GRB;
     if 0<CRbfat[i]<13 & CRbfat[i]=CRbfat[k] then gapbteq=1;
   end;
end;

gapbteqPR=0;
do i=1 to (GRB-2);
   do k=i+2 to GRB;
     if 0<alattd[i]<13 & alattd[i]=alattd[k] then gapbteqPR=1;
   end;
end;

gapbteqCR=0;
do i=(GRB+1) to (GRC-2);
   do j=i+2 to GRC;
      if 0<alattd(i)<13 & alattd(i)=alattd(j) then gapbteqCR=1;
   end;
end;

strtmis=0; endmis=0;
do i=1 to GRB;
  if 0<CRbfat[i]<13 & CRbfst[i] in (-4,.) then strtmis=1;
  if 0<CRbfat[i]<13 & CRbfed[i] in (-4,.) then endmis=1;
end;

array vars11 r11bfrpev r11bfskev r11bf2857 r11bfint
            r11bfat1-r11bfat15  r11bfat1-r11bfat15
            r11bfst1-r11bfst15  r11bfst1-r11bfst15
            r11bfed1-r11bfed15  r11bfed1-r11bfed15
            r11bfcp1-r11bfcp15  r11bfcp1-r11bfcp15;
array varsC CRbfrpev CRbfskev CRbf2857 CRbfint
            CRbfat1-CRbfat15  CRbfat1-CRbfat15
            CRbfst1-CRbfst15  CRbfst1-CRbfst15
            CRbfed1-CRbfed15  CRbfed1-CRbfed15
            CRbfcp1-CRbfcp15  CRbfcp1-CRbfcp15;
do over varsC;
   vars11=varsC;
end;

endsas;