Dear Forum-Users,
I have a data set in long format – combining prescription data and dates of visits which looks like this – except for the „newvar“ – which I would like to obtain.
patID docID prescDATE visitDATE newvar
1XY A 10jan2018 . 2
1XY B 10mar2018 . 0
1XY A . 09jan2018 .
1XY A . 10jan2018 .
1XY B . 15may2018 .
1XY C . 13mar2018 .
2GH A 20jan2018 . 1
2GH C 25mar2018 . 0
2GH A . 18jan2018 .
2GH C . 15mar2018 0
3WE A 02jan2018 . 1
3WE C 05mar2018 . 0
3WE A 03jan2018 .
3WE B 07mar2018 .
What I want to do is „check“ whether each patient (patID) has been - for each prescription (prescDATE) - to the doctor who issued the prescription (docID) within a certain timeframe (lets say five days before or after the prescription date). If this is true, the "newvar" should count the visits within that timeframe.

I guess one solution would be to convert the dataset in the wide format and loop through the prescription dates?
I m looking for a clue how this could be handled in the long format?
Many Thanx, Lynde