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

Hello, I am trying to create my own format using PROC FORMAT and apply this format to show the difference in days, hours, min, sec between two datetimes.  The hours, minutes and seconds calculate perfectly but I can't seem to find any directive that will display the number of days correctly.  If anyone could help me figure this out, I would really appreciate it.  Here's my code:

PROC FORMAT;

PICTURE Diff_Format

LOW-HIGH = '%N days, %H hrs, %M min, %S sec' (DATATYPE=DATETIME);

QUIT;

data _null_;
StartDate = '15APR2015:15:23:32'dt;
EndDate = '18APR2015:16:44:56'dt;
Difference = EndDate - StartDate;

/* Calculating the difference */
PUT Difference = Diff_Format.;
RUN;

The answer should be -> Difference=03 days, 1 hrs, 21 min, 24 sec

But this code yields -> Difference=01 days, 1 hrs, 21 min, 24 sec

1 ACCEPTED SOLUTION

Accepted Solutions
BrunoMueller
SAS Super FREQ

Hi

The DATATYPE= has to be time, also the directives are case sensitive, it should work using the following code:

PROC FORMAT;
 
PICTURE Diff_Format
    LOW-HIGH =
'%n days, %H hrs, %M min, %S sec' (DATATYPE=time)
  ;
QUIT;

data _null_;
  StartDate =
'15APR2015:15:23:32'dt;
  EndDate =
'18APR2015:16:44:56'dt;
  Difference = EndDate - StartDate;

 
/* Calculating the difference */
 
PUT Difference = Diff_Format.;
RUN;

View solution in original post

6 REPLIES 6
Reeza
Super User

I don't have an answer but I also don't get the same result you do, so I'm confused :smileyconfused:

SAS 9.3

1151

1152  data want;

1153  format startdate enddate datetime21.;

1154  StartDate = '15APR2015:15:23:32'dt;

1155  EndDate = '18APR2015:16:44:56'dt;

1156  Difference = EndDate - StartDate;

1157  /* Calculating the difference */

1158  PUT Difference = Diff_Format.;

1159  put difference = datetime21.;

1160  RUN;

Difference=0 days, 1 hrs, 21 min, 24 sec

Difference=04JAN1960:01:21:24

NOTE: The data set WORK.WANT has 1 observations and 3 variables.

NOTE: DATA statement used (Total process time):

      real time           0.00 seconds

      cpu time            0.00 seconds

SASKiwi
PROC Star

To get the right result use DATATYPE = TIME on your PICTURE FORMAT.

This is because you are counting time intervals not datetime intervals.

T_Harrell
Calcite | Level 5

Hello SASKiwi, my values are datetime because I want to know the difference in days, hours, min, sec.  Were you successful in running my code with DATATYPE = TIME?  When I ran it, there were errors.

BrunoMueller
SAS Super FREQ

Hi

The DATATYPE= has to be time, also the directives are case sensitive, it should work using the following code:

PROC FORMAT;
 
PICTURE Diff_Format
    LOW-HIGH =
'%n days, %H hrs, %M min, %S sec' (DATATYPE=time)
  ;
QUIT;

data _null_;
  StartDate =
'15APR2015:15:23:32'dt;
  EndDate =
'18APR2015:16:44:56'dt;
  Difference = EndDate - StartDate;

 
/* Calculating the difference */
 
PUT Difference = Diff_Format.;
RUN;
T_Harrell
Calcite | Level 5

Yes, it worked perfectly!  Thanks everyone for your help!

Reeza
Super User

Please mark question as answered

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!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 6 replies
  • 954 views
  • 1 like
  • 4 in conversation