<?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: Using Proc SQL to delete rows with a where and having clause in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/Using-Proc-SQL-to-delete-rows-with-a-where-and-having-clause/m-p/175661#M45060</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;The SQL code works for SELECT and not for DELETE because delete operations modify the set in which aggregate operations occur, and thus, their result. &lt;/P&gt;&lt;P&gt;What if&lt;SPAN style="font-family: 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; font-size: 13px; background-color: #ffffff;"&gt;&lt;STRONG&gt; min(tariff_dim_key)&lt;/STRONG&gt; is not the same after you do the first deletion? Should it be evaluated again? Different answers to this question would lead to different results. SQL standard designers have probably chosen the safest approach to prevent inconsistencies between DBMSs.&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="font-size: 13px; font-family: 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; background-color: #ffffff;"&gt;&lt;/P&gt;&lt;P style="font-size: 13px; font-family: 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; background-color: #ffffff;"&gt;PG&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Fri, 20 Feb 2015 20:07:06 GMT</pubDate>
    <dc:creator>PGStats</dc:creator>
    <dc:date>2015-02-20T20:07:06Z</dc:date>
    <item>
      <title>Using Proc SQL to delete rows with a where and having clause</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Using-Proc-SQL-to-delete-rows-with-a-where-and-having-clause/m-p/175658#M45057</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;What I'd like to work is this:&lt;/P&gt;&lt;P&gt;proc sql;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; delete from odbclib.cp_tariff_dim&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; where tariff_initial = 'NS'&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; and tariff_item = '0010100000;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; having min(tariff_dim_key) ne tariff_dim_key&lt;/P&gt;&lt;P&gt;; quit;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;This code works perfectly to create a data set as in select * from etc.&lt;/P&gt;&lt;P&gt;It creates a data set of all the rows I want to delete. But if I run the above I get an "ERROR 22-322: Syntax error...".&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Is there a way to use a 'having' clause with the where in order to delete rows from a SQL Server table?&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 19 Feb 2015 21:31:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Using-Proc-SQL-to-delete-rows-with-a-where-and-having-clause/m-p/175658#M45057</guid>
      <dc:creator>kcskaiser</dc:creator>
      <dc:date>2015-02-19T21:31:22Z</dc:date>
    </item>
    <item>
      <title>Re: Using Proc SQL to delete rows with a where and having clause</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Using-Proc-SQL-to-delete-rows-with-a-where-and-having-clause/m-p/175659#M45058</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;A point of view.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;data t_a;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;input a1 a2;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;1 10&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;1 11&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;1 12&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;1 13&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;2 11&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;2 15&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;2 17&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;proc sql;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;&amp;nbsp; delete from t_a a&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;&amp;nbsp; where a.a1 in (select a1 from&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN style="font-family: courier new,courier;"&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; (select b.a1, min(a2) as min_a2 &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN style="font-family: courier new,courier;"&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; from t_a b &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN style="font-family: courier new,courier;"&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; group by b.a1 &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN style="font-family: courier new,courier;"&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; having min(a2)=10));&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;quit;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;I end up with t_a becoming:&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;=============&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;a1&amp;nbsp;&amp;nbsp;&amp;nbsp; a2&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt; 2&amp;nbsp;&amp;nbsp;&amp;nbsp; 11&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN style="font-family: courier new,courier;"&gt; 2&amp;nbsp;&amp;nbsp;&amp;nbsp; 15&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN style="font-family: courier new,courier;"&gt; 2&amp;nbsp;&amp;nbsp;&amp;nbsp; 17&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;=============&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;But SAS gives a warning:&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;WARNING: This DELETE/INSERT statement recursively references the target table. A consequence of this is a possible data integrity &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; problem.&lt;/SPAN&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 19 Feb 2015 23:55:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Using-Proc-SQL-to-delete-rows-with-a-where-and-having-clause/m-p/175659#M45058</guid>
      <dc:creator>billfish</dc:creator>
      <dc:date>2015-02-19T23:55:00Z</dc:date>
    </item>
    <item>
      <title>Re: Using Proc SQL to delete rows with a where and having clause</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Using-Proc-SQL-to-delete-rows-with-a-where-and-having-clause/m-p/175660#M45059</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hmmm, very interesting approach. After adding a 'datalines' on your input I was able to reproduce your output. Additionally your solution isn't working correctly anyway because you should end up with just the first row (min(a2)=10), not three or four (min(a2) ge 10).&amp;nbsp; I get the same warning and like you it concerns me.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;My table is a bit more complex so my actual code would need to look something like this:&lt;/P&gt;&lt;P&gt;proc sql;&lt;/P&gt;&lt;P&gt;&amp;nbsp; delete from cp_tariff_dim_backup a&lt;/P&gt;&lt;P&gt;&amp;nbsp; where a.TARIFF_INITIAL = 'NS'&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; and a.TARIFF_ITEM = '0010100000'&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; and a.TARIFF_NBR = '00025'&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; and a.SRC_SYS = 'KCS'&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; and a.TARIFF_ISS_ID = 'NSPQ' &lt;/P&gt;&lt;P&gt;&amp;nbsp; in (select tariff_dim_key from &lt;/P&gt;&lt;P&gt;&amp;nbsp; (select b.tariff_dim_key, min(tariff_dim_key) as min_dim&lt;/P&gt;&lt;P&gt;&amp;nbsp; from cp_tariff_dim_backup b&lt;/P&gt;&lt;P&gt;&amp;nbsp; group by b.tariff_dim_key&lt;/P&gt;&lt;P&gt;&amp;nbsp; having min(a.tariff_dim_key) &amp;lt;&amp;gt; b.tariff_dim_key))&lt;/P&gt;&lt;P&gt;&amp;nbsp; ;&lt;/P&gt;&lt;P&gt;quit;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Which gives me an 'ERROR 22-322: Syntax error..'.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Oddly enough (at least I find it odd) if you surround the condition checks in the where clause with parens () the syntax error goes away but no rows are deleted.&lt;/P&gt;&lt;P&gt;where (a.TARIFF_INITIAL = 'NS'&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; and a.TARIFF_ITEM = '0010100000'&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; and a.TARIFF_NBR = '00025'&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; and a.SRC_SYS = 'KCS'&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; and a.TARIFF_ISS_ID = 'NSPQ')&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thoughts?&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 20 Feb 2015 16:46:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Using-Proc-SQL-to-delete-rows-with-a-where-and-having-clause/m-p/175660#M45059</guid>
      <dc:creator>kcskaiser</dc:creator>
      <dc:date>2015-02-20T16:46:19Z</dc:date>
    </item>
    <item>
      <title>Re: Using Proc SQL to delete rows with a where and having clause</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Using-Proc-SQL-to-delete-rows-with-a-where-and-having-clause/m-p/175661#M45060</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;The SQL code works for SELECT and not for DELETE because delete operations modify the set in which aggregate operations occur, and thus, their result. &lt;/P&gt;&lt;P&gt;What if&lt;SPAN style="font-family: 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; font-size: 13px; background-color: #ffffff;"&gt;&lt;STRONG&gt; min(tariff_dim_key)&lt;/STRONG&gt; is not the same after you do the first deletion? Should it be evaluated again? Different answers to this question would lead to different results. SQL standard designers have probably chosen the safest approach to prevent inconsistencies between DBMSs.&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="font-size: 13px; font-family: 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; background-color: #ffffff;"&gt;&lt;/P&gt;&lt;P style="font-size: 13px; font-family: 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; background-color: #ffffff;"&gt;PG&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 20 Feb 2015 20:07:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Using-Proc-SQL-to-delete-rows-with-a-where-and-having-clause/m-p/175661#M45060</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2015-02-20T20:07:06Z</dc:date>
    </item>
    <item>
      <title>Re: Using Proc SQL to delete rows with a where and having clause</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Using-Proc-SQL-to-delete-rows-with-a-where-and-having-clause/m-p/175662#M45061</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;You have a logical problem &lt;A href="http://en.wikipedia.org/wiki/ACID" title="http://en.wikipedia.org/wiki/ACID"&gt;ACID - Wikipedia, the free encyclopedia&lt;/A&gt; as deleting records that could add to the value of the aggregated function.&lt;/P&gt;&lt;P&gt;That is why you are getting: &lt;/P&gt;&lt;P&gt;WARNING: This DELETE/INSERT statement recursively references the target table. A consequence of this is a possible data integrity&amp;nbsp;&amp;nbsp; problem.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 21 Feb 2015 11:15:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Using-Proc-SQL-to-delete-rows-with-a-where-and-having-clause/m-p/175662#M45061</guid>
      <dc:creator>jakarman</dc:creator>
      <dc:date>2015-02-21T11:15:21Z</dc:date>
    </item>
  </channel>
</rss>

