I have a dataset temp1 as:
| ID | Drug | lob | Timestamp |
|---|---|---|---|
| 123 | acetam | counter | 01JAN22 04:25:17 |
| 123 | acetam | counter | 01JAN22 09:15:13 |
| 123 | acetam | prescr | 02JAN22 15:05:25 |
| 123 | acetam | counter | 03JAN22 23:28:05 |
| 234 | tylenol | counter | 11JAN22 18:12:39 |
| 345 | aztyr | counter | 03FEB22 16:11:19 |
| 345 | aztyr | counter | 03FEB22 16:15:20 |
for the same ID, Drug, lob and the old timestamp create a flag as 'Yes'
the second record of same ID, Drug, lob will also have a flag of 'Yes' when the timestamp is greater than 5 days compared to timestamp of previous entry.
expected output
| ID | Drug | lob | Timestamp | flag |
|---|---|---|---|---|
| 123 | acetam | counter | 01JAN22 04:25:17 | Yes |
| 123 | acetam | counter | 01JAN22 09:15:13 | No |
| 123 | acetam | prescr | 02JAN22 15:05:25 | Yes |
| 123 | acetam | counter | 11JAN22 23:28:05 | Yes |
| 234 | tylenol | counter | 11JAN22 18:12:39 | Yes |
| 345 | aztyr | counter | 03FEB22 16:11:19 | Yes |
| 345 | aztyr | counter | 03FEB22 16:15:20 | No |
My Code:
create table temp2 as
select id, drug,lob, datepart(timestamp)as timestamp format=mmddyy10.
from temp1
order by id, drug,lob
quit;
data temp3;
set temp2;
by id, drug,lob
diff=timestamp-lag(timestamp);
if first.id and first.drug and first.lob then do diff=.;end;
run;
I am first trying to calculate the difference between dates so that I can create a flag, but for the 3rd record for an lob='prescr' the diff I am getting is 1 but since the lob is different from the previous records this has to be considered as new record and flag has to 'yes'. I am stuck here and not able to move. Can anyone help?