BookmarkSubscribeRSS Feed
TCy
Calcite | Level 5 TCy
Calcite | Level 5

Help!

I need to find the difference between 2 time stamps. If I have the columns Task and TimeStamp, I'm looking for the difference between TimeStamp of row n & of row (n-1). I can't do this in query builder, as I learned... I know that I can use the dif function or a lag function, but where would I put this? Would I need to make a new program to pull the needed data from the existing table and then make the calculation?

I wish I could just use Excel!! Any help would be GREATLY appreciated!

1 REPLY 1
Keith
Obsidian | Level 7

The DIF function is what you need here.  You put it in a data step, such as :

data want;

set have;

by timestamp;

time_dif=dif(timestamp);

run;

This assumes that the dataset is sorted in timestamp order and that you don't have any other BY variables (e.g. Task).  I put the BY statement in so that the step would fail if the data wasn't sorted by timestamp.  You have to be careful if you are using IF or WHERE statements with DIF and LAG, it's worth reading the following article which describes the pitfalls you may encounter and how to resolve them.


www.nesug.org/proceedings/nesug07/cc/cc33.pdf

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

Creating Custom Steps in SAS Studio

Check out this tutorial series to learn how to build your own steps in SAS Studio.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 1 reply
  • 1108 views
  • 0 likes
  • 2 in conversation