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

It is extremely frustrating going from one date format to another.  It feels like each time I'm using a date function, it calls for something different.  What is the inner logic that governs all these different SAS date formats.  All I want to do is to take the difference in months between two dates.  Apparently, INTNX only accepts something like ' 05JAN2014'd.  So how do I convert 05JAN2014:00:00:00 into the required format?

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

INTNX and INTCK accepts dates or date/time values. '05JAN2014'd is a date value. '05JAN2014:00:00:00'dt is a datetime value.

 

To determine the difference in months from date values, use INTCK('month',date1,date2) (note: both variables must contain date values)

 

To determine the difference in months from datetime values, use INTCK('dtmonth',datetime1,datetime2) (note: both variables must contain datetime values)

 

You can convert datetime values to date values using the DATEPART() function.

--
Paige Miller

View solution in original post

6 REPLIES 6
PaigeMiller
Diamond | Level 26

INTNX and INTCK accepts dates or date/time values. '05JAN2014'd is a date value. '05JAN2014:00:00:00'dt is a datetime value.

 

To determine the difference in months from date values, use INTCK('month',date1,date2) (note: both variables must contain date values)

 

To determine the difference in months from datetime values, use INTCK('dtmonth',datetime1,datetime2) (note: both variables must contain datetime values)

 

You can convert datetime values to date values using the DATEPART() function.

--
Paige Miller
AndyTam
Fluorite | Level 6

Ok, but how do I convert the column, called IncidentDate, of date-time values into 'xxxxxx'.dt?

PaigeMiller
Diamond | Level 26

@AndyTam wrote:

Ok, but how do I convert the column, called IncidentDate, of date-time values into 'xxxxxx'.dt?


You don't. If IncidentDate contains datetime values, then there's no reason to "convert" it into 'xxxxx'.dt which is also a datetime value.

--
Paige Miller
Kurt_Bremser
Super User

@AndyTam wrote:

Ok, but how do I convert the column, called IncidentDate, of date-time values into 'xxxxxx'.dt?


You don't need to, you just use the value. As already stated, internally it is a count of seconds, and that's what the functions expect, if used with the 'dt'-type date and the time intervals.

Kurt_Bremser
Super User

Internally, dates are counts of days, and datetimes and times counts of seconds.

Formats are only there to control how those values are displayed.

To compare values, you must make them comparable. You can't compare a date with a datetime.

Intervals between two values are calculated with the INTCK function; with dates, you use the interval 'month', with datetimes 'dtmonth'.

SASKiwi
PROC Star

Here is an example of calculating the number of months between two datetimes. I suspect you will want to count contiguous months rather than month boundaries, hence the "C" option.

data _null_;
   months=intck('dtmonth', '01aug2011:00:10:48'dt, '01feb2012:00:10:48'dt, 'C');
   put months=;
run;

Read the documentation here  

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 6 replies
  • 783 views
  • 3 likes
  • 4 in conversation