I have a data set containing ID, Visit Type, and Date. ID Visit Date 1 clinic 20060505 1 hospital 20060506 1 hospital 20061217 2 clinic 20060301 2 clinic 20060305 2 clinic 20070503 2 clinic 20070506 2 clinic 20100505 3 clinic 20061112 4 clinic 20080103 4 clinic 20081012 4 hospital 20081227 5 clinic 20050325 5 hospital 20050412 5 hospital 20070510 5 clinic 20061010 5 clinic 20061231 5 clinic 20070125 6 clinic 20060718 6 clinic 20060817 6 clinic 20070918 Now based on this, I want to define "Index Date." For patients who visited both hospital and clinic, I want the index date to be the first visit of hospital. For patients who only visited clinics, I want the index date to be the first visit of clinic. So the new table I want would look something like this ID Visit Date Index Date 1 clinic 20060505 20060506 1 hospital 20060506 20060506 1 hospital 20061217 20060506 2 clinic 20060301 20060301 2 clinic 20060305 20060301 2 clinic 20070503 20060301 2 clinic 20070506 20060301 2 clinic 20100505 20060301 3 clinic 20061112 20061112 4 clinic 20080103 20081227 4 clinic 20081012 20081227 4 hospital 20081227 20081227 5 clinic 20050325 20050412 5 hospital 20050412 20050412 5 hospital 20070510 20050412 5 clinic 20061010 20050412 5 clinic 20061231 20050412 5 clinic 20070125 20050412 6 clinic 20060718 20060718 6 clinic 20060817 20060718 6 clinic 20070918 20060718 I tried using DO loop but seems like I just can't get it right. How to solve this?!
... View more