turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

Find a Community

- Home
- /
- SAS Programming
- /
- Base SAS Programming
- /
- How to calculate difference time (minutes) between...

Topic Options

- RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

11-17-2017 02:33 PM

I'm a layman in SAS. I'm using SAS Enterprise Guide and I have a problem.

I need to calculate the difference time (minutes) between two rows.

However, only calculate the difference when it's on the same day.

For example:

How to calculate difference time and to create the Difference Column?

Accepted Solutions

Solution

11-21-2017
02:49 PM

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to guimeleiro

11-17-2017 05:07 PM - edited 11-17-2017 05:14 PM

This will give you the number of minutes with a caveat.

If your hour variable actually have second components you may not get what you want because the intck function counts interval boundaries. The second code example shows the time values 2 seconds apart but the function returns 1 "minute" interval.

If you need 60-second minutes then you'll get into some different rounding such as round( (hour-lhour)/60, 1) or possibly Floor or Ceil instead of Round depending on your need.

data want; set have; by day; lhour=lag(hour); if first.day then difference=.; else difference = intck('minute',lhour,hour); drop lhour; run;

data junk; x= "12:10:59"t; y= "12:11:01"t; interval = intck('minute',x,y); run;

All Replies

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to guimeleiro

11-17-2017 03:01 PM

Is your hour variable an actual SAS date value?

And do you want the result to be a count of minutes? Your result shows a time value but I am going to guess that's because you're working in Excel.

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to ballardw

11-17-2017 03:41 PM - edited 11-17-2017 03:42 PM

I imported the Excel data.

Actually "day" and "hour" are SAS date value.

Yes, I want the result to be a count of minutes.

Solution

11-21-2017
02:49 PM

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to guimeleiro

11-17-2017 05:07 PM - edited 11-17-2017 05:14 PM

This will give you the number of minutes with a caveat.

If your hour variable actually have second components you may not get what you want because the intck function counts interval boundaries. The second code example shows the time values 2 seconds apart but the function returns 1 "minute" interval.

If you need 60-second minutes then you'll get into some different rounding such as round( (hour-lhour)/60, 1) or possibly Floor or Ceil instead of Round depending on your need.

data want; set have; by day; lhour=lag(hour); if first.day then difference=.; else difference = intck('minute',lhour,hour); drop lhour; run;

data junk; x= "12:10:59"t; y= "12:11:01"t; interval = intck('minute',x,y); run;

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to ballardw

11-21-2017 03:45 PM

Other doubt:

In case I want to calculate difference time (minutes) between two rows by conditioning it for day and group (Group A or Group B, for example), I put:

if first.day and first.group then difference=.;

Or Is there another way?

In case I want to calculate difference time (minutes) between two rows by conditioning it for day and group (Group A or Group B, for example), I put:

if first.day and first.group then difference=.;

Or Is there another way?

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to guimeleiro

11-17-2017 05:15 PM

This is a good situation for the DIF function embedded in an IFN function:

data want;

set have;

by day;

difference=ifn(first.day,.,dif(hour));

format difference time8.0;

run;