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
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.
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.
The answer I need is 00:12:04.
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.
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.
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;
the code works beautifully.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.