BookmarkSubscribeRSS Feed
marleeakerson
Calcite | Level 5

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. 

3 REPLIES 3
PaigeMiller
Diamond | Level 26

@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.

--
Paige Miller
ballardw
Super User

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 ;
Tom
Super User Tom
Super User

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'); 

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 3 replies
  • 406 views
  • 0 likes
  • 4 in conversation