Calcite | Level 5

## Convert HH:MM to days hours and minutes

Hi,

I need to convert a varible with HH:MM(eg .  76:02) to 3 days, 2hours and 02 minutes.

The  value is stored as numeric. I coverted it to time5. format.

1 ACCEPTED SOLUTION

Accepted Solutions
Obsidian | Level 7

## Re: Convert HH:MM to days hours and minutes

each Mimi,

Time is counted in seconds. To get number of days you need to divide the time variable by number of seconds, i.e. 24*60*60.

data a;
informat time_var time5. ;
input time_var;
format  time_var time5. days best8. time time5.;
days=floor(time_var/(24*60*60));
time=time_var - days*24*60*60;
hours=floor(time/(60*60));
minutes=time/60-hours*60;
seconds=time - hours*(60*60) - minutes*(60);
days_hrs_min=put(days, best4.)||" days "||put(hours,2.)||" hours "||put(minutes,2.)||" minutes";
cards;
76:02
72:40
;
run;

proc print data=a;
var time_var days hours minutes days_hrs_min;
run;

Obs    time_var        days    hours    minutes      days_hrs_min

1      76:02             3      4          2      3 days  4 hours  2 minutes
2      72:40             3      0         40      3 days  0 hours 40 minutes

Even if you assigned the time5. format to the time variable, the entire value is still available and you can use a different format or deassign a format.

format time_var;

Then you will have a numerical variable represnting the number of seconds.

I hope this helps.

Dorota

Message was edited by: Dorota Jarosz added code to calculate days, hours, minutes and a character string concatenating all three.

9 REPLIES 9
Lapis Lazuli | Level 10

## Re: Convert HH:MM to days hours and minutes

data have;

informat time \$5.;

input time ;

cards;

76:02

72:40

;

data want(drop=h time);

set have;

h=input(scan(time,1,':'),4.);

min=input(scan(time,2,':'),2.);

do days=0 to 999 ;

hours=h-24*days;

if hours <24 then do;

output;

leave;end;

end;

run;

proc print;run;

Calcite | Level 5

## Re: Convert HH:MM to days hours and minutes

Thank you sooo much Lin.  I tried this way.  I was getting some errors.

Iwas looking for some functions.

Obsidian | Level 7

## Re: Convert HH:MM to days hours and minutes

I am not getting any errors with Linlin's code. Perhaps your data may have larger number of hours than 99, then the \$5. informat will be insufficient. You may change it to \$6. or more, if necessary.

However, in the loop calculating the division modulo 24 hours, I would use a "do while" loop rather than "leave;" or "stop;" statement to jump out of the loop. But this is a matter of programming style (old school).

Obsidian | Level 7

## Re: Convert HH:MM to days hours and minutes

each Mimi,

Time is counted in seconds. To get number of days you need to divide the time variable by number of seconds, i.e. 24*60*60.

data a;
informat time_var time5. ;
input time_var;
format  time_var time5. days best8. time time5.;
days=floor(time_var/(24*60*60));
time=time_var - days*24*60*60;
hours=floor(time/(60*60));
minutes=time/60-hours*60;
seconds=time - hours*(60*60) - minutes*(60);
days_hrs_min=put(days, best4.)||" days "||put(hours,2.)||" hours "||put(minutes,2.)||" minutes";
cards;
76:02
72:40
;
run;

proc print data=a;
var time_var days hours minutes days_hrs_min;
run;

Obs    time_var        days    hours    minutes      days_hrs_min

1      76:02             3      4          2      3 days  4 hours  2 minutes
2      72:40             3      0         40      3 days  0 hours 40 minutes

Even if you assigned the time5. format to the time variable, the entire value is still available and you can use a different format or deassign a format.

format time_var;

Then you will have a numerical variable represnting the number of seconds.

I hope this helps.

Dorota

Message was edited by: Dorota Jarosz added code to calculate days, hours, minutes and a character string concatenating all three.

Calcite | Level 5

## Re: Convert HH:MM to days hours and minutes

Thanks a lot Dorota!!!! This is the one I was looking for easy to understand.  This helped me to solved my issue. This is the perfect answer for me . It s my first time in this blog

Great help!!!!

Obsidian | Level 7

Opal | Level 21

## Re: Convert HH:MM to days hours and minutes

Just another way of doing this:

proc format;
picture mytime
low-high='%H hours %M minutes' (datatype=time)
;
run;

data _null_;
time='36:12't;
if time ne . then string=catx(' ',put(floor(time/'24:00't),8.),'day',put(mod(time,86400),mytime.));
put string=;
run;

Calcite | Level 5

## Re: Convert HH:MM to days hours and minutes

Patric, Great one . works perfectly !!!  simple with less code. Thank you all foor the fast response!!

Rhodochrosite | Level 12

## Re: Convert HH:MM to days hours and minutes

Patrick

you were so close!

The picture format should perform the whole job - days included

(ok as long as the number of days is less than 29 (in Feb, or 30/31 as appropriate)

Adapting Patrick's proc format code, here is a SASlog snippet

70   proc format;

71     picture mytime

72       low-high=' %d days %H hours %M minutes'(datatype=datetime)

73     ;

NOTE: Format MYTIME has been output.

74     %put %sysfunc( dhms( 3,15,23,0), mytime );

4 days 15 hours 23 minutes

75   run;

NOTE: PROCEDURE FORMAT used

(showing perhaps why Patrick used another method to report the "day")

So I needed to re-think:

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

With little improvement on Patrick's code, my "one-liner" becomes

%put %sysfunc( intnx( dtday, %sysfunc( dhms( 3,15,23,0 )), -1, s ), mytime );

where in INTNX, the DTDAY interval deducts 1 day from the "datetime" value while the "S" parameter keeps the same time

and the result was

3 days 15 hours 23 minutes

It was disappointing that in my SAS9.3 the PROC FORMAT %n directrive didn't provide the duration days, just 0.

peterC

p.s.

dhms( 3,15,23,0)

provides the datetime value

Message was edited by: Peter Crawford once the duration directive %n is working, this approach will be better. otherwise special arrangements are needed for duration of less than one day (instead of 0 it reports 31 from 31Dec1959 !) maybe later!

Discussion stats
• 9 replies
• 17523 views
• 5 likes
• 5 in conversation