Hello!
I am trying to calculate the difference between a date variable and today's date, using the INTCK function. I am trying to see if there is a difference between calculations using date variables or datetime variables. Here is my code:
data work.report_assessment2;
set work.report_assessment;
DateTime=input(Date, anydtdtm.);
format DateTime datetime20.;
Month1 = intck('month', Date, '20Aug2020'd, 'continuous');
Month2 = intck('month', DateTime, '20Aug2020'd, 'continuous');
run;
Is this the correct code? Month1 and Month2 end up as blank variables.
@marleeakerson wrote:
Hello!
I am trying to calculate the difference between a date variable and today's date, using the INTCK function. I am trying to see if there is a difference between calculations using date variables or datetime variables. Here is my code:
data work.report_assessment2;
set work.report_assessment;
DateTime=input(Date, anydtdtm.);
format DateTime datetime20.;
Month1 = intck('month', Date, '20Aug2020'd, 'continuous');
Month2 = intck('month', DateTime, '20Aug2020'd, 'continuous');
run;
Is this the correct code? Month1 and Month2 end up as blank variables.
Did you look at this data set? Does DATE have a value? Is it numeric or character? What is a typical value of DATE?
Also, when you straighten that out, you need to fix this part as follows:
Month2 = intck('dtmonth', DateTime, '20Aug2020'd, 'continuous');
Depending on your answers to my questions above, there may be other errors that need to be fixed as well.
data work.report_assessment2;
set work.report_assessment;
DateTime=input(Date, anydtdtm.); <= if this doesn't throw a warning or error then likely DATE is not a date value.
format DateTime datetime20.;
Month1 = intck('month', Date, '20Aug2020'd, 'continuous');
Month2 = intck('month', DateTime, '20Aug2020'd, 'continuous');
run;
You need compare dates to dates and datetimes to datetimes:
data example;
input date :date9. datetime :datetime.;
format date date9. datetime datetime18.;
datedif = intck('month',date,today(),'c');
/*wrong as the following comparing a date with DT
uses different units*/
todayasdatetime = today();
/* format date as datetime to show how the value will be used
intck function with DT intervals
*/
format todayasdatetime datetime18.;
dtdif1 = intck('dtmonth',datetime,todayasdatetime,'c');
/* compare datetime to datetime*/
dtdif2 = intck('dtmonth',datetime, dhms(today(),0,0,time()),'c');
datalines;
21Jun2020 23Mar2020:12:15:13
;
The code is definitely wrong. Without knowing what you have it is hard to know exactly which parts are wrong.
The variable DATE can either be a character string. In which case this line could work (depending on what values it has).
DateTime=input(Date, anydtdtm.);
Or it is a numeric variable in which case this line could work, as long as the number is the number of days and not the number of seconds that the statement above is trying to assign to the other variable DATETIME.
Month1 = intck('month', Date, '20Aug2020'd, 'continuous');
If DATE is a string and you want to convert it to a date value use the ANYDTDTE informat instead of the ANYDTDTM informat. If DATE is a number with a date value (number of days) and you want to create a datetime value then use the DHMS() function and supply the time of day you want to use, usually midnight is used.
DateTime = dhms(date,0,0,0);
If DATE is a number with a datetime value (number of seconds) and you want to count months you can either use the DTMONTH interval
Month1 = intck('dtmonth', Date, '20Aug2020:00:00:00'dt, 'continuous');
or convert it to a date value and use the MONTH interval.
Month1 = intck('month', datepart(Date), '20Aug2020'd, 'continuous');
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
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.