Hi all, I'm currently having the same codes for multiple years (2000-2007), and I was wondering if there is a way to loop all years together instead of typing it out year-by-year?

For example, this is my current code:
Code:
* Year 2000 *
use "/Users/kyereen/merge.dta"
gen yr=year(date)
rename yr year
keep if year == 2000
drop if permco == .
foreach v of varlist exalus-exvzus{
 rename `v' u_`v'
 }
gen long seq =_n
reshape long u_, i(seq) j(exc) string
rename exc country_pair_exchange
rename u_ exchange_rates
drop if exchange_rates == .
cd ~/stata
rangestat (reg) ret sprtrn exchange_rates, by(permco year country_pair_exchange) interval(year . .)
drop reg_r2 reg_adj_r2 se_sprtrn se_exchange_rates se_cons ret sprtrn exchange_rates reg_nobs b_sprtrn b_cons seq
collapse b_exchange_rates, by(permco year country_pair_exchange)
save "/Users/kyereen/test2000.dta", replace

* Year 2001 *
use "/Users/kyereen/merge.dta"
gen yr=year(date)
rename yr year
keep if year == 2001
drop if permco == .
foreach v of varlist exalus-exvzus{
 rename `v' u_`v'
 }
gen long seq =_n
reshape long u_, i(seq) j(exc) string
rename exc country_pair_exchange
rename u_ exchange_rates
drop if exchange_rates == .
cd ~/stata
rangestat (reg) ret sprtrn exchange_rates, by(permco year country_pair_exchange) interval(year . .)
drop reg_r2 reg_adj_r2 se_sprtrn se_exchange_rates se_cons ret sprtrn exchange_rates reg_nobs b_sprtrn b_cons seq
collapse b_exchange_rates, by(permco year country_pair_exchange)
save "/Users/kyereen/test2001.dta", replace

* Year 2002 *
use "/Users/kyereen/merge.dta"
gen yr=year(date)
rename yr year
keep if year == 2002
drop if permco == .
foreach v of varlist exalus-exvzus{
 rename `v' u_`v'
 }
gen long seq =_n
reshape long u_, i(seq) j(exc) string
rename exc country_pair_exchange
rename u_ exchange_rates
drop if exchange_rates == .
cd ~/stata
rangestat (reg) ret sprtrn exchange_rates, by(permco year country_pair_exchange) interval(year . .)
drop reg_r2 reg_adj_r2 se_sprtrn se_exchange_rates se_cons ret sprtrn exchange_rates reg_nobs b_sprtrn b_cons seq
collapse b_exchange_rates, by(permco year country_pair_exchange)
save "/Users/kyereen/test2002.dta", replace

* Year 2003, 2004, ... 2007 *
Thank you.