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

Hi, 

 

I have the data stored in character for start time and end time which represent yyyymmddhhmmss.

eg date start time   20210218050525 ,   date end time 20210218051729

I use the substr function to extract the time as below:

 

start time 050525   end time 051729 

 

I need to calculate the time difference. 

If the date go over onto the next day. Is there a condition statement can be used ?

 

 

 

Thanks

 

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User
data have;
	start_time='20191011234432AD';
	end_time='20191012005325AD';
run;

data want;
	set have;
	start_time_sas=input(substr(start_time, 1, 14) , b8601dj.);
	end_time_sas=input(substr(end_time, 1, 14) , b8601dj.);
	format start_time_sas end_time_sas datetime22.;
	diff=end_time_sas - start_time_sas;
	format diff time8.;
run;

proc print data=want;
run;

Results:

Obs	start_time	end_time	start_time_sas	end_time_sas	diff
1	20191011234432AD	20191012005325AD	11OCT2019:23:44:32	12OCT2019:00:53:25	1:08:53

@EPANG1 wrote:

the example for data spread into 2 days are as follow:

 

start time: 20191011234432AD

end time: 20191012005325AD

 

I use substr (starttime,9,6) = 234432  and substr(endtime,9,6) = 005325

 

The output I need is 53 minutes 25 seconds plus 15minutes 28 seconds which gives the final answer as 01:08:53. 

 

Hope this make sense. 

 


 

View solution in original post

10 REPLIES 10
Reeza
Super User
What exactly do you want to see as the results, "12 minutes and 4 seconds" or 001204 or 00:12:04?

EPANG1
Fluorite | Level 6

the answer I need is to count the time in seconds. So I can convert the answer into hours and minutes later. Also, if the time go over onto the next day, I need a condition statement in order to calculate the time.At the moment, the data is only set as character.

Reeza
Super User
How do you know if it goes into the next day?
Please provide more details, specifically, an example of your input data and then ideally the matched output example.

SAS stores times as seconds by default. So if you convert them to SAS times and do a mathematical subtraction that will be seconds but it doesn't account for crossing days so you need to explain how that works. If you have dates in another column then you can create datetime variables, subtract them and end up with seconds as well.

EPANG1
Fluorite | Level 6

The answer I need is 00:12:04.

Reeza
Super User
What are your inputs?
EPANG1
Fluorite | Level 6

the example for data spread into 2 days are as follow:

 

start time: 20191011234432AD

end time: 20191012005325AD

 

I use substr (starttime,9,6) = 234432  and substr(endtime,9,6) = 005325

 

The output I need is 53 minutes 25 seconds plus 15minutes 28 seconds which gives the final answer as 01:08:53. 

 

Hope this make sense. 

 

Reeza
Super User
data have;
	start_time='20191011234432AD';
	end_time='20191012005325AD';
run;

data want;
	set have;
	start_time_sas=input(substr(start_time, 1, 14) , b8601dj.);
	end_time_sas=input(substr(end_time, 1, 14) , b8601dj.);
	format start_time_sas end_time_sas datetime22.;
	diff=end_time_sas - start_time_sas;
	format diff time8.;
run;

proc print data=want;
run;

Results:

Obs	start_time	end_time	start_time_sas	end_time_sas	diff
1	20191011234432AD	20191012005325AD	11OCT2019:23:44:32	12OCT2019:00:53:25	1:08:53

@EPANG1 wrote:

the example for data spread into 2 days are as follow:

 

start time: 20191011234432AD

end time: 20191012005325AD

 

I use substr (starttime,9,6) = 234432  and substr(endtime,9,6) = 005325

 

The output I need is 53 minutes 25 seconds plus 15minutes 28 seconds which gives the final answer as 01:08:53. 

 

Hope this make sense. 

 


 

ballardw
Super User

Or use the Informat width definition to avoid the SUBSTR call

data want;
	set have;
	start_time_sas=input(start_time, b8601dj14.);
	end_time_sas=input(end_time, b8601dj14.);
	format start_time_sas end_time_sas datetime22.;
	diff=end_time_sas - start_time_sas;
	format diff time8.;
run;
Reeza
Super User
Thanks, good tip there!
EPANG1
Fluorite | Level 6

the code works beautifully. 

SAS Innovate 2025: Call for Content

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!

Submit your idea!

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
  • 10 replies
  • 1062 views
  • 2 likes
  • 3 in conversation