Difference between values in two rows

Solved
Occasional Contributor
Posts: 14

Difference between values in two rows

Hi,

any one knows how to get difference between two rows in DI. i.e. following is the orginal Table

 Date Value 06/12/2013 150 07/12/2013 160 08/12/2013 180 09/12/2013 200 10/12/2013 250

and We need to generate a new column called Difference as the following table:

 Date Value Difference 06/12/2013 150 07/12/2013 160 10 08/12/2013 180 20 09/12/2013 200 20 10/12/2013 250 50

Accepted Solutions
Solution
‎01-16-2013 02:07 PM
Super Contributor
Posts: 1,636

Re: Difference between values in two rows

data want;

set have;

difference=dif(value);

run;

Message was edited by: Linlin

All Replies
Solution
‎01-16-2013 02:07 PM
Super Contributor
Posts: 1,636

Re: Difference between values in two rows

data want;

set have;

difference=dif(value);

run;

Message was edited by: Linlin

Contributor
Posts: 41

Re: Difference between values in two rows

Hello Linlin,

This solves my issue in terms of calculating differences across my dataset, however I dont want to run this for each record in the table everytime I insert a new row in the table - do you know if there is a function / piece of code which will basically calculate the difference between the last row and the new row being inserted and then insert the value? Also is this kind of difference calc only possible in Datastep?

Thanks,
Tom

Learner
Posts: 1

Re: Difference between values in two rows

 Date Value Difference 06/12/2013 250 50 07/12/2013 200 40 08/12/2013 160 30 09/12/2013 130 10 10/12/2013 120 0

How to get the difference output like this?

☑ This topic is solved.