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;
Result:
Robert |
1987-12-27 |
Alex |
1946-11-23 |
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:
Robert |
1987-12-27 |
32.8466 |
32 |
Alex |
1946-11-23 |
73.9397 |
73 |
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:
Robert |
1987-12-27 |
32 |
Alex |
1946-11-23 |
73 |
Paul |
1996-03-12 |
24 |
May the SAS be with you!
Mark