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

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.

1 ACCEPTED SOLUTION

Accepted Solutions
justine05
Calcite | Level 5

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;

  

View solution in original post

2 REPLIES 2
SASKiwi
PROC Star

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.

justine05
Calcite | Level 5

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;

  

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