Hi Patrick, Your hash solution is too diffcult for me:smileysilly:. I like your Sql solution (I changed having b.date2-s.date1 = min(b.date2-s.date1) to having b.date2-s.date1 = min(b.date2-s.date1) and b.id=s.id . Thank you ! Astounding, I will try to come up a solution as you suggested . PG, I tried to rewrite your code to make sure I understand completely. I got the same result even without the ORDER statement. I wonder why :smileyconfused:. data small; format date1 mmddyy10.; input id date1 mmddyy10.; cards; 1 08/08/2000 1 08/08/2007 1 08/08/2012 2 07/05/2002 2 09/09/2004 ; data big; format date2 mmddyy10.; input id date2 mmddyy10.; cards; 1 07/08/2000 1 08/08/2002 1 09/08/2004 1 10/08/2007 1 10/08/2009 1 10/08/2011 1 08/08/2012 2 06/05/2002 2 08/07/2003 2 09/09/2004 2 08/20/2005 ; run; proc sort data=small; by id descending date1; data snext; format lastdate1 firstdate1 mmddyy10.; lastdate1='01jan2020'd; do until (last.id); set small; by id; firstdate1=intnx('month',date1,-3,'s'); output; lastdate1=intnx('day',date1,-1); end; run; proc sql; create table want as select s.id,s.date1,b.date2 from snext as s inner join big as b on s.id=b.id and (b.date2 between firstdate1 and lastdate1); quit; Message was edited by: Linlin
... View more