I have table in SAS Enterprise Guide like below:
name of table: table123
date typ:
- ID - numeric
- DT - numeric
- VAL1 - string
- VAL2 - string
| ID | DT | VAL1 | VAL2 |
|---|---|---|---|
| 123 | 20210523 | PR | P |
| 123 | 20211222 | P | P |
| 222 | 20210502 | PR | M |
| 222 | 20210711 | M | PR |
| 38 | 20210513 | PR | PR |
| 991 | 20211123 | M | P |
And I need to create column "COL1" with values 0/1:
- If ID changed value from column "VAL1" from PR to value P or M in column "VAL2" and has not changed back to PR for 4 months or more -> 1 else 0
SO I need something like below:
| ID | DT | VAL1 | VAL2 | COL1 |
|---|---|---|---|---|
| 123 | 20210523 | PR | P | 1 |
| 123 | 20211222 | P | P | 1 |
| 222 | 20210502 | PR | M | 0 |
| 222 | 20210711 | M | PR | 0 |
| 38 | 20210513 | PR | PR | 0 |
| 991 | 20211123 | M | P | 0 |
Because:
- 123 - has 1 in "COL1" because changed PR to P and does not changed back to PR for 4 month or more
- 222 - has 0 in "COL1" because changed PR to M but did not last like this for at least 4 months (after 2 months backed to PR)
- 38 - has 0 in "COL1" because did not change PR to P or M
- 991 - has 0 in "COL1" because did not change PR to P or M
How can I do that in SAS Enterprise Guide in PROC SQL ?