<?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 Where X in Y  condition in Proc SQL Join in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Where-X-in-Y-condition-in-Proc-SQL-Join/m-p/753659#M237574</link>
    <description>&lt;P&gt;I want to join dataset X &amp;amp;B to create a new dataset C., joining key being account number.&lt;/P&gt;
&lt;P&gt;But I want to use only those observations from dataset X where account number is present in another datset Z.&lt;/P&gt;
&lt;P&gt;I tried this code but its giving an error&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;PROC SQL;
CREATE TABLE C AS
SELECT A*,B.*SPEND FROM 
X WHERE ACCT_NUM IN (SELECT DISTINCT ACCT_NUM FROM Z)  AS A
LEFT JOIN
B AS B
ON A.ACCT_NUM=B.ACCT_NUM;
QUIT;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;What is the correct way to do this?&lt;/P&gt;</description>
    <pubDate>Tue, 13 Jul 2021 05:23:11 GMT</pubDate>
    <dc:creator>Shradha1</dc:creator>
    <dc:date>2021-07-13T05:23:11Z</dc:date>
    <item>
      <title>Where X in Y  condition in Proc SQL Join</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Where-X-in-Y-condition-in-Proc-SQL-Join/m-p/753659#M237574</link>
      <description>&lt;P&gt;I want to join dataset X &amp;amp;B to create a new dataset C., joining key being account number.&lt;/P&gt;
&lt;P&gt;But I want to use only those observations from dataset X where account number is present in another datset Z.&lt;/P&gt;
&lt;P&gt;I tried this code but its giving an error&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;PROC SQL;
CREATE TABLE C AS
SELECT A*,B.*SPEND FROM 
X WHERE ACCT_NUM IN (SELECT DISTINCT ACCT_NUM FROM Z)  AS A
LEFT JOIN
B AS B
ON A.ACCT_NUM=B.ACCT_NUM;
QUIT;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;What is the correct way to do this?&lt;/P&gt;</description>
      <pubDate>Tue, 13 Jul 2021 05:23:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Where-X-in-Y-condition-in-Proc-SQL-Join/m-p/753659#M237574</guid>
      <dc:creator>Shradha1</dc:creator>
      <dc:date>2021-07-13T05:23:11Z</dc:date>
    </item>
    <item>
      <title>Re: Where X in Y  condition in Proc SQL Join</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Where-X-in-Y-condition-in-Proc-SQL-Join/m-p/753679#M237585</link>
      <description>&lt;P&gt;Is it B.* or B.spend? It looks like a comma could be missing. And if acct_num is in both table A and table B then you will have to put either A.acct_num or B.acct_num to distinguish which one you want to use. I also think you need to put the where statement after the join.&lt;/P&gt;</description>
      <pubDate>Tue, 13 Jul 2021 07:19:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Where-X-in-Y-condition-in-Proc-SQL-Join/m-p/753679#M237585</guid>
      <dc:creator>tarheel13</dc:creator>
      <dc:date>2021-07-13T07:19:08Z</dc:date>
    </item>
    <item>
      <title>Re: Where X in Y  condition in Proc SQL Join</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Where-X-in-Y-condition-in-Proc-SQL-Join/m-p/753704#M237602</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/361528"&gt;@Shradha1&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I think you'll get a better performance if you avoid the IN operator and the associated subquery by using an inner join with dataset Z ([edit:] more precisely: the set of distinct account numbers from Z) instead.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Something like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table c as
select x.*, b.spend from 
x join (select distinct acct_num from z) z
on x.acct_num=z.acct_num
left join b
on x.acct_num=b.acct_num;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 13 Jul 2021 08:50:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Where-X-in-Y-condition-in-Proc-SQL-Join/m-p/753704#M237602</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2021-07-13T08:50:12Z</dc:date>
    </item>
    <item>
      <title>Re: Where X in Y  condition in Proc SQL Join</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Where-X-in-Y-condition-in-Proc-SQL-Join/m-p/753727#M237608</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
    create table test as 
    select a.*, c.lower
    from sashelp.class as a inner join
                                (select  name
                                from sashelp.class
                                where sex = 'M') as b
                       on a.name=b.name

                       left join sashelp.classfit as c
                       on a.name=c.name
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 13 Jul 2021 09:45:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Where-X-in-Y-condition-in-Proc-SQL-Join/m-p/753727#M237608</guid>
      <dc:creator>blueskyxyz</dc:creator>
      <dc:date>2021-07-13T09:45:58Z</dc:date>
    </item>
    <item>
      <title>Re: Where X in Y  condition in Proc SQL Join</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Where-X-in-Y-condition-in-Proc-SQL-Join/m-p/753737#M237609</link>
      <description>&lt;P&gt;Add another join (sub-selects perform notoriously bad in SAS SQL):&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table c as
  select
    a.*,
    b.spend
  from a
  left join b
  on a.acct_num = b.acct_num
  inner join z
  on a.acct_num = z.acct_num
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;BTW there is no need for shouting at the SAS interpreter, it respects lowercase perfectly well.&lt;/P&gt;</description>
      <pubDate>Tue, 13 Jul 2021 10:02:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Where-X-in-Y-condition-in-Proc-SQL-Join/m-p/753737#M237609</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2021-07-13T10:02:06Z</dc:date>
    </item>
  </channel>
</rss>

