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

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;

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

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;


 

View solution in original post

4 REPLIES 4
Reeza
Super User

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;


 

DanD999
Quartz | Level 8

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

Reeza
Super User

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

DanD999
Quartz | Level 8

Thanks Reeza.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 4 replies
  • 605 views
  • 2 likes
  • 2 in conversation