Hi,
I have the following data:
Activation Complete
02SEP2018:08:07:12 02SEP2018:08:13:04
09SEP2018:15:03:33 09SEP2018:15:10:00
15SEP2018:21:22:23 15SEP2018:21:33:55
14JAN2019:04:03:03 14JAN2019:04:07:10
The "Activation" is format datetime19. and the "Complete" is datetime16. with informat datetime26.7
I want to create a new variable called "Difference" that calculate the timedifference between Activation and Complete. It should be in the format time8, so I am only interested in the timedifference in hour:min:sec (no dates e.g. 14JAN).
Hope someone can help!
Thank you in advance.
Like this?
data have;
input (Activation Complete)(:datetime26.7);
format Activation Complete datetime16.;
datalines;
02SEP2018:08:07:12 02SEP2018:08:13:04
09SEP2018:15:03:33 09SEP2018:15:10:00
15SEP2018:21:22:23 15SEP2018:21:33:55
14JAN2019:04:03:03 14JAN2019:04:07:10
;
data want;
set have;
Difference = Complete - Activation;
format Difference time8.;
run;
Like this?
data have;
input (Activation Complete)(:datetime26.7);
format Activation Complete datetime16.;
datalines;
02SEP2018:08:07:12 02SEP2018:08:13:04
09SEP2018:15:03:33 09SEP2018:15:10:00
15SEP2018:21:22:23 15SEP2018:21:33:55
14JAN2019:04:03:03 14JAN2019:04:07:10
;
data want;
set have;
Difference = Complete - Activation;
format Difference time8.;
run;
If your time different es could run across several days, I suggest to use a larger value for the TIME format, as SAS can display more than 2 digits for hours:
data have;
input (Activation Complete)(:datetime26.7);
format Activation Complete datetime16.;
datalines;
02SEP2018:08:07:12 08SEP2018:08:13:04
;
data want;
set have;
Difference = Complete - Activation;
format Difference time9.;
run;
Thanks to both of you!
If I want to calculate the mean difference I have tried the following code:
PROC MEANS data=have
N Mean median min max Q1 Q3 nmiss maxdec=2;
VAR Difference;
Run;
It returns the mean, meadian, min, max, Q1, and Q3 in seconds. Can it be displayed in min:sec instead?
Use the MMSS Format.
Complete code:
data have;
input (Activation Complete)(:datetime26.7);
format Activation Complete datetime16.;
datalines;
02SEP2018:08:07:12 02SEP2018:08:13:04
09SEP2018:15:03:33 09SEP2018:15:10:00
15SEP2018:21:22:23 15SEP2018:21:33:55
14JAN2019:04:03:03 14JAN2019:04:07:10
;
data want;
set have;
Difference = Complete - Activation;
format Difference mmss8.;
run;
ods path(prepend) work.templat(update);
proc template;
edit base.summary;
edit mean;
format=mmss8.;
end;
edit median;
format=mmss8.;
end;
edit min;
format=mmss8.;
end;
edit max;
format=mmss8.;
end;
edit q1;
format=mmss8.;
end;
edit q3;
format=mmss8.;
end;
end;
run;
proc means
data=want
n mean median min max Q1 Q3
nmiss
maxdec=2
;
var Difference;
run;
proc template;
delete base.summary;
run;
MEANS and SUMMARY provide no direct way to format your VAR variables, but you can use PROC TEMPLATE to do this through ODS:
/* create sample data */
data test;
input amount ;
cards;
1000
12345
8500
;
run;
/* Modify the base.summary table template using */
/* EDIT statements. */
ods path(prepend) work.templat(update);
proc template;
edit base.summary;
edit mean;
format=dollar12.2;
end;
edit sum;
format=dollar12.;
end;
end;
run;
proc means data=test n mean sum;
var amount;
run;
/* restore default template */
proc template;
delete base.summary;
run;
This example was taken from https://blogs.sas.com/content/sgf/2015/07/17/customizing-output-from-proc-means/, found through a Google search for "sas proc means use format" (third result); see Maxim 6.
Another way is to create an output dataset from MEANS, and then use PROC REPORT to have a nicely formatted report.
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.
Find more tutorials on the SAS Users YouTube channel.