I have table in SAS Enterprise Guide like below. Table presents history of clients and his eventually changes of services used (columns: BEFORE, AGAIN).
ID and DT are not sorted, but could be if it is important
data types of columns:
- ID - numeric
- DT - date
- BEFORE - character
- AFTER - character
| ID | DT | BEFORE | AFTER |
|---|---|---|---|
| 123 | 21MAY2021 | PR | P |
| 123 | 28OCT2021 | P | P |
| 123 | 30OCT2023 | P | P |
| 85 | 01AUG2021 | PR | PR |
| 85 | 15AUG2021 | PR | M |
| 85 | 22AUG2021 | M | PR |
| 11 | 25JUN2021 | P | P |
| 122 | 22JUL2021 | PR | PR |
| 444 | 18MAY2022 | PR | M |
And I need to create new binary column "COL1" with values 0/1
- If some client ("ID") changed service from PR (in "BEFORE") to P or M (in "AFTER") and did not return to PR (in "AFTER") service for another 4 or more months ("DT") --> then 1
- If some client changed service from PR (in "BEFORE") to P or M (in "AFTER") and return to PR (in "AFTER") earlier than after 4 months --> then 0
- If client did not make change from PR to P or M --> then 0
So as a result I need something like below:
| ID | DT | BEFORE | AFTER | COL1 |
|---|---|---|---|---|
| 123 | 21MAY2021 | PR | P | 1 --> changed PR to P and did not back to PR for 4 or more months |
| 123 | 28OCT2021 | P | P | 1 |
| 123 | 30OCT2023 | P | P | 1 |
| 85 | 01AUG2021 | PR | PR | 0 |
| 85 | 15AUG2021 | PR | M | 0 |
| 85 | 22AUG2021 | M | PR | 0 --> back to PR less than 4 months after changing PR to P or M |
| 11 | 25JUN2021 | P | P | 0 --> client did not make change from PR to P or M |
| 122 | 22JUL2021 | PR | PR | 0 --> client did not make change from PR to P or M |
| 444 | 18MAY2022 | PR | M | 1 --> customer changed PR to P or M and we have no further history of changes so 1 |
I totally do not know how to do that in PROC SQL in SAS Enterprise Guide, do you know what to do ?