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

How do you count the number of times a specific time of day (for example 23:59, i.e. 11:59PM) occurs in a datetime interval?

 

data have;  
	input ID $ begDate:datetime13. endDate:datetime13.;
	format begDate endDate datetime13.;
cards;
aaa 10NOV19:01:49 10NOV19:03:49
bbb 10NOV19:22:49 11NOV19:03:49
ccc 10NOV19:22:49 12NOV19:03:49
	;
run;

rommel_0-1643058550135.png

 

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

If your test is to see how many times midnight is in the interval, then the INTCK function does the job.

 

data want;
    set have;
    incl_midnight = intck('dtday',begdate,enddate);
run;

If you want some other time, let's say 10:45pm, I believe (untested), you subtract '22:45:00't from both begdate and enddate and then use the INTCK function.

--
Paige Miller

View solution in original post

2 REPLIES 2
HarrySnart
SAS Employee

Hi @dataMart87 , it may depend on what your use case is but an easy method would be to use the TIMEPART() function then do a count distinct. See example below 

*Load data;
data have;  
	input ID $ begDate:datetime13. endDate:datetime13.;
	format begDate endDate datetime13.;
cards;
aaa 10NOV19:01:49 10NOV19:03:49
bbb 10NOV19:22:49 11NOV19:03:49
ccc 10NOV19:22:49 12NOV19:03:49
	;
run;

*Extract time;
data want;
set have;
begTimePart = timepart(begDate);
format begTimePart time.;
run;

*Get count of time appearance;
proc sql;select distinct begTimePart,count(begTimePart) 
from work.want group by begTimePart;quit;
PaigeMiller
Diamond | Level 26

If your test is to see how many times midnight is in the interval, then the INTCK function does the job.

 

data want;
    set have;
    incl_midnight = intck('dtday',begdate,enddate);
run;

If you want some other time, let's say 10:45pm, I believe (untested), you subtract '22:45:00't from both begdate and enddate and then use the INTCK function.

--
Paige Miller

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

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!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 1021 views
  • 0 likes
  • 3 in conversation