Obsidian | Level 7

## How to calculate the difference between two datetimes in minutes?

I tried to calculate Diff by doing computed column > Advanced expression and then `datetime() - t1.datetime / 60 `. Diff is actually the difference between SAS_update and DateTime, SAS_Update is just the lastruntime (datetime())

This is what I got:

I get a very weird value but I expected Diff to have value -1 because there is a a difference of -1 minutes between them SAS_update and DateTime...

1 ACCEPTED SOLUTION

Accepted Solutions
PROC Star

## Re: How to calculate the difference between two datetimes in minutes?

Your approach is OK, but you need to add parentheses. Without them, SAS performs division first, then subtraction. Use parentheses to force the subtraction to take place first.
4 REPLIES 4

## Re: How to calculate the difference between two datetimes in minutes?

Use the function intck.

Datetime-variables are, without a format, the number of seconds passed from 1Jan1960 to the actual datetime. The function takes care of the details, if you need the difference in minutes, then use "dtminute" as interval.

Obsidian | Level 7

## Re: How to calculate the difference between two datetimes in minutes?

Could you show an example?
Super User

## Re: How to calculate the difference between two datetimes in minutes?

@Andalusia wrote:
Could you show an example?

Looks like this:

``intck('minute',t1.datetime,datetime(),"c")``

The "c" (for continuous) means that difference / 60 is used, and not the count of minute boundaries between the two values (90 seconds difference can turn out to be 2 minutes if the default is used).

For reference, run this:

``````data _null_;
time = datetime();
rc = sleep (119,1);
diff1 = intck('minute',time,datetime(),"c");
diff2 = intck('minute',time,datetime());
put _all_;
run;``````
PROC Star

## Re: How to calculate the difference between two datetimes in minutes?

Your approach is OK, but you need to add parentheses. Without them, SAS performs division first, then subtraction. Use parentheses to force the subtraction to take place first.
Discussion stats
• 4 replies
• 547 views
• 0 likes
• 4 in conversation