BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
djbateman
Lapis Lazuli | Level 10

I am trying to create a report that shows a list of subjects, their date/time of first dose, date/time of last dose, and the difference between the two date/times.  I am supposed to report the difference in the format "D Days:HH:MM".  In other words, if the first dose date/time was 01JAN2019:08:00 and the last dose date/time was 05JAN2019:09:30, then the result for the difference would say, "4 Days:01:30".

 

I have been able to use INTNX to get the number of days, but how would I further extract the hours and minutes that has transpired?  I think my biggest concern is if the example above had the times swapped: 01JAN2019:09:30 and 05JAN2019:08:00.  In this case, the difference should be "3 Days:22:30".  I don't think INTNX would produce the proper number of days.  It would still return 4.

 

Here is a sample dataset to help get you started.  Any suggestions?

 

proc sql;
	create table have (SUBJECT char(11), MINDTTM num format=datetime18., MAXDTTM num format=datetime18.);
	insert into have (subject, mindttm, maxdttm)
		values('001-001-001','06DEC2018:07:35:00'dt,'08JAN2019:08:24:00'dt)
		values('001-001-002','06DEC2018:07:50:00'dt,'07JAN2019:07:17:00'dt)
		values('001-001-003','07DEC2018:08:30:00'dt,'07JAN2019:07:30:00'dt)
		values('001-001-004','07DEC2018:08:40:00'dt,'18MAR2019:07:49:00'dt)
		values('001-001-005','01JAN2019:08:00:00'dt,'05JAN2019:09:30:00'dt)
		values('001-001-006','01JAN2019:09:30:00'dt,'05JAN2019:08:00:00'dt);
quit;

data want;
	set have;
	daydiff=intck('DTDAY',mindttm,maxdttm);
run;

 

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

Just take the difference, which will be a value in seconds.  To display it as DAY and HH:MM then split the value using 24Hours.

seconds=maxdttm - mindtdtm;
days=int(seconds/'24:00't);
hhmm=mod(seconds,'24:00't);
string=catx('/',catx(' ',days,'days'),put(hhmm,tod5.));

View solution in original post

2 REPLIES 2
Tom
Super User Tom
Super User

Just take the difference, which will be a value in seconds.  To display it as DAY and HH:MM then split the value using 24Hours.

seconds=maxdttm - mindtdtm;
days=int(seconds/'24:00't);
hhmm=mod(seconds,'24:00't);
string=catx('/',catx(' ',days,'days'),put(hhmm,tod5.));
djbateman
Lapis Lazuli | Level 10
Thanks, Tom! I knew it had to be something simpler than I was making it.

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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
  • 5779 views
  • 3 likes
  • 2 in conversation