Hi everyone,

I had a question about writing a double loop. Below is my data for reference. In my data, I have a student identifier called id. I have a variable called sem which indicates the semester that a student first enrolled in school. The variable sem has four possible values. A 5 represents the Fall semester. A 2 represents the Spring semester. A 3 represents Summer Session 1. A four represents Summer Session 2. The other variable is called year and it represents the calendar year that the student enrolled in the school.


Code:
* Example generated by -dataex-. To install: ssc install dataex
clear
input float(id sem year)
 1 5 2011
 2 2 2012
 3 3 2012
 4 4 2012
 5 5 2012
 6 2 2013
 7 3 2013
 8 4 2013
 9 5 2013
10 2 2014
11 3 2014
12 4 2014
13 5 2014
14 2 2015
15 3 2015
16 4 2015
17 5 2015
18 2 2016
19 3 2016
20 4 2016
21 5 2016
22 2 2017
23 3 2017
24 4 2017
25 5 2017
26 2 2018
27 3 2018
28 4 2018
29 5 2018
30 2 2019
31 3 2019
32 4 2019
33 5 2019
34 2 2020
35 3 2020
36 4 2020
end
I am trying to identify the academic year that a student enrolled in the school. An academic year runs from Fall to Summer Session 2. Thus a student who enrolled in the first academic year may have enrolled in either:
  1. Fall 2011
  2. Spring 2012
  3. Summer Session 1 2012
  4. Summer Session 2 2012
For individuals enrolled in the Fall semester of a particular calendar year, I can successfully identify the academic year that they enrolled using the following loop:


Code:
*Generate the academic year variable
gen academic_year_enroll=.

*Create for all Fall semesters from 2011-2019
*i indexes for both academic year and calendar year since they are the same
foreach i of num 1/9 {
    replace academic_year_enroll = `i' if year==201`i' & sem==5
    }

For individuals enrolled in the Spring semester of a particular calendar year, I cannot successfully identify the academic year that they enrolled in. I am using the following loop:


Code:
*All Spring semesters from 2012-2020
*i should index the academic year
*j should index the calendar year
*sem is equal to 2 because I am looking for the Spring semester.
foreach i of num 1/9 {
    foreach j of num 12/20 {
    replace cademic_year_enroll =`i' if year==20`j' & sem==2
        }
}
Instead, I manually wrote out what I wanted
Code:
replace academic_year_enroll = 1 if year==2012 & sem==2
replace academic_year_enroll = 2 if year==2013 & sem==2
replace academic_year_enroll = 3 if year==2014 & sem==2
replace academic_year_enroll = 4 if year==2015 & sem==2
replace academic_year_enroll = 5 if year==2016 & sem==2
replace academic_year_enroll = 6 if year==2017 & sem==2
replace academic_year_enroll = 7 if year==2018 & sem==2
replace academic_year_enroll = 8 if year==2019 & sem==2
replace academic_year_enroll = 9 if year==2020 & sem==2
Can I ask someone for help in fixing my loop? Manually writing it out invites mistakes and I want to learn how to address this issue. I am just trying to improve my coding and feedback is highly appreciated. Thanks in advance