DATA Step, Macro, Functions and more

computing age from datetime variable

Reply
Contributor
Posts: 71

computing age from datetime variable

I have a DOB variable as follows and I want to compute age from this variable

DOB

16JUL51:00:00:00

24NOV33:00:00:00

07MAY45:00:00:00

 

I think INTCK can not be used in this case.

I tried 

age = '24AUG16:00:00:00'd-DOB; but its giving me some wierd numbers. 

 

Experts I need your help !

 

Super Contributor
Posts: 266

Re: computing age from datetime variable

Please refer to this post.

Only difference in your question is date has time part, so apply datepart function on DOB column.

 

https://communities.sas.com/t5/SAS-Procedures/Age-from-BirthDate/td-p/80935

Super User
Posts: 7,808

Re: computing age from datetime variable

[ Edited ]

Both your values are datetime values, which means they are stored as numbers containing the seconds from 01jan1960:00:00:00.

If you subtract them, you once again get a count of seconds, but one that can't be displayed with SAS datetime formats, as it would fall into the first century AD, where the Gregorian calendar doesn't work.

If you use date values (date() and datepart(DOB)), you will get a count of days; to get years, use the intck() function with the correct parameters.

data have;
input DOB:datetime20.;
format DOB datetime19.;
cards;
16JUL51:00:00:00
24NOV33:00:00:00
07MAY45:00:00:00
;
run;

data want;
set have;
age = intck('year',datepart(DOB),date());
run;

proc print noobs;
run;

Result:

               DOB    age

16JUL1951:00:00:00     65
24NOV1933:00:00:00     83
07MAY1945:00:00:00     71

 

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Super User
Posts: 10,035

Re: computing age from datetime variable

Posted in reply to KurtBremser
The third parameter would be better.

age = intck('year',datepart(DOB),date(),'c');

Respected Advisor
Posts: 3,156

Re: computing age from datetime variable

Actually I am thinking no need for DATEPART() at all:

age=intck('dtyear',dob,datetime(),'c');

There are native intervals built for datetime values.

 

Ask a Question
Discussion stats
  • 4 replies
  • 262 views
  • 2 likes
  • 5 in conversation