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


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

1 ACCEPTED SOLUTION

Accepted Solutions
Dorota_Jarosz
Obsidian | Level 7

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.

View solution in original post

9 REPLIES 9
Linlin
Lapis Lazuli | Level 10

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;

mimi
Calcite | Level 5

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

Iwas looking for some functions.

Dorota_Jarosz
Obsidian | Level 7

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).

Dorota_Jarosz
Obsidian | Level 7

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.

mimi
Calcite | Level 5

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!!!!

Dorota_Jarosz
Obsidian | Level 7

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.

Patrick
Opal | Level 21

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;

mimi
Calcite | Level 5

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

Peter_C
Rhodochrosite | Level 12

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!

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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