DATA Step, Macro, Functions and more

How to convert time in sec to dd hh:mm format

Reply
Super Contributor
Posts: 272

How to convert time in sec to dd hh:mm format

Dear,

 

what formay I have to use to convert a value into 'dd hh:mm'' format. 

 

eg;

 

variable1                      variable2

06JUN15:09:19             06JUN15:10:54

 

output needed;

duration(variable2-variable1)

0 01:35

 

Thanks

 

 

 

Respected Advisor
Posts: 4,173

Re: How to convert time in sec to dd hh:mm format

[ Edited ]
Posted in reply to knveraraju91

I'm not aware of an out of the box format which would write a SAS time value (=numeric value containing number of seconds) in the form you want to.

You can create your own format though which gives you what you want.

proc format;
picture myTime
other='%0n %0H:%0M:%0S' (datatype=time);
run;

data sample;
format variable1 variable2 datetime21.;
format dt_diff myTime.;
variable1='05JUN15:09:19'dt;
variable2='06JUN15:10:54'dt;

dt_diff=variable2-variable1;

run;

 

N.B: 

%n

number of days in a duration as a decimal number (maximum of 10 digits) (for example, 25).

Restriction This directive is not valid for DBCS and Unicode SAS sessions.

http://support.sas.com/documentation/cdl/en/proc/68954/HTML/default/viewer.htm#p0n990vq8gxca6n1vnsra...

 

P.S: Can you please go through your previous posts and mark the answer which helped you the most as the solution. It really helps us to no more look into questions which have already been answered.

Super User
Posts: 10,020

Re: How to convert time in sec to dd hh:mm format

Posted in reply to knveraraju91

proc format;
picture fmt(default=10)
 other='%n %0H:%0M'(datatype=time);
run;


data have;
input (variable1    variable2) (: datetime.);
dif=variable2-variable1;
format variable1 variable2 datetime. dif fmt.;
cards;
06JUN15:09:19             06AUG15:10:54
;
run;

proc print;run;
Super User
Posts: 10,020

Re: How to convert time in sec to dd hh:mm format

Posted in reply to knveraraju91
proc fcmp outlib=work.func.func;
function xx(c) $;
 length f hhmm $ 10;
 day=int(c/'24:00:00't);
 hhmm=put(mod(c,'24:00:00't),tod5.);
 f=catx(' ',day,hhmm);
 return (f);
endsub;
run;
options cmplib=work.func;
proc format;
value fmt(default=10)
 other=[xx()];
run;


data have;
input (variable1    variable2) (: datetime.);
dif=variable2-variable1;
format variable1 variable2 datetime. dif fmt.;
cards;
06JUN15:09:19             06AUG15:10:54
;
run;

proc print;run;

If you are using UE , you could try this, since %n is not supported by UE.

Super Contributor
Posts: 272

Re: How to convert time in sec to dd hh:mm format

Hi,

 

Thanks for the format. It worked for me except a few OBS where variable2 is less than variable1.

 

variable1                                          variable2

06JUN15:09:19             06AUG15:10:54
06JUN15:08:55             06JUN15:06:30

 

eg:

It gives the output correctly for OBS1, but not OBS2.

For OBS2 the output should be '-0 02:25'. I am getting '0 21:35'. Do you have any suggestion. Thanks.

 

'

'

 

Frequent Contributor
Posts: 136

Re: How to convert time in sec to dd hh:mm format

[ Edited ]
Posted in reply to knveraraju91

Hope this code works for your need.

 

data have;

input (variable1 variable2) (: datetime.);

format variable1 variable2 datetime. ;

cards;

06JUN15:09:19 06AUG15:10:54

06JUN15:08:55 06JUN15:06:30

;

run;

data one;

set have;

diff=INTCK('seconds',variable1,variable2);

If diff>=0 Then do; days=floor(diff/(24*60*60));

time=diff - 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";

end;

else do;

diff=-INTCK('seconds',variable1,variable2);

days=floor(diff/(24*60*60));

time=diff - 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");

end;

drop diff days time hours minutes seconds ;

run;

Thanks,
Suryakiran
Super User
Posts: 10,020

Re: How to convert time in sec to dd hh:mm format

Posted in reply to knveraraju91

Sure.

proc fcmp outlib=work.func.func;
function xx(c) $ 12;
 length f hhmm $ 12;
 day=int(abs(c)/'24:00:00't);
 hhmm=put(mod(abs(c),'24:00:00't),tod5.);
 if sign(c)=-1 then f='-'||catx(' ',day,hhmm);
  else f=catx(' ',day,hhmm);
 return (f);
endsub;
run;
options cmplib=work.func;
proc format;
value fmt(default=12)
 other=[xx()];
run;


data have;
input (variable1    variable2) (: datetime.);
dif=variable2-variable1;
format variable1 variable2 datetime. dif fmt.;
cards;
06JUN15:09:19             06AUG15:10:54
06JUN15:08:55             06JUN15:06:30
;
run;

proc print;run;
Ask a Question
Discussion stats
  • 6 replies
  • 927 views
  • 6 likes
  • 4 in conversation