You do not have a date, you have a datetime. Datetimes are counts of seconds, dates are counts of days. To extract the date from a datetime, use the datepart() function.
Your double function call keeps the value as it is, only cutting off any fractions of seconds, which could be achieved easier with round().
I don't know what you mean by converting a date into days,
but may be you mean:
date = datepart(datetime_variable);
having to variables you can calculate age by subtracting years:
age = intck('year', datetime2, datetime1);
or
age = intck('year', date2, date1);
You have to use "dtyear" as the interval.
@Kurt_Bremser wrote:
You have to use "dtyear" as the interval.
Or DTDAY, DTMONTH , DTWEEK (see a pattern here?).
The code
age = intck('year', date2, date1);
will not always produce the result you might expect, because what it counts is the number of occurences of January 1 between the two dates:
69 data _null_; 70 years = intck('year', '31dec2019'd, '01jan2020'd); 71 put years=; 72 run; years=1
Use of the CONTINUOUS option might produce the result you want:
data _null_; years = intck('year', '31dec2019'd, '01jan2020'd, 'continuous'); put years=; run;
The traditional method of calculating age in SAS, attributed to Billy Kreuter, is
age = floor(( intck( 'month', dob, death) - ( day(death) < day(dob)))/ 12);
<https://www.lexjansen.com/pharmasug/2011/CC/PharmaSUG-2011-CC20.pdf>
There's a lazy method that just takes the difference in days and divides by 365.25. Please don't use that method.
@sasuser123123 wrote:
Thank you for suggestion, actually I'm trying to calculate age from two variables. While calculating age should we consider time and seconds or not
That would be YOUR requirement to know.
Hours seldom become a consideration unless dealing with newborn babies though.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.