BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
raddad34
Fluorite | Level 6

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-12

I need to find how old each member was in November of 2020 and I'm not sure how to do this

1 ACCEPTED SOLUTION

Accepted Solutions
SASJedi
Ammonite | Level 13

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:

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

Check out my Jedi SAS Tricks for SAS Users

View solution in original post

3 REPLIES 3
ballardw
Super User

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?

 

 

SASJedi
Ammonite | Level 13

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:

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

Check out my Jedi SAS Tricks for SAS Users
raddad34
Fluorite | Level 6

Thank you so much!!