Dear statalist users,
I'm wondering if it's possible to write a more efficient iterator in these three cases.
case 1.
gen making = 20190 if month==1 & year == 2019
forvalues i=2/9 {
replace making = 20190 if month==`i' & year==2019
}
forvalues i=1/9 {
replace making = 20200 if month==`i' & year==2020
}
forvalues i=1/9 {
replace making = 20210 if month==`i' & year==2021
}
forvalues i=1/6 {
replace making = 20220 if month==`i' & year==2022
}
forvalues i=10/12 {
replace making = year if month==`i'
}
case 2.
gen year_month = making*10 + month if month== 1 & year == 2019
forvalues i=2/9 {
replace year_month = making*10 + month if month==`i' & year==2019
}
forvalues i=1/9 {
replace year_month = making*10 + month if month==`i' & year==2020
}
forvalues i=1/9 {
replace year_month = making*10 + month if month==`i' & year==2021
}
forvalues i=1/6 {
replace year_month = making*10 + month if month==`i' & year==2022
}
forvalues i=10/12 {
replace year_month = making*100 + month if month==`i'
}
case 3.
gen quarter = 1 if month == 1 |month == 2| month == 3
replacequarter = 2 if month == 4 |month == 5| month == 6
replacequarter = 3 if month == 7 |month == 8| month == 9
replacequarter = 4 if month == 10|month == 11| month == 12
thank you!
I'm wondering if it's possible to write a more efficient iterator in these three cases.
case 1.
gen making = 20190 if month==1 & year == 2019
forvalues i=2/9 {
replace making = 20190 if month==`i' & year==2019
}
forvalues i=1/9 {
replace making = 20200 if month==`i' & year==2020
}
forvalues i=1/9 {
replace making = 20210 if month==`i' & year==2021
}
forvalues i=1/6 {
replace making = 20220 if month==`i' & year==2022
}
forvalues i=10/12 {
replace making = year if month==`i'
}
case 2.
gen year_month = making*10 + month if month== 1 & year == 2019
forvalues i=2/9 {
replace year_month = making*10 + month if month==`i' & year==2019
}
forvalues i=1/9 {
replace year_month = making*10 + month if month==`i' & year==2020
}
forvalues i=1/9 {
replace year_month = making*10 + month if month==`i' & year==2021
}
forvalues i=1/6 {
replace year_month = making*10 + month if month==`i' & year==2022
}
forvalues i=10/12 {
replace year_month = making*100 + month if month==`i'
}
case 3.
gen quarter = 1 if month == 1 |month == 2| month == 3
replacequarter = 2 if month == 4 |month == 5| month == 6
replacequarter = 3 if month == 7 |month == 8| month == 9
replacequarter = 4 if month == 10|month == 11| month == 12
thank you!
Comment