Hello, I have about 500 DOBs and i need to find how old each one was on a certain date.
My data is like this and the date is ordered YYYY-MM-DD
Name DateOfBirth
Robert 1987-12-27
Alex 1946-11-23
Paul 1996-3-12I need to find how old each member was in November of 2020 and I'm not sure how to do this
Fortunately, SAS has a function made just for this 😁 YRDIF. The syntax is (oldestDate,newestDate). You can express your newest date as a date constant like this: '01NOV2020'd. So here's some code that creates your starter data:
data have;
input Name:$10. DateOfBirth:yymmdd10.;
format DateOfBirth yymmdd10.;
datalines;
Robert 1987-12-27
Alex 1946-11-23
Paul 1996-3-12
;
proc print data=have;
run;
| Obs | Name | DateOfBirth |
|---|---|---|
| 1 | Robert | 1987-12-27 |
| 2 | Alex | 1946-11-23 |
| 3 | Paul | 1996-03-12 |
Next we process the data:
data want;
set have;
AgeWithFractions=yrdif(DateOfBirth,'01Nov2020'd);
Age=int(AgeWithFractions);
run;
proc print data=want;
run;
Result:
| Obs | Name | DateOfBirth | AgeWithFractions | Age |
|---|---|---|---|---|
| 1 | Robert | 1987-12-27 | 32.8466 | 32 |
| 2 | Alex | 1946-11-23 | 73.9397 | 73 |
| 3 | Paul | 1996-03-12 | 24.6411 | 24 |
So let's be kind, and use the age without fractions as the result. We can calculate that straight up in a single expression like this:
data want;
set have;
Age=int(yrdif(DateOfBirth,'01Nov2020'd));
run;
proc print data=want;
run;
Result:
| Obs | Name | DateOfBirth | Age |
|---|---|---|---|
| 1 | Robert | 1987-12-27 | 32 |
| 2 | Alex | 1946-11-23 | 73 |
| 3 | Paul | 1996-03-12 | 24 |
May the SAS be with you!
Mark
Is your date a SAS date value, numeric with a format of yymmddd10.?
If the value is character you will need to convert it to a date value.
What is the result supposed to be? Age in days? weeks? months? calendar years?
Which DAY in November 2020?
Fortunately, SAS has a function made just for this 😁 YRDIF. The syntax is (oldestDate,newestDate). You can express your newest date as a date constant like this: '01NOV2020'd. So here's some code that creates your starter data:
data have;
input Name:$10. DateOfBirth:yymmdd10.;
format DateOfBirth yymmdd10.;
datalines;
Robert 1987-12-27
Alex 1946-11-23
Paul 1996-3-12
;
proc print data=have;
run;
| Obs | Name | DateOfBirth |
|---|---|---|
| 1 | Robert | 1987-12-27 |
| 2 | Alex | 1946-11-23 |
| 3 | Paul | 1996-03-12 |
Next we process the data:
data want;
set have;
AgeWithFractions=yrdif(DateOfBirth,'01Nov2020'd);
Age=int(AgeWithFractions);
run;
proc print data=want;
run;
Result:
| Obs | Name | DateOfBirth | AgeWithFractions | Age |
|---|---|---|---|---|
| 1 | Robert | 1987-12-27 | 32.8466 | 32 |
| 2 | Alex | 1946-11-23 | 73.9397 | 73 |
| 3 | Paul | 1996-03-12 | 24.6411 | 24 |
So let's be kind, and use the age without fractions as the result. We can calculate that straight up in a single expression like this:
data want;
set have;
Age=int(yrdif(DateOfBirth,'01Nov2020'd));
run;
proc print data=want;
run;
Result:
| Obs | Name | DateOfBirth | Age |
|---|---|---|---|
| 1 | Robert | 1987-12-27 | 32 |
| 2 | Alex | 1946-11-23 | 73 |
| 3 | Paul | 1996-03-12 | 24 |
May the SAS be with you!
Mark
Thank you so much!!
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.
Find more tutorials on the SAS Users YouTube channel.