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

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!

Register now

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
  • 1163 views
  • 0 likes
  • 2 in conversation