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?
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.
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.
Ok, but how do I convert the column, called IncidentDate, of date-time values into 'xxxxxx'.dt?
@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.
@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.
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'.
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 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.