Hi! I need to edit a number of files and am looking to create a loop to do so. There are two changes I need to do to each dataset:
1. Remove variable prefix: each dataset has a letter prefix for all variables. The first dataset (w1_indresp.dta) has a, the second dataset (w2_indresp.dta) has b and so on...
2. Generate a year variable: each dataset needs a year variable. The first dataset (w1_indresp.dta) needs year 1991, the second dataset (w2_indresp.dta) needs 1992and so on...
The code below is the "non-loop" version for the first two datasets.
use "/w1_indresp.dta"
renpfix a
gen year = 1991
duplicates report pid
save "w1_indresp_edit.dta", replace
use "w2_indresp.dta"
renpfix b
gen year = 1992
duplicates report pid
save "w2_indresp_edit.dta", replace
How can I create a loop that runs over both letters and numbers? I appreciate any help :-)
1. Remove variable prefix: each dataset has a letter prefix for all variables. The first dataset (w1_indresp.dta) has a, the second dataset (w2_indresp.dta) has b and so on...
2. Generate a year variable: each dataset needs a year variable. The first dataset (w1_indresp.dta) needs year 1991, the second dataset (w2_indresp.dta) needs 1992and so on...
The code below is the "non-loop" version for the first two datasets.
use "/w1_indresp.dta"
renpfix a
gen year = 1991
duplicates report pid
save "w1_indresp_edit.dta", replace
use "w2_indresp.dta"
renpfix b
gen year = 1992
duplicates report pid
save "w2_indresp_edit.dta", replace
How can I create a loop that runs over both letters and numbers? I appreciate any help :-)
Comment