Time Accumulation

Accepted Solution Solved
Reply
Contributor
Posts: 23
Accepted Solution

Time Accumulation

I am trying to total up some times and print them out to no avail. when I do a PUTLOG on TOTALTIME it says 0; I've tried doing a PUT with TIME5. and still no success. I am trying to RETAIN and sum up the DURATION time - any recommendations to accumate time for hours and minutes?

RETAIN TOTALTIME;

DATE_START=INPUT(BEGINDT, JULIAN7.);

DATE_END=INPUT(ENDDT, JULIAN7.);

TIME_START=INPUT(BEGINTM, TIME10.);

TIME_END=INPUT(ENDTM, TIME10.);

DURATION=(DATE_END*86400+TIME_END) - (DATE_START*86400+TIME_START);

TOTALTIME =(TOTALTIME + DURATION);

PUTLOG 'TOTALTIME =' TOTALTIME;


Accepted Solutions
Solution
‎06-19-2014 06:35 PM
Contributor
Posts: 22

Re: Time Accumulation

Since, as you mentioned, the duration is correct, and the only thing lacking is the total time summed over observations, as Reeza mentioned, use the sum function. Here are the minor adjustments to you code:

retain totaltime 0;

date_start=input(begindt, julian7.);

date_end=input(enddt, julian7.);

time_start=input(begintm, time10.);

time_end=input(endtm, time10.);

duration=(date_end*86400+time_end) - (date_start*86400+time_start);

totaltime = sum(totaltime, duration);

putlog 'totaltime =' totaltime;

View solution in original post


All Replies
Super User
Super User
Posts: 6,495

Re: Time Accumulation

Show some sample data.

Watch out for records with invalid values for one of your four input variables.

Try using SUM() function or SUM statement instead of simple addition so that missing values will be ignored.

data xx;

  informat dt1 dt2 datetime20.;

  format dt1 dt2 datetime20.;

  format duration totaltime time.;

  input dt1 dt2 ;

  duration = dt2 -dt1 ;

  totaltime+duration;

  put (_n_ _all_) (=);

cards;

01JAN2014:11:30 01JAN2014:11:45

01JAN2014:23:30 02JAN2014:01:45

run;

Super Contributor
Posts: 644

Re: Time Accumulation

It looks like your data contains text representation of Julian dates.  I would suggest creating timestamps from your derived dates and times using the DHMS() function.

     Format TS_Start TS_End Datetime20. ;

     TS_Start = DHMS(Date_Start, 0, 0, Time_Start) ;

etc

Then

     Duration = TS_End - TS_Start ;     /*     Duration in Seconds     */

to address missing times using midnight as the default

     TIME_START=SUM (0, INPUT(BEGINTM, TIME10.)) ;

etc.

Richard

Contributor
Posts: 23

Re: Time Accumulation

Some sample data is:

DATE_START =

14168

DATE_END =

14168

TIME_START=

23.30

TIME_END=

01.25

The DURATION is correct but I want to keep adding up the DURATION for a final HH.MM total

Super Contributor
Posts: 275

Re: Time Accumulation

data _null_;

start='22Jan2014:08:14'dt;

end='22Jan2014:11:09'dt;

duration=end-start;

put duration=time5.;

run;

Super User
Posts: 17,748

Re: Time Accumulation

Use the sum function instead of +, i.e. sum(total time, duration). If duration is correct, and your retain statement is included the total should be accumulating to the end of the data set.

Super Contributor
Posts: 644

Re: Time Accumulation

Wile the retain method will meet your requirement, provided your data is sorted and you set duration to 0 at the start of a new group, I would suggest leaving that out in the data step and doing your summation in a subsequent SQL or Proc Means / Summary / Tabulate / Report (take your pick).  That gives you much more analytic flexibility - you can calculate the sum, the count, the max and min values of duration, and the median and mean etc.  It is also a more robust method as you are relying on a procedure to summarise thather than hand coding.

Richard

Solution
‎06-19-2014 06:35 PM
Contributor
Posts: 22

Re: Time Accumulation

Since, as you mentioned, the duration is correct, and the only thing lacking is the total time summed over observations, as Reeza mentioned, use the sum function. Here are the minor adjustments to you code:

retain totaltime 0;

date_start=input(begindt, julian7.);

date_end=input(enddt, julian7.);

time_start=input(begintm, time10.);

time_end=input(endtm, time10.);

duration=(date_end*86400+time_end) - (date_start*86400+time_start);

totaltime = sum(totaltime, duration);

putlog 'totaltime =' totaltime;

Contributor
Posts: 23

Re: Time Accumulation

Thanks for the great input - the RETAIN and SUM did just what I needed.

I then just did a PUT on it for time and it came out perfect with the following format:

TOTALTIMEO=PUT(TOTALTIME, TIME5.);

Thanks again everybody for your recommendations -

Stephen

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 8 replies
  • 362 views
  • 0 likes
  • 6 in conversation