I have a dataset with unique IDs and date of births, and I would like to calculate their age from future birthdays in specific years (for example, 2018 and 2019). I understand how to calculate age between two dates, but am having trouble with creating the dates of the future birthdays in the first place. In other words, I'd like to take the month and day of their birthday, and append to a specified year.
Have:
ID_1 DOB
A2543 08OCT1993
A3667 12JUL2003
A3624 30MAR2000
Want:
ID_1 DOB bday_2018 bday_2019 age_2018 age_2019
A2543 08OCT1993 08OCT2018 08OCT2019 26 27
A3667 12JUL2003 12JUL2018 12JUL2019 15 16
A3624 30MAR2000 30MAR2018 30MAR2019 18 19
You're making this way more difficult than it needs to be. If you want to use the same day and month, you don't have to use month and day. You need one of these:
age_2018 = 2018 - year(dob);
Looking at the calculations you have already made, you may be looking at a variation:
age_2018 = 2018 + 1 - year(dob);
But I would expect the first formula is the correct one.
Thanks for you response. The reason I am interested in extracting the specific month and day is that is I have another variable (date of enrollment), and I'd like to find their age at this date.
For A2543 in my example, they are 18 after their birthday of 08OCT2018 and before their next birthday 08OCT2019. If their enrollment date was on 01JUN2019, they would be 18 on this day. Your solution would yield age_2019 of 19, which would not be what I want.
Apologies for not including this piece in my original post.
@gabagotati wrote:
Thanks for you response. The reason I am interested in extracting the specific month and day is that is I have another variable (date of enrollment), and I'd like to find their age at this date.
For A2543 in my example, they are 18 after their birthday of 08OCT2018 and before their next birthday 08OCT2019. If their enrollment date was on 01JUN2019, they would be 18 on this day. Your solution would yield age_2019 of 19, which would not be what I want.
Apologies for not including this piece in my original post.
If you have another DATE valued variable then the YRDIF function may be what you want:
ageatdate = floor( yrdif(dob,enrollmentdate) );
This will return a decimal portion. So you may want to use the FLOOR function to remove the decimal bit:
I think you have chosen a poor layout of the data to do this. A better layout would be
Want:
ID_1 DOB year age
A2543 08OCT1993 2018 26
A2543 08OCT1993 2019 27
A3667 12JUL2003 2018 15
A3667 12JUL2003 2019 16
@gabagotati wrote:
Thanks for you response. The reason I am interested in extracting the specific month and day is that is I have another variable (date of enrollment), and I'd like to find their age at this date.
For A2543 in my example, they are 18 after their birthday of 08OCT2018 and before their next birthday 08OCT2019. If their enrollment date was on 01JUN2019, they would be 18 on this day. Your solution would yield age_2019 of 19, which would not be what I want.
Apologies for not including this piece in my original post.
However, to address the problem you now state, I make up some data and address the solution.
data have;
input ID $ DOB :date9. date_of_enrollment :date9.;
cards;
A2543 08OCT1993 01JUN2019
A3667 12JUL2003 12JUL2020
A3624 30MAR2000 19JAN2021
;
data want;
set have;
age_at_enrollment = intck('year',dob,date_of_enrollment,'c');
run;
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.