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

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;

1 ACCEPTED SOLUTION

Accepted Solutions
WaltSmith
Fluorite | Level 6

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

8 REPLIES 8
Tom
Super User Tom
Super User

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;

RichardinOz
Quartz | Level 8

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

srosenfe
Fluorite | Level 6

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

slchen
Lapis Lazuli | Level 10

data _null_;

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

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

duration=end-start;

put duration=time5.;

run;

Reeza
Super User

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.

RichardinOz
Quartz | Level 8

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

WaltSmith
Fluorite | Level 6

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;

srosenfe
Fluorite | Level 6

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

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

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
  • 8 replies
  • 1374 views
  • 0 likes
  • 6 in conversation