I am using SAS Data Integration Studio(Can be SAS Code or SQL Code) trying to solve one of the business rules given. The business rule is: To find out the Staff claimed for the same expense type, AND date purchase within 30 days(For example, one person buy toner every week, buying the same)(expense type)
I have data in this format:
StaffNo|Date |Trip |Desc | InvoiceNumber | ExpenseType | Amount----------------------------------------------------------------------------
1 |01JAN2015 |A |XX | ZXC |100 |50
1 |05JAN2015 |A |XX | ZXC |100 |50
1 |02FEB2015 |A |XX | ZXC |100 |50
1 |03APR2015 |B |YY | PPP |90 |1000
1 |02MAY2015 |B |YY | PPP |90 |1000
2 |01OCT2017 |C |LO | 123 |55 |777
2 |02JAN2018 |D |HI | 456 |66 |888
2 |02MAY2018 |E |LL | 789 |44 |11
3 |11MAR2016 |F |PO | 99AA |122 |88.5
3 |13MAR2016 |G |PO | 99AA |122 |88.5
3 |31DEC2016 |H |PO | 99AA |122 |88.5
Here is the thing, based on the sample record, i need to find out for Staff Number 1 to 3(In actual data, more than 100k Staff) that purchased the same ExpenseType item within 30 days. If found out, create a flag with value 'Y' with column named called 'RepeatPurchase' for all records that have this pattern.
In layman term, i want to find out the same StaffNo that purchases the same ExpenseType within 30 days and there is no rules stating to compare the first record against the 2nd or the last record, but preferably to always compare the next row of record that tie to the same StaffNo and ExpenseType.
Do take note that the Date might range from 1 day difference to 10 years difference for next row record, not necessarily to be in above Date pattern as above is just a sample.
I couldn't think of the way to do this.I was thinking to loop but couldn't think of the logic of it. Furthermore, if it can be solved without the use of loop or just a simple SQL statement with transpose or etc, it would be great either. However, whichever works is fine.
Any advice/guide given is very much appreciated.