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
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:
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. |
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.
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;
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.
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.
'
'
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;
Thank you so much. This is exactly I wanted.
Regards,
Saumil
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;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.