<?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 Conditional join on two variables in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Conditional-join-on-two-variables/m-p/151825#M29900</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I have two datasets I am trying to join. One of them has ID, and the other one has ID1 and ID2. ID2 is only populated if someone's ID1 changes (the old value goes in ID2, while the new value goes in ID1). What I want to do is join the datasets such that as many people join as possible. That means I want to join ID to ID1, but then, for those where there is no ID1 match, I'd like to join ID to ID2. I know how to use a proc sql statement to join on two variables, but I don't know of a way to add conditions to the join.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Any help is much appreciated.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks!&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Fri, 31 Oct 2014 13:15:03 GMT</pubDate>
    <dc:creator>Walternate</dc:creator>
    <dc:date>2014-10-31T13:15:03Z</dc:date>
    <item>
      <title>Conditional join on two variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditional-join-on-two-variables/m-p/151825#M29900</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I have two datasets I am trying to join. One of them has ID, and the other one has ID1 and ID2. ID2 is only populated if someone's ID1 changes (the old value goes in ID2, while the new value goes in ID1). What I want to do is join the datasets such that as many people join as possible. That means I want to join ID to ID1, but then, for those where there is no ID1 match, I'd like to join ID to ID2. I know how to use a proc sql statement to join on two variables, but I don't know of a way to add conditions to the join.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Any help is much appreciated.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks!&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 31 Oct 2014 13:15:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditional-join-on-two-variables/m-p/151825#M29900</guid>
      <dc:creator>Walternate</dc:creator>
      <dc:date>2014-10-31T13:15:03Z</dc:date>
    </item>
    <item>
      <title>Re: Conditional join on two variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditional-join-on-two-variables/m-p/151826#M29901</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;proc sql;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; create table WANT as&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; select&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; A.*,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; B.*&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; from&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; TABLEA A&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; full join&amp;nbsp;&amp;nbsp; TABLEB B&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; on&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; A.ID=COALESCE(B.ID2,B.ID1);&lt;/P&gt;&lt;P&gt;quit;&lt;/P&gt;&lt;P&gt;/* I.e. if ID2 exists then use that for compare, else use ID1 */&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;You could also do the join a couple of times:&lt;/P&gt;&lt;P&gt;proc sql;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ...&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; on&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; A.ID=B.ID1&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; full join TABLEB&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; on&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; A.ID=B.ID2;&lt;/P&gt;&lt;P&gt;quit;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 31 Oct 2014 13:29:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditional-join-on-two-variables/m-p/151826#M29901</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2014-10-31T13:29:31Z</dc:date>
    </item>
    <item>
      <title>Re: Conditional join on two variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditional-join-on-two-variables/m-p/151827#M29902</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;/*prepare some data*/&lt;/P&gt;&lt;P&gt;data a;&lt;BR /&gt;set sashelp.class;&lt;BR /&gt;if mod(_N_,2)=1 then do name1=name;age=0;end;&lt;BR /&gt;else do;&lt;BR /&gt;name1="ABC";&lt;BR /&gt;name2=name;&lt;BR /&gt;age=15;&lt;BR /&gt;end;&lt;/P&gt;&lt;P&gt;if _n_=19 then delete;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;proc sql;&lt;BR /&gt;create table want as&lt;BR /&gt; select b.name, a.age&lt;BR /&gt; from a,sashelp.class b&lt;BR /&gt; where&lt;BR /&gt; b.name=a.name1 or b.name=a.name2;&lt;BR /&gt;quit;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 31 Oct 2014 14:21:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditional-join-on-two-variables/m-p/151827#M29902</guid>
      <dc:creator>Loko</dc:creator>
      <dc:date>2014-10-31T14:21:40Z</dc:date>
    </item>
  </channel>
</rss>

