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
Hi
The DATATYPE= has to be time, also the directives are case sensitive, it should work using the following code:
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
To get the right result use DATATYPE = TIME on your PICTURE FORMAT.
This is because you are counting time intervals not datetime intervals.
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.
Hi
The DATATYPE= has to be time, also the directives are case sensitive, it should work using the following code:
Yes, it worked perfectly! Thanks everyone for your help!
Please mark question as answered
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 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.