Hi,
I am new to SAS programming in SAS EG.
I am trying to add a duration column which calculates the duration between two time columns.
I am currently using the following:
data duration;
set mydata_table;
DURATION=end_time-start_time;
run;
These are the type of results I get
row | start_time | end_time | duration
1 | 11:05:19 | 12:00:43 | 3324
2 | 12:00:43 | 4:19:25 | -27678
I would like the output of duration to be in seconds for all rows in the data set
and where the time goes into the following day as per row 2, I would like it to calculate the actual duration from one day to the next rather than displaying in a negative value.
Hope you can help.
Hi There,
I managed to use the following code which gave me the outcome I was after.
My data set consisted of a start_date column, a start_time column, and an end_time column.
data mydata_set_2;
set mydata_set;
drop start_date enddate end_time start_time;
START_DT=dhms(eventdate,0,0,modestart;
format START_DT datetime.;
if end_time<start_time then enddate=eventdate+1;
else enddate=eventdate;
format enddate date9.;
END_DT=dhms(enddate,0,0,modeend);
format END_DT datetime.;
DURATION=END_DT-START_DT;
run;
If you wish to have date differences as well then you need to add dates to your input data, either as separate date variables or you could combine them with your time variables as datetimes.There is no way of telling from your row 2 if the end time is for the next day, next week, or next month.
Hi There,
I managed to use the following code which gave me the outcome I was after.
My data set consisted of a start_date column, a start_time column, and an end_time column.
data mydata_set_2;
set mydata_set;
drop start_date enddate end_time start_time;
START_DT=dhms(eventdate,0,0,modestart;
format START_DT datetime.;
if end_time<start_time then enddate=eventdate+1;
else enddate=eventdate;
format enddate date9.;
END_DT=dhms(enddate,0,0,modeend);
format END_DT datetime.;
DURATION=END_DT-START_DT;
run;
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!
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.