Not applicable
Posts: 1

# Simple Calculation Help

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!

Regular Contributor
Posts: 151

## Re: Simple Calculation Help

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.

Discussion stats