<?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: Bizarre SQL error when building table over itself in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Bizarre-SQL-error-when-building-table-over-itself/m-p/504366#M134958</link>
    <description>Apparently this has already been raised:&lt;BR /&gt;&lt;A href="http://support.sas.com/techsup/notes/v8/12/330.html" target="_blank"&gt;http://support.sas.com/techsup/notes/v8/12/330.html&lt;/A&gt;</description>
    <pubDate>Mon, 15 Oct 2018 15:49:27 GMT</pubDate>
    <dc:creator>EvoluZion3</dc:creator>
    <dc:date>2018-10-15T15:49:27Z</dc:date>
    <item>
      <title>Bizarre SQL error when building table over itself</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Bizarre-SQL-error-when-building-table-over-itself/m-p/504302#M134936</link>
      <description>&lt;P&gt;Hi, I've come across this unexpected behaviour when writing a table over itself when referencing itself. I know this is generally bad practice and I avoid it whenever I can, however it'd be interesting to see what SAS is trying to do when running the below code.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If you run the below code (I know the join isn't complete for the Shoes table, but no matter) it should be successful.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;FONT face="Courier New" size="1"&gt;PROC SQL;

  CREATE TABLE WORK.DEV AS SELECT Product FROM SASHELP.SHOES;

  CREATE TABLE WORK.DEV AS
  SELECT
    DEV.Product
  FROM
    WORK.DEV
    LEFT JOIN ( SELECT Product FROM WORK.DEV WHERE 1 = 1 ) NQ
    ON DEV.Product = NQ.Product;
QUIT;&lt;/FONT&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;However if you change the "1 = 1" to "1&amp;nbsp;= 0" and rerun, my SAS returns&lt;/P&gt;&lt;P&gt;"ERROR: Close of OUTPUT file WORK.DEV.DATA failed; another copy of the file is still open for INPUT; file not replaced."&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I wouldn't expect this to error (certainly not driven by the trueness/falsehood of the clause).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm using EG7.1 and 9.4, but the above doesn't error on EG5.1/9.3 (we've recently upgraded).&lt;/P&gt;</description>
      <pubDate>Mon, 15 Oct 2018 13:44:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Bizarre-SQL-error-when-building-table-over-itself/m-p/504302#M134936</guid>
      <dc:creator>EvoluZion3</dc:creator>
      <dc:date>2018-10-15T13:44:05Z</dc:date>
    </item>
    <item>
      <title>Re: Bizarre SQL error when building table over itself</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Bizarre-SQL-error-when-building-table-over-itself/m-p/504315#M134940</link>
      <description>&lt;P&gt;Interesting.&amp;nbsp; Replicated on 9.4M3.&amp;nbsp; I wonder if it's a problem of the SQL compiler doing bad short-circuiting when there is an obviously false where clause.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This seems to work okay (ignoring the warning about possibility integrity problems, which seems risky to ignore : ) .&amp;nbsp; I use RANUNI to filter:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;PROC SQL;
  CREATE TABLE WORK.DEV AS SELECT Product FROM SASHELP.SHOES;
  CREATE TABLE WORK.DEV AS
  SELECT
    DEV.Product
  FROM
    WORK.DEV
    LEFT JOIN ( SELECT Product FROM WORK.DEV where ranuni(0)&amp;lt;.5 ) NQ
    ON DEV.Product = NQ.Product;
QUIT;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you change to an expression which will always be false, but the compiler isn't smart enough to know it will always be false, it works:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;PROC SQL;
  CREATE TABLE WORK.DEV AS SELECT Product FROM SASHELP.SHOES;
  CREATE TABLE WORK.DEV AS
  SELECT
    DEV.Product
  FROM
    WORK.DEV
    LEFT JOIN ( SELECT Product FROM WORK.DEV where ranuni(0)&amp;lt; -1 ) NQ
    ON DEV.Product = NQ.Product;
QUIT;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I would think specifying obs=0 would be logically equivalent to WHERE 0, but obs=0 does not cause a problem:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;PROC SQL;
  CREATE TABLE WORK.DEV AS SELECT Product FROM SASHELP.SHOES;
  CREATE TABLE WORK.DEV AS
  SELECT
    DEV.Product
  FROM
    WORK.DEV
    LEFT JOIN ( SELECT Product FROM WORK.DEV (obs=0)) NQ
    ON DEV.Product = NQ.Product;
QUIT;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 15 Oct 2018 14:11:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Bizarre-SQL-error-when-building-table-over-itself/m-p/504315#M134940</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2018-10-15T14:11:18Z</dc:date>
    </item>
    <item>
      <title>Re: Bizarre SQL error when building table over itself</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Bizarre-SQL-error-when-building-table-over-itself/m-p/504317#M134941</link>
      <description>&lt;P&gt;You don't actually need a join for this to happen.&amp;nbsp; A simpler test case is:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;PROC SQL;
  CREATE TABLE WORK.DEV AS SELECT Product FROM SASHELP.SHOES;

  CREATE TABLE WORK.DEV AS
  SELECT
    DEV.Product
  FROM
    WORK.DEV
  WHERE 0
  ;

QUIT;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 15 Oct 2018 14:15:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Bizarre-SQL-error-when-building-table-over-itself/m-p/504317#M134941</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2018-10-15T14:15:03Z</dc:date>
    </item>
    <item>
      <title>Re: Bizarre SQL error when building table over itself</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Bizarre-SQL-error-when-building-table-over-itself/m-p/504326#M134943</link>
      <description>&lt;P&gt;Good catch. Bring it to the attention of SAS technical support.&lt;/P&gt;</description>
      <pubDate>Mon, 15 Oct 2018 14:21:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Bizarre-SQL-error-when-building-table-over-itself/m-p/504326#M134943</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2018-10-15T14:21:24Z</dc:date>
    </item>
    <item>
      <title>Re: Bizarre SQL error when building table over itself</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Bizarre-SQL-error-when-building-table-over-itself/m-p/504344#M134951</link>
      <description>&lt;P&gt;Ok I'll raise this with SAS. Quentin I hope you don't mind if I use the shortened version which you discovered.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This will be the second fault I've found with SAS and it makes me worry about the quality of my SQL haha.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 15 Oct 2018 14:44:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Bizarre-SQL-error-when-building-table-over-itself/m-p/504344#M134951</guid>
      <dc:creator>EvoluZion3</dc:creator>
      <dc:date>2018-10-15T14:44:07Z</dc:date>
    </item>
    <item>
      <title>Re: Bizarre SQL error when building table over itself</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Bizarre-SQL-error-when-building-table-over-itself/m-p/504366#M134958</link>
      <description>Apparently this has already been raised:&lt;BR /&gt;&lt;A href="http://support.sas.com/techsup/notes/v8/12/330.html" target="_blank"&gt;http://support.sas.com/techsup/notes/v8/12/330.html&lt;/A&gt;</description>
      <pubDate>Mon, 15 Oct 2018 15:49:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Bizarre-SQL-error-when-building-table-over-itself/m-p/504366#M134958</guid>
      <dc:creator>EvoluZion3</dc:creator>
      <dc:date>2018-10-15T15:49:27Z</dc:date>
    </item>
    <item>
      <title>Re: Bizarre SQL error when building table over itself</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Bizarre-SQL-error-when-building-table-over-itself/m-p/504422#M134984</link>
      <description>&lt;P&gt;Thanks for posting the resolution.&amp;nbsp; I suggest you accept your post with the link to the tech support note as the solution.&amp;nbsp; At least an explanation. : )&lt;/P&gt;</description>
      <pubDate>Mon, 15 Oct 2018 18:25:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Bizarre-SQL-error-when-building-table-over-itself/m-p/504422#M134984</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2018-10-15T18:25:55Z</dc:date>
    </item>
  </channel>
</rss>

