BookmarkSubscribeRSS Feed
knveraraju91
Barite | Level 11

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

 

 

 

8 REPLIES 8
Patrick
Opal | Level 21

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.

Ksharp
Super User

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;
Ksharp
Super User
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.

knveraraju91
Barite | Level 11

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.

 

'

'

 

SuryaKiran
Meteorite | Level 14

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
saumil_tripathi
Calcite | Level 5

Thank you so much. This is exactly I wanted.

Regards,

Saumil

Ksharp
Super User

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;
AravindRaj001
Calcite | Level 5
How to convert Time format to hours...

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