Dear all,
I have been trying to find a way to use 'foreach' or 'forvalues' loop to generate agegroups. I have a survey dataset including and individual age variable. Now I would like to create agegroups in steps of 4 (<1, 1-4, 5-9, 10-14, ....., 95-99, >100). The way I am able to do it is continuing as shown below, but as there are more tasks like this I need to do in my analysis it would be very helpful to be able to do it a shorter (and less error prone) way. I tried to use 'forvalues' and give the ages in steps, but I cannot find a solution yet.
Eventually I will need to attach a specific growth rate to each age group which I will try to do in a further loop, or, if possible, in the same one. (That is if it is possible to attach different values to each agegroup in a loop, I imagine I would need to enter each value and then the loop would be redundant.)
Any help or suggestion would be very appreciated!
Thank you very much!
gen agegroup=.
replace agegroup=1 if age<1
replace agegroup=2 if age>=1 & age<=4
replace agegroup=3 if age>=5 & age<=9
replace agegroup=4 if age>=10 & age<=14
replace agegroup=5 if age>=15 & age<=19
...
forvalues age = 1(1)4 5(1)9 10(1)14 15(1)19 20(1)24 25(1)29 30(1)34 35(1)39 {
gen agegroup
}
I have been trying to find a way to use 'foreach' or 'forvalues' loop to generate agegroups. I have a survey dataset including and individual age variable. Now I would like to create agegroups in steps of 4 (<1, 1-4, 5-9, 10-14, ....., 95-99, >100). The way I am able to do it is continuing as shown below, but as there are more tasks like this I need to do in my analysis it would be very helpful to be able to do it a shorter (and less error prone) way. I tried to use 'forvalues' and give the ages in steps, but I cannot find a solution yet.
Eventually I will need to attach a specific growth rate to each age group which I will try to do in a further loop, or, if possible, in the same one. (That is if it is possible to attach different values to each agegroup in a loop, I imagine I would need to enter each value and then the loop would be redundant.)
Any help or suggestion would be very appreciated!
Thank you very much!
gen agegroup=.
replace agegroup=1 if age<1
replace agegroup=2 if age>=1 & age<=4
replace agegroup=3 if age>=5 & age<=9
replace agegroup=4 if age>=10 & age<=14
replace agegroup=5 if age>=15 & age<=19
...
forvalues age = 1(1)4 5(1)9 10(1)14 15(1)19 20(1)24 25(1)29 30(1)34 35(1)39 {
gen agegroup
}
Comment