Hi Sas users
I have two dates in the format of mmddyy8. How can I convert these dates to years. See my code below.
data colon.service;
number_years=year(admdate-svcdate);
run;
Hi @nturne
You can use the INTCK function, which returns the count of the number of interval boundaries between two dates, two
times, or two datetime values.
data service;
input admdate:mmddyy8. svcdate:mmddyy8.;
format admdate svcdate mmddyy8.;
number_years=intck("year",admdate,svcdate);
cards;
01/16/17 12/16/19
;
run;
admdate-svcdate is the number of days between admdate and svcdate. To get approximate years, you divide by 365 or 365.25. To get exact years accounting for leap years, use the INTCK function or the YRDIF function.
Thank you! The intck function worked.
The YEAR() function wants a date as input. If you subtract two dates you will get some small number of days which the YEAR() function will then interpret as a date. So if the difference in days is less than 365 then result will be a date in the year 1960 so applying the YEAR() function to it will return 1960.
How do you want to define the difference in years? Do you just want to subtract 2015 from 2019?
number_years=year(admdate)-year(svcdate);
Do you want to take the difference in days and divide by an average number of days in a year?
number_years=(admdate-svcdate)/365.25 ;
Or perhaps you want to use the INTCK() function to count for you.
Hi @nturne
You can use the INTCK function, which returns the count of the number of interval boundaries between two dates, two
times, or two datetime values.
data service;
input admdate:mmddyy8. svcdate:mmddyy8.;
format admdate svcdate mmddyy8.;
number_years=intck("year",admdate,svcdate);
cards;
01/16/17 12/16/19
;
run;
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.