<?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: SQL JOIN in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/SQL-JOIN/m-p/14236#M2223</link>
    <description>Sorry for the late reply Patrick and Ksharp. My apologies. Both codes worked brilliantly. &lt;BR /&gt;
Thanks a lot for your help.&lt;BR /&gt;
&lt;BR /&gt;
DB</description>
    <pubDate>Wed, 27 Oct 2010 13:33:39 GMT</pubDate>
    <dc:creator>DB_ECON</dc:creator>
    <dc:date>2010-10-27T13:33:39Z</dc:date>
    <item>
      <title>SQL JOIN</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/SQL-JOIN/m-p/14233#M2220</link>
      <description>Suppose I have two tables. &lt;BR /&gt;
                    &lt;BR /&gt;
Table 1&lt;BR /&gt;
&lt;I&gt;week&lt;/I&gt; &lt;I&gt;term     &lt;/I&gt; 			&lt;BR /&gt;
   &lt;BR /&gt;
1         1                 &lt;BR /&gt;
1         2                 &lt;BR /&gt;
1         3                 &lt;BR /&gt;
1         5                 &lt;BR /&gt;
1         8                 &lt;BR /&gt;
2         8                 &lt;BR /&gt;
2         5                 &lt;BR /&gt;
2         3                 &lt;BR /&gt;
2         1                 &lt;BR /&gt;
2         2                 &lt;BR /&gt;
&lt;BR /&gt;
Table 2&lt;BR /&gt;
&lt;I&gt;week &lt;/I&gt; 	&lt;I&gt;term&lt;/I&gt; &lt;I&gt;score  &lt;/I&gt; &lt;BR /&gt;
     1                1           5&lt;BR /&gt;
     1                2           9&lt;BR /&gt;
     1                3           7&lt;BR /&gt;
     1                4           6&lt;BR /&gt;
     1                7           3&lt;BR /&gt;
     2                2           4&lt;BR /&gt;
     2                4           7&lt;BR /&gt;
     2                5           3&lt;BR /&gt;
     2                1           2&lt;BR /&gt;
     2                9           6&lt;BR /&gt;
&lt;BR /&gt;
I want to attach &lt;I&gt;score&lt;/I&gt; from table 2 to table 1 in a particular way. For each &lt;I&gt;week&lt;/I&gt; and each &lt;I&gt;term&lt;/I&gt; in table 1, grab the variable &lt;I&gt;term&lt;/I&gt; in table 2 (which is not more than + 1 or - 1 units from &lt;I&gt;term&lt;/I&gt; in table 1) from that &lt;I&gt;week&lt;/I&gt; which has the highest &lt;I&gt;score&lt;/I&gt; .&lt;BR /&gt;
&lt;BR /&gt;
For e.g., for the first record (&lt;I&gt;week&lt;/I&gt; 1 and &lt;I&gt;term&lt;/I&gt; 1 in table 1),  there are two candidate &lt;I&gt;scores&lt;/I&gt; in table 2 : &lt;I&gt;Term&lt;/I&gt; 1 &lt;I&gt;week&lt;/I&gt; 1, which has a &lt;I&gt;score&lt;/I&gt; of 5 and &lt;I&gt;term&lt;/I&gt; 2 &lt;I&gt;week&lt;/I&gt; 1,  which has a &lt;I&gt;score&lt;/I&gt; of 9. I want to select the max of 5 and 9 which is 9. So the first record in table 1 is given a score of 9. &lt;BR /&gt;
&lt;BR /&gt;
Can anyone help me create this new table in SQL. I already know how to do this using transpose and a bunch of data steps. However I have too many groups and values which will make data step processing cumbersome. &lt;BR /&gt;
&lt;BR /&gt;
Thanks in advance.

Message was edited by: DB_ECON</description>
      <pubDate>Fri, 08 Oct 2010 18:35:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/SQL-JOIN/m-p/14233#M2220</guid>
      <dc:creator>DB_ECON</dc:creator>
      <dc:date>2010-10-08T18:35:02Z</dc:date>
    </item>
    <item>
      <title>Re: SQL JOIN</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/SQL-JOIN/m-p/14234#M2221</link>
      <description>Try this:&lt;BR /&gt;
&lt;BR /&gt;
proc sql;&lt;BR /&gt;
  select distinct &lt;BR /&gt;
      l.week&lt;BR /&gt;
    , l.term&lt;BR /&gt;
    , r.score&lt;BR /&gt;
