<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: left join with condition in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/left-join-with-condition/m-p/649154#M194583</link>
    <description>&lt;P&gt;This is, SQL-wise, not really a join, but a union. Which makes for rather complicated SQL, as you have to repeat the subquery for data from HAVE1.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I think it is a lot easier with a data step, e.g.:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  found=0;
  do until(last.IDnumber);
    set have1(in=in1) have2(in=in2);
    by IDnumber date;
    if in1 and found=0 then do;
      found=1;
      output;
      end;
    else if in2 and found=1 then
      output;
    end;
  drop found;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Wed, 20 May 2020 10:22:05 GMT</pubDate>
    <dc:creator>s_lassen</dc:creator>
    <dc:date>2020-05-20T10:22:05Z</dc:date>
    <item>
      <title>left join with condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/left-join-with-condition/m-p/648897#M194475</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;I have following data&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;data have1;
infile datalines missover; 
   input IDnumber @3 date  mmddyy10.;
   format date mmddyy10.;
   datalines;  
1 4-7-2003 
1 4-7-2009 
1 4-7-2010 
2 4-7-2005 
2 4-7-2011
;       
run; &lt;/LI-CODE&gt;&lt;P&gt;and&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;data have2;
infile datalines missover; 
   input IDnumber @3 date  mmddyy10.;
   format date mmddyy10.;
   datalines;  
1 4-8-2003 
1 4-9-2003 
1 4-10-2009 
1 4-11-2009 
2 4-8-2005 
2 4-9-2005 
2 4-8-2011 
2 4-9-2011 
;       
run; &lt;/LI-CODE&gt;&lt;P&gt;and i need&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;data want;
infile datalines missover; 
   input IDnumber @3 date  mmddyy10.;
   format date mmddyy10.;
   datalines;  
1 4-7-2003 
1 4-8-2003 
1 4-9-2003 
1 4-10-2009 
1 4-11-2009 
2 4-7-2005 
2 4-8-2005 
2 4-9-2005 
2 4-8-2011 
2 4-9-2011 
;       
run; &lt;/PRE&gt;&lt;P&gt;What i need is to join have1 with have2 by id , check the dates in have 2, if greater than in have 1 then add the row under the first row in have 1.&lt;/P&gt;</description>
      <pubDate>Tue, 19 May 2020 16:07:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/left-join-with-condition/m-p/648897#M194475</guid>
      <dc:creator>ifti_ch2002</dc:creator>
      <dc:date>2020-05-19T16:07:47Z</dc:date>
    </item>
    <item>
      <title>Re: left join with condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/left-join-with-condition/m-p/648907#M194478</link>
      <description>&lt;P&gt;The Want doesn't seems to be what you described, it looks rather like: for a given ID if minimum value of date in have1 is less then dates in have2 then select minimum date from have 1 and all dates from have2 less than that value.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;From description it seem like for `1 4-7-2009` in have1 there should be two rows from have2 i.e. `1 4-10-2009` and `1 4-11-2009` but I don't see them in Want.&lt;/P&gt;
&lt;P&gt;Could you clarify?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Bart&lt;/P&gt;</description>
      <pubDate>Tue, 19 May 2020 16:25:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/left-join-with-condition/m-p/648907#M194478</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2020-05-19T16:25:13Z</dc:date>
    </item>
    <item>
      <title>Re: left join with condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/left-join-with-condition/m-p/648909#M194479</link>
      <description>&lt;P&gt;&lt;SPAN&gt;`1 4-10-2009` and `1 4-11-2009`&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;they are there in want. I can not understand what do u mean.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 19 May 2020 16:27:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/left-join-with-condition/m-p/648909#M194479</guid>
      <dc:creator>ifti_ch2002</dc:creator>
      <dc:date>2020-05-19T16:27:56Z</dc:date>
    </item>
    <item>
      <title>Re: left join with condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/left-join-with-condition/m-p/648913#M194482</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have1;
infile datalines missover; 
   input IDnumber @3 date  mmddyy10.;
   format date mmddyy10.;
   datalines;  
1 4-7-2003 
1 4-7-2009 
1 4-7-2010 
2 4-7-2005 
2 4-7-2011
;       
run; 

data have2;
infile datalines missover; 
   input IDnumber @3 date  mmddyy10.;
   format date mmddyy10.;
   datalines;  
1 4-8-2003 
1 4-9-2003 
1 4-10-2009 
1 4-11-2009 
2 4-8-2005 
2 4-9-2005 
2 4-8-2011 
2 4-9-2011 
;       
run; 


proc sql;
create table want as
select *
from have1
group by idnumber
having min(date)=date
union all
select b.*
from 
(select * from have1 group by idnumber having min(date)=date) a
left join have2 b
on a.idnumber=b.idnumber
and a.date&amp;lt;b.date
order by idnumber,date;
quit;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 19 May 2020 16:34:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/left-join-with-condition/m-p/648913#M194482</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2020-05-19T16:34:41Z</dc:date>
    </item>
    <item>
      <title>Re: left join with condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/left-join-with-condition/m-p/648918#M194484</link>
      <description>&lt;P&gt;Yes they are, but the&amp;nbsp;&lt;SPAN&gt;`1 4-7-2009` is not in the data, and from your description: "check the dates in have 2, if greater than in have 1 then add the row under the first row in have 1" I would expect to have also&amp;nbsp;`1 4-7-2009` in the Want like:&lt;/SPAN&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
infile datalines missover; 
   input IDnumber @3 date  mmddyy10.;
   format date mmddyy10.;
   datalines;  
1 4-7-2003 
1 4-8-2003 
1 4-9-2003 
1 4-10-2009 
1 4-11-2009 
1 4-7-2009 /**/
1 4-10-2009 /**/
1 4-11-2009 /**/
...
;       
run; &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 19 May 2020 16:47:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/left-join-with-condition/m-p/648918#M194484</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2020-05-19T16:47:46Z</dc:date>
    </item>
    <item>
      <title>Re: left join with condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/left-join-with-condition/m-p/649154#M194583</link>
      <description>&lt;P&gt;This is, SQL-wise, not really a join, but a union. Which makes for rather complicated SQL, as you have to repeat the subquery for data from HAVE1.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I think it is a lot easier with a data step, e.g.:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  found=0;
  do until(last.IDnumber);
    set have1(in=in1) have2(in=in2);
    by IDnumber date;
    if in1 and found=0 then do;
      found=1;
      output;
      end;
    else if in2 and found=1 then
      output;
    end;
  drop found;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 20 May 2020 10:22:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/left-join-with-condition/m-p/649154#M194583</guid>
      <dc:creator>s_lassen</dc:creator>
      <dc:date>2020-05-20T10:22:05Z</dc:date>
    </item>
  </channel>
</rss>

