What am I doing wrong. I copied the "diff" example out of a SAS blog and it doesn't work for me.
data table2;
infile datalines dlm=',';
input h_id date1 :DATETIME24.3 date2 :DATETIME24.3;
format date1 date2 DATETIME24.3;
return;
datalines;
52,16AUG2021:20:52:52.136,09JUL2021:08:52:29.693
10,11AUG2021:22:24:41.894,09JUL2021:16:40:26.374
41,13AUG2021:20:01:24.427,18JUL2021:14:38:36.254
74,23AUG2021:19:33:19.111,18JUL2021:14:54:06.874
37,08AUG2021:19:51:53.322,18JUL2021:15:54:10.637
82,23AUG2021:20:27:52.563,26JUL2021:15:47:00.000
90,28AUG2021:20:28:50.595,29JUL2021:18:46:08.624
65,22AUG2021:14:50:58.576,27JUL2021:18:46:08.624
23,01AUG2021:23:59:58.285,25JUL2021:18:46:08.624
;
run;
data table3;
set table2;
calendar_days =intck('dtday', date2, date1);
calendar_days1 =intck('day', date2, date1);
week_days=intck('WEEKDAY', date2, date1);
diff = intck('WEEKDAY', date2, date1);
run;
You need to specify dates, not datetimes. If you do specify datetimes you need to use DT in front of the interval specification, as your first one which is why it works.
calendar_days =intck('dtday', date2, date1); calendar_days1 =intck('day', date2, date1); week_days=intck('WEEKDAY', date2, date1); diff = intck('WEEKDAY', date2, date1);
https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lefunctionsref/p0syn64amroombn14vrdzksh459w.h...
Interval Names and SAS Dates
Specific interval names are used with SAS date values, and other interval names are used with SAS time and datetime values. The interval names that are used with SAS date values are YEAR, SEMIYEAR, QTR, MONTH, SEMIMONTH, TENDAY, WEEK, WEEKDAY, and DAY. The interval names that are used with SAS time and datetime values are HOUR, MINUTE, and SECOND.
Interval names that are used with SAS date values can be prefixed with 'DT' to construct interval names for use with SAS datetime values. The interval names DTYEAR, DTSEMIYEAR, DTQTR, DTMONTH, DTSEMIMONTH, DTTENDAY, DTWEEK, DTWEEKDAY, and DTDAY are used with SAS time or datetime values.
@DanD999 wrote:
What am I doing wrong. I copied the "diff" example out of a SAS blog and it doesn't work for me.
data table2; infile datalines dlm=','; input h_id date1 :DATETIME24.3 date2 :DATETIME24.3; format date1 date2 DATETIME24.3; return; datalines; 52,16AUG2021:20:52:52.136,09JUL2021:08:52:29.693 10,11AUG2021:22:24:41.894,09JUL2021:16:40:26.374 41,13AUG2021:20:01:24.427,18JUL2021:14:38:36.254 74,23AUG2021:19:33:19.111,18JUL2021:14:54:06.874 37,08AUG2021:19:51:53.322,18JUL2021:15:54:10.637 82,23AUG2021:20:27:52.563,26JUL2021:15:47:00.000 90,28AUG2021:20:28:50.595,29JUL2021:18:46:08.624 65,22AUG2021:14:50:58.576,27JUL2021:18:46:08.624 23,01AUG2021:23:59:58.285,25JUL2021:18:46:08.624 ; run; data table3; set table2; calendar_days =intck('dtday', date2, date1); calendar_days1 =intck('day', date2, date1); week_days=intck('WEEKDAY', date2, date1); diff = intck('WEEKDAY', date2, date1); run;
You need to specify dates, not datetimes. If you do specify datetimes you need to use DT in front of the interval specification, as your first one which is why it works.
calendar_days =intck('dtday', date2, date1); calendar_days1 =intck('day', date2, date1); week_days=intck('WEEKDAY', date2, date1); diff = intck('WEEKDAY', date2, date1);
https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lefunctionsref/p0syn64amroombn14vrdzksh459w.h...
Interval Names and SAS Dates
Specific interval names are used with SAS date values, and other interval names are used with SAS time and datetime values. The interval names that are used with SAS date values are YEAR, SEMIYEAR, QTR, MONTH, SEMIMONTH, TENDAY, WEEK, WEEKDAY, and DAY. The interval names that are used with SAS time and datetime values are HOUR, MINUTE, and SECOND.
Interval names that are used with SAS date values can be prefixed with 'DT' to construct interval names for use with SAS datetime values. The interval names DTYEAR, DTSEMIYEAR, DTQTR, DTMONTH, DTSEMIMONTH, DTTENDAY, DTWEEK, DTWEEKDAY, and DTDAY are used with SAS time or datetime values.
@DanD999 wrote:
What am I doing wrong. I copied the "diff" example out of a SAS blog and it doesn't work for me.
data table2; infile datalines dlm=','; input h_id date1 :DATETIME24.3 date2 :DATETIME24.3; format date1 date2 DATETIME24.3; return; datalines; 52,16AUG2021:20:52:52.136,09JUL2021:08:52:29.693 10,11AUG2021:22:24:41.894,09JUL2021:16:40:26.374 41,13AUG2021:20:01:24.427,18JUL2021:14:38:36.254 74,23AUG2021:19:33:19.111,18JUL2021:14:54:06.874 37,08AUG2021:19:51:53.322,18JUL2021:15:54:10.637 82,23AUG2021:20:27:52.563,26JUL2021:15:47:00.000 90,28AUG2021:20:28:50.595,29JUL2021:18:46:08.624 65,22AUG2021:14:50:58.576,27JUL2021:18:46:08.624 23,01AUG2021:23:59:58.285,25JUL2021:18:46:08.624 ; run; data table3; set table2; calendar_days =intck('dtday', date2, date1); calendar_days1 =intck('day', date2, date1); week_days=intck('WEEKDAY', date2, date1); diff = intck('WEEKDAY', date2, date1); run;
Thanks so much for the quick reply. I've used day and weekday before although not often. It was frustrating not being able to figure out why it wouldn't work. Now I know what the "dt" is for. I don't remember seeing that before.
Thanks again.
Dan
Here's a great, but longer and in depth, reference for dates and times in SAS that's useful
https://communities.sas.com/t5/SAS-Communities-Library/Working-with-Dates-and-Times-in-SAS-Tutorial/...
Thanks Reeza.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.