- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
Thanks in advance
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Thank you sooo much Lin. I tried this way. I was getting some errors.
Iwas looking for some functions.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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).
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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!!!!
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
I am glad I could help. It would be nice if you could select the best answer and mark your inquiry as Answered, so other users hunting for open problems know this one is answered - they will still be able to contribute their opinion. I am a newbie here, too. At first I could not find the option to select the best answer to my own stated problem. I believe you have to click on the header link in a particular post and then several options appear. You can select the best answer as well as several helpful answers. Blog participants earn (accumulate) higher experience this way.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Patric, Great one . works perfectly !!! simple with less code. Thank you all foor the fast response!!
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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!