<?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 select statement in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/SQL-select-statement/m-p/766786#M243040</link>
    <description>&lt;P&gt;You can't do that in the Data Set Options directly.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;See the difference in the two PROC SQL steps below. Should give you the answer.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* Test data */
data one;
input id x;
datalines;
1 10
2 20
3 30
;

data two;
input id;
datalines;
1
3
;

/* Error */
proc sql;
   create table want as 
   select one.*
   from one(where = (id in (select id from two)));
quit;

/* All good */
proc sql;
   create table want as
   select one.*
   from one
   where id in (select id from two);
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Thu, 09 Sep 2021 08:59:49 GMT</pubDate>
    <dc:creator>PeterClemmensen</dc:creator>
    <dc:date>2021-09-09T08:59:49Z</dc:date>
    <item>
      <title>SQL select statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SQL-select-statement/m-p/766784#M243038</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I want to select in proc sql with a where statement from a list&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;from							test.account(where =(delete_ind = 'N'
														and current_record_ind ='Y'
														and appid in(select id from test2)))&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;ERROR 22-322: Syntax error, expecting one of the following: a quoted string, a numeric constant, a datetime constant, &lt;BR /&gt;a missing value, -. &lt;BR /&gt;ERROR 76-322: Syntax error, statement will be ignored.&lt;BR /&gt;ERROR: Syntax error while parsing WHERE clause.&lt;BR /&gt;ERROR: Invalid value for the WHERE option.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;How should I do this?&lt;/P&gt;</description>
      <pubDate>Thu, 09 Sep 2021 08:52:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SQL-select-statement/m-p/766784#M243038</guid>
      <dc:creator>Kiteulf</dc:creator>
      <dc:date>2021-09-09T08:52:15Z</dc:date>
    </item>
    <item>
      <title>Re: SQL select statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SQL-select-statement/m-p/766786#M243040</link>
      <description>&lt;P&gt;You can't do that in the Data Set Options directly.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;See the difference in the two PROC SQL steps below. Should give you the answer.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* Test data */
data one;
input id x;
datalines;
1 10
2 20
3 30
;

data two;
input id;
datalines;
1
3
;

/* Error */
proc sql;
   create table want as 
   select one.*
   from one(where = (id in (select id from two)));
quit;

/* All good */
proc sql;
   create table want as
   select one.*
   from one
   where id in (select id from two);
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 09 Sep 2021 08:59:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SQL-select-statement/m-p/766786#M243040</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2021-09-09T08:59:49Z</dc:date>
    </item>
    <item>
      <title>Re: SQL select statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SQL-select-statement/m-p/766788#M243042</link>
      <description>&lt;P&gt;So then I will need to do;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;from							test.account

where                                              delete_ind = 'N'
and                                                  current_record_ind ='Y'
and                                                  appid in(select id from test2)&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The problem then is that the query bogs down. At the moment I have only 3 records from test2 but there will be more later.&lt;/P&gt;
&lt;P&gt;If I am doing it manually and&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;from							test.account (where =(delete_ind = 'N'
														and current_record_ind ='Y'
														and appid in(3819532,3809511,3812779))) as t0&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;it is super fast&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 09 Sep 2021 09:08:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SQL-select-statement/m-p/766788#M243042</guid>
      <dc:creator>Kiteulf</dc:creator>
      <dc:date>2021-09-09T09:08:26Z</dc:date>
    </item>
    <item>
      <title>Re: SQL select statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SQL-select-statement/m-p/766814#M243049</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/35213"&gt;@Kiteulf&lt;/a&gt;&amp;nbsp; What about an inner join?&lt;/P&gt;
&lt;PRE&gt;from test.account r inner join test2 l
on r.delete_ind='N' and r.current_record_ind='Y' and r.appid=l.id&lt;/PRE&gt;
&lt;P&gt;or implicit:&lt;/P&gt;
&lt;PRE&gt;from test.account r,test2 l
where r.delete_ind='N' and r.current_record_ind='Y' and r.appid=l.id&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 09 Sep 2021 11:24:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SQL-select-statement/m-p/766814#M243049</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2021-09-09T11:24:28Z</dc:date>
    </item>
    <item>
      <title>Re: SQL select statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SQL-select-statement/m-p/766826#M243060</link>
      <description>&lt;P&gt;It sort of works.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It is not painstakingly slow but also not fast. Valid input! I forgot about that possibility.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;from test.account r,test2 l
where r.delete_ind='N' and r.current_record_ind='Y' and r.appid=l.id&lt;/PRE&gt;</description>
      <pubDate>Thu, 09 Sep 2021 12:08:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SQL-select-statement/m-p/766826#M243060</guid>
      <dc:creator>Kiteulf</dc:creator>
      <dc:date>2021-09-09T12:08:48Z</dc:date>
    </item>
    <item>
      <title>Re: SQL select statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SQL-select-statement/m-p/766828#M243061</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/35213"&gt;@Kiteulf&lt;/a&gt;&amp;nbsp;Depending on the number of rows in your tables below eventually performs much better.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set test.account(where=(delete_ind='N' and current_record_ind='Y'));
  if _n_=1 then
    do;
      if 0 then set test2(keep=id rename=(id=appid));
      dcl hash h1 (dataset:'test2(keep=id rename=(id=appid))');
      h1.defineKey('appid')
      h1.defineDone();
    end;
  if h1.check()=0;
run;&lt;/CODE&gt;&amp;nbsp;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 09 Sep 2021 12:20:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SQL-select-statement/m-p/766828#M243061</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2021-09-09T12:20:39Z</dc:date>
    </item>
    <item>
      <title>Re: SQL select statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SQL-select-statement/m-p/766832#M243063</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I forgot to mention that this data is on a SQL server. So the hashing will not work very well towards that right...&lt;/P&gt;</description>
      <pubDate>Thu, 09 Sep 2021 12:30:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SQL-select-statement/m-p/766832#M243063</guid>
      <dc:creator>Kiteulf</dc:creator>
      <dc:date>2021-09-09T12:30:40Z</dc:date>
    </item>
    <item>
      <title>Re: SQL select statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SQL-select-statement/m-p/767025#M243110</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;
&lt;P&gt;I forgot to mention that this data is on a SQL server. So the hashing will not work very well towards that right...&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;That's certainly not an insignificant detail. You could investigate if appid and/or id are indexed and then eventually have this condition first in your where clause.&amp;nbsp;Not sure how smart the SQL Server compiler is so these things are always worth a try.&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;r.appid=l.id&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If performance is really that important to you then you could also investigate or talk to a DBA if there are any SQL Server side tweaks possible - like writing code that executes in parallel. You then would use Postgres SQL code and pass it to the DB from SAS via explicit SQL.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also make sure that you don't use a select * but list explicitly the columns you really need. Sending the data from the DB to the SAS Server is often the bottleneck.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;A few other things that also can improve performance significantly - &lt;A href="https://go.documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/acreldb/n0gz66qe8msnkyn1jgnhrr0l2k8y.htm" target="_self"&gt;docu&lt;/A&gt; here:&lt;/P&gt;
&lt;P&gt;DIRECT_SQL=Yes&lt;/P&gt;
&lt;P&gt;READBUFF set to a value suitable for the environment&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And last but not least: I assume BOTH your tables are in the DB. If not then please tell us as this will be highly likely the reason for bad performance - and there are ways how to code differently to improve things.&lt;/P&gt;</description>
      <pubDate>Fri, 10 Sep 2021 10:50:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SQL-select-statement/m-p/767025#M243110</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2021-09-10T10:50:42Z</dc:date>
    </item>
  </channel>
</rss>

