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

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;

1 ACCEPTED SOLUTION

Accepted Solutions
ed_sas_member
Meteorite | Level 14

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;

View solution in original post

4 REPLIES 4
PaigeMiller
Diamond | Level 26

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.

--
Paige Miller
nturne
Calcite | Level 5

Thank you! The intck function worked.

Tom
Super User Tom
Super User

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.

 

ed_sas_member
Meteorite | Level 14

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;

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

Mastering the WHERE Clause in PROC SQL

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.

Discussion stats
  • 4 replies
  • 2592 views
  • 0 likes
  • 4 in conversation