Solved
New Contributor
Posts: 3

# PROC FORMAT Help

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

Accepted Solutions
Solution
‎05-04-2015 04:44 PM
SAS Super FREQ
Posts: 824

## Re: PROC FORMAT Help

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;

All Replies
Super User
Posts: 23,709

## Re: PROC FORMAT Help

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

Super User
Posts: 3,918

## Re: PROC FORMAT Help

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

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

New Contributor
Posts: 3

## Re: PROC FORMAT Help

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.

Solution
‎05-04-2015 04:44 PM
SAS Super FREQ
Posts: 824

## Re: PROC FORMAT Help

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;
New Contributor
Posts: 3

## Re: PROC FORMAT Help

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

Super User
Posts: 23,709