How come the result change if I sort the admission data prior to the procedures? How can I get the right result everytime not depending on how the data is sorted? I have added a sorting statement to the below procedure. And notice that I have removed the admission number because I dont have that after all. data have1; input Record_id (Admission_date Discharge_date ) (:ddmmyy10.) ; format Admission_date Discharge_date ddmmyyd10.; datalines; 1 01-01-2010 01-02-2010 1 03-03-2010 31-03-2010 1 03-03-2011 31-03-2011 2 01-01-2010 01-02-2010 2 01-01-2011 01-02-2011 2 03-03-2013 31-03-2013 2 03-03-2018 31-03-2018 3 03-03-2010 31-03-2010 3 03-03-2015 31-03-2015 4 04-02-2011 04-03-2011 ; run; data have2; input Record_id (treatin_date treatout_date) (:ddmmyy10.) ; format treatin_date treatout_date ddmmyyd10.; datalines; 1 01-01-2007 01-02-2007 1 03-02-2010 28-02-2010 1 03-03-2017 31-03-2017 2 01-01-2006 01-02-2006 2 01-01-2008 01-03-2008 2 03-03-2017 31-03-2017 3 03-03-2017 31-03-2017 4 03-03-2010 31-03-2010 4 04-02-2012 04-03-2012 5 07-02-2012 02-04-2012 ; run; /*proc sort data=have1; by record_id admission_date; run;*/ proc sql; create table temp as select have1.*, have2.treatout_date from have1, have2 where have1.Record_id=have2.Record_id; quit; data want(drop=treatout_date); treated_2years_prior=0; do until (last.record_id| last.Admission_date); set temp; by record_id notsorted Admission_date notsorted; if intnx('year', Admission_date, -2, 's') le treatout_date le Admission_date then treated_2years_prior=1; end; run;
... View more