## calculate the time period

Frequent Contributor
Posts: 128

# calculate the time period

I have a variable "TIME", format "DATETIME18."

How can I calculate Time1-Time2?
For example:

19JAN07:22:00:00 - 19JAN07:16:00:00?

I try to minus them directly, but I get " 1484863200", useless....

Thanks! Message was edited by: Ken_oy
Not applicable
Posts: 0

## Re: calculate the time period

Have you tried applying a DATETIME18. format to the difference?
Frequent Contributor
Posts: 128

## Re: calculate the time period

Posted in reply to deleted_user
It won't work for me. I tried both of the following:
(Sorry, mistakes in my code, skip this post pls)
--------------------------------------------------------

data insulin_l;
format ltime datatime18.;
set insulin;
ltime=lag(ttime);
ldose=lag(dose);
run;
--------------------------------------------------------
data insulin_l;
set insulin;
ltime=lag(ttime);
ldose=lag(dose);
format ltime datatime18.;
run;
--------------------------------------------------------

Message was edited by: Ken_oy Message was edited by: Ken_oy
Not applicable
Posts: 0

## Re: calculate the time period

Well, it that was a literal cut & paste, you need to use datEtime18 and not "datatime18"
Frequent Contributor
Posts: 128

## Re: calculate the time period

Posted in reply to deleted_user
I am using the following code now
--------------------------------------------------
data insulin_l;
set insulin;
ltime=ttime-lag(ttime);
ldose=dose-lag(dose);
format ltime datetime18.;
run;
-------------------------------------------

But the answer is:

18JAN07:20:00:00 - 19JAN07:08:00:00

= 31DEC59:12:00:00
----------------------------------
1959?? What I want is the difference between those time points.

Thanks, Kmg!
Not applicable
Posts: 0

## Re: calculate the time period

Yes, I wasn't thinking this through correctly.

Please check out the SAS function INTCK (or DATDIF if you are only interested in the number of days).
Super Contributor
Posts: 3,176

## Re: calculate the time period

Referencing the SAS support http://support.sas.com/ website, here are some useful reading links on SAS DATE and DATETIME variables.

Scott Barry
SBBWorks, Inc.

SAS(R) 9.2 Language Reference: Concepts
About SAS Date, Time, and Datetime Values
http://support.sas.com/documentation/cdl/en/lrcon/61722/HTML/default/a002200738.htm

WORKING WITH SAS® DATE AND TIME FUNCTIONS
Andrew H. Karp
Sierra Information Services, Inc.
San Francisco, California USA
http://www2.sas.com/proceedings/sugi24/Begtutor/p58-24.pdf
Frequent Contributor
Posts: 128

## Re: calculate the time period

Thanks everyone!

By using:

w=intck('minute', ttime, lag(ttime));
Super Contributor
Posts: 3,176

## Re: calculate the time period

I believe you want "dtminute" not "minute" when working with INTCK and DATETIME variables (as opposed to TIME variables). If you were to perform the calculation and then use a FORMAT statement for the difference variable, apply a FORMAT to that variable, I believe you would see the incorrect result reflected in the log.

Scott Barry
SBBWorks, Inc.
Discussion stats
• 8 replies
• 192 views
• 0 likes
• 3 in conversation