Dear all,
I am using a dataset in which I have information on events (from 1972 - 2000) and information on the day of birth of the respondent. I created an age variable using: gen age=(td(1jan2017)-gebdat)/365.25. So age represents the age in year 2017.
It is organized as follows:
ID 1972 1973 1974 AGE
1 1 2 4 44.000
2 0 0 2 33.199
3 1 2 2 22.677
4 12 4 0 55.998
So, person 1 participated in one event in 1972, in two event in 1973 etc
However, I want to create new variables, in which the events correspond to age. So, I want to end up with:
ID event_age12 event_age13 event_age14 event_age15
1
2
3
4
So, if a person scores 2 at an event in 1973 and his/her DOB is between 1 jan 1961 and 31 dec 1961 (or age = 56), he/she should have 2 at variable event_age12
I tried to generate variables using:
gen age_12
tab 1972
replace age_12 = 1 if (1972==1)
replace age_12 = 2 if (1972==2)
replace age_12 = 0 if (age>=57) & (1972>=1)
replace age_12 = 0 if (age<56) & (1972>=1)
But, if I do it like this, I have to do the same thing for every age from 12 till 70 AND every event year.... So, therefore I tried to use loops.
However, I cannot figure out how to combine age and event in one loop, without making event a dummy variable.
For example I tried to do something like this:
foreach eventyear of varlist 'eventyear' { generate ...
(eventyear as cutpoints?)
As you can see, I don't know how to solve this issue. Should I use a loop inside a loop?
I would very much appreciate it if you could help me figure this out, every step in the right direction is welcome!
Thanks a lot in advance!
Best, Lara
I am using a dataset in which I have information on events (from 1972 - 2000) and information on the day of birth of the respondent. I created an age variable using: gen age=(td(1jan2017)-gebdat)/365.25. So age represents the age in year 2017.
It is organized as follows:
ID 1972 1973 1974 AGE
1 1 2 4 44.000
2 0 0 2 33.199
3 1 2 2 22.677
4 12 4 0 55.998
So, person 1 participated in one event in 1972, in two event in 1973 etc
However, I want to create new variables, in which the events correspond to age. So, I want to end up with:
ID event_age12 event_age13 event_age14 event_age15
1
2
3
4
So, if a person scores 2 at an event in 1973 and his/her DOB is between 1 jan 1961 and 31 dec 1961 (or age = 56), he/she should have 2 at variable event_age12
I tried to generate variables using:
gen age_12
tab 1972
replace age_12 = 1 if (1972==1)
replace age_12 = 2 if (1972==2)
replace age_12 = 0 if (age>=57) & (1972>=1)
replace age_12 = 0 if (age<56) & (1972>=1)
But, if I do it like this, I have to do the same thing for every age from 12 till 70 AND every event year.... So, therefore I tried to use loops.
However, I cannot figure out how to combine age and event in one loop, without making event a dummy variable.
For example I tried to do something like this:
foreach eventyear of varlist 'eventyear' { generate ...
(eventyear as cutpoints?)
As you can see, I don't know how to solve this issue. Should I use a loop inside a loop?
I would very much appreciate it if you could help me figure this out, every step in the right direction is welcome!
Thanks a lot in advance!
Best, Lara
Comment