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

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.  

1 ACCEPTED SOLUTION

Accepted Solutions
PeterClemmensen
Tourmaline | Level 20

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;

View solution in original post

9 REPLIES 9
PeterClemmensen
Tourmaline | Level 20

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;
Mikkel_madsen
Obsidian | Level 7
Perfect, thank you a lot @PeterClemmensen!

All best
MM
Kurt_Bremser
Super User

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;
Mikkel_madsen
Obsidian | Level 7

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? 

Mikkel_madsen
Obsidian | Level 7
Thanks @PeterClemmensen. Unfortunately, it won't work for me. I'm not sure how to incorporate the MMSS format in the code. Can you elaborate?
Kurt_Bremser
Super User

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;
Kurt_Bremser
Super User

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.

Mikkel_madsen
Obsidian | Level 7
Thank you @Kurt_Bremser - it seems to work perfectly!

sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


Register now!

Mastering the WHERE Clause in PROC SQL

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.

Discussion stats
  • 9 replies
  • 1499 views
  • 3 likes
  • 3 in conversation