&lt;BR /&gt;
  from table1 L left join table2 R&lt;BR /&gt;
    on    l.week=r.week &lt;BR /&gt;
      and r.term between l.term-1 and l.term+1&lt;BR /&gt;
&lt;BR /&gt;
  group by l.week, l.term&lt;BR /&gt;
  having r.score=max(r.score)&lt;BR /&gt;
  ;&lt;BR /&gt;
quit;</description>
      <pubDate>Sat, 09 Oct 2010 01:19:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/SQL-JOIN/m-p/14234#M2221</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2010-10-09T01:19:52Z</dc:date>
    </item>
    <item>
      <title>Re: SQL JOIN</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/SQL-JOIN/m-p/14235#M2222</link>
      <description>&lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;   Long time no see.&lt;BR /&gt;
I have not enter this forum for a long time (a couple of weeks) .&lt;BR /&gt;
Cartesian Product would be useful, but To the large dataset ,It will waste lots of time.&lt;BR /&gt;
&lt;BR /&gt;
[pre]&lt;BR /&gt;
data table1;&lt;BR /&gt;
 input week term;&lt;BR /&gt;
datalines;&lt;BR /&gt;
1 1&lt;BR /&gt;
1 2&lt;BR /&gt;
1 3&lt;BR /&gt;
1 5&lt;BR /&gt;
1 8&lt;BR /&gt;
2 8&lt;BR /&gt;
2 5&lt;BR /&gt;
2 3&lt;BR /&gt;
2 1&lt;BR /&gt;
2 2 &lt;BR /&gt;
;&lt;BR /&gt;
data table2;&lt;BR /&gt;
 input week term score;&lt;BR /&gt;
 datalines;&lt;BR /&gt;
 1 1 5&lt;BR /&gt;
1 2 9&lt;BR /&gt;
1 3 7&lt;BR /&gt;
1 4 6&lt;BR /&gt;
1 7 3&lt;BR /&gt;
2 2 4&lt;BR /&gt;
2 4 7&lt;BR /&gt;
2 5 3&lt;BR /&gt;
2 1 2&lt;BR /&gt;
2 9 6&lt;BR /&gt;
;&lt;BR /&gt;
&lt;BR /&gt;
proc sql feedback;&lt;BR /&gt;
 select table1.week,table1.term,max(table2.score) as score&lt;BR /&gt;
  from table1,table2&lt;BR /&gt;
   where table2.week eq table1.week and table2.term  between  (table1.term - 1)  and  (table1.term + 1)&lt;BR /&gt;
    group by table1.week,table1.term;&lt;BR /&gt;
quit;&lt;BR /&gt;
&lt;BR /&gt;
[/pre]&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
or use left join just as patrick mentioned which can save your resource.&lt;BR /&gt;
&lt;BR /&gt;
[pre]&lt;BR /&gt;
proc sql feedback;&lt;BR /&gt;
 select table1.week,table1.term,max(table2.score) as score&lt;BR /&gt;
  from table1 left join table2 &lt;BR /&gt;
     on table1.week eq table2.week&lt;BR /&gt;
   where  table2.term  between  (table1.term - 1)  and  (table1.term + 1)&lt;BR /&gt;
    group by table1.week,table1.term;&lt;BR /&gt;
quit;&lt;BR /&gt;
[/pre]&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
I have to work now.Hope my code is right. &lt;span class="lia-unicode-emoji" title=":winking_face:"&gt;😉&lt;/span&gt;&lt;BR /&gt;
and I test it ,it does work.&lt;BR /&gt;
I really hope to spend enough time on this forum. But......&lt;BR /&gt;
&lt;BR /&gt;
Ksharp

Message was edited by: Ksharp</description>
      <pubDate>Sat, 09 Oct 2010 07:58:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/SQL-JOIN/m-p/14235#M2222</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2010-10-09T07:58:47Z</dc:date>
    </item>
    <item>
      <title>Re: SQL JOIN</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/SQL-JOIN/m-p/14236#M2223</link>
      <description>Sorry for the late reply Patrick and Ksharp. My apologies. Both codes worked brilliantly. &lt;BR /&gt;
Thanks a lot for your help.&lt;BR /&gt;
&lt;BR /&gt;
DB</description>
      <pubDate>Wed, 27 Oct 2010 13:33:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/SQL-JOIN/m-p/14236#M2223</guid>
      <dc:creator>DB_ECON</dc:creator>
      <dc:date>2010-10-27T13:33:39Z</dc:date>
    </item>
  </channel>
</rss>

