calculations between rows

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

calculations between rows

Paul Mcgeoghan
Hi,

I have a customer with data as follows:
      ID        Date
         1      11-DEC-2001
       1        21-DEC-2001
       1        30-DEC-2001
       2        10-JAN-2000
       2        12-JAN-2000
       2        15-FEB-2000
       2        20-MAR-2000
       2        26-APR-2000

He wants to have:
ID  Days
1   19
2   107

What is best way to do this?
Thanks,
Paul

==================
Paul McGeoghan,
Application support specialist (Statistics and Databases),
University Infrastructure Group (UIG),
Information Services,
Cardiff University.
Tel. 02920 (875035).
Reply | Threaded
Open this post in threaded view
|

Re: calculations between rows

Antro, Mark
Hi Paul,

Using v13 and above you can use the following syntax to obtain this result...

      ID        Date            Diff   Difference
       1        11-DEC-2001   0        19
       1        21-DEC-2001   10       19
       1        30-DEC-2001   19       19
       2        10-JAN-2000   0       107
       2        12-JAN-2000   2       107
       2        15-FEB-2000   36      107
       2        20-MAR-2000   70      107
       2        26-APR-2000   107     107

Syntax:

COMPUTE diff = 0.
DO IF (id = LAG(id)).
 COMPUTE Diff = LAG(Diff) + (DATEDIFF(date, LAG(date), "days")).
END IF.
AGGREGATE
  /OUTFILE=*
  MODE=ADDVARIABLES
  /BREAK=id
  /Difference = MAX(Diff).

Just ensure you have the data sorted by ID and ascending date.

Rgds,
Antro