<?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 PROC SQL - Get count, iterate through a table that many times, delete records based on conditions in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-Get-count-iterate-through-a-table-that-many-times/m-p/708387#M217699</link>
    <description>&lt;P&gt;&lt;FONT face="andale mono,times"&gt;Hi,&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="andale mono,times"&gt;I have 2 tables (whole_set &amp;amp; mismatches) derived from 2 data steps (abc_vers &amp;amp; xyz_vers), as below:&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT face="andale mono,times" size="3" color="#0000FF"&gt;title ' full set '; &lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="andale mono,times" size="3" color="#0000FF"&gt;create table whole_set as &lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="andale mono,times" size="3" color="#0000FF"&gt;select source, rec_no, rec_01 from &lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="andale mono,times" size="3" color="#0000FF"&gt;( &lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="andale mono,times" size="3" color="#0000FF"&gt;select 'abc', monotonic() as rec_no, rec_01 from abc_vers&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="andale mono,times" size="3" color="#0000FF"&gt;union &lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="andale mono,times" size="3" color="#0000FF"&gt;select 'xyz', monotonic() as rec_no, rec_01 from xyz_vers&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="andale mono,times" size="3" color="#0000FF"&gt;) &lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="andale mono,times" size="3" color="#0000FF"&gt;as tbl01 (source, rec_no, rec_01) &lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="andale mono,times" size="3" color="#0000FF"&gt;where source = 'abc' &lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="andale mono,times" size="3" color="#0000FF"&gt;group by rec_01 &lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="andale mono,times" size="3" color="#0000FF"&gt;having count(*) &amp;lt;= 2 &lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="andale mono,times" size="3" color="#0000FF"&gt;order by rec_no &lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="andale mono,times" size="3" color="#0000FF"&gt;;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT face="andale mono,times" size="3" color="#0000FF"&gt;title ' mismatch set ';&lt;BR /&gt;create table mismatches as&lt;BR /&gt;select * from&lt;BR /&gt;(&lt;BR /&gt;select 'rowz2add', rec_01 from fis_vers&lt;BR /&gt;except&lt;BR /&gt;select 'rowz2add', rec_01 from nab_vers&lt;BR /&gt;union&lt;BR /&gt;select 'rowz2del', rec_01 from nab_vers&lt;BR /&gt;where substr(rec_01,66,1) = 'L'&lt;BR /&gt;and substr(rec_01,67,1) in ('0','1','2','3','4','5','6','7','8','9')&lt;BR /&gt;and substr(rec_01,68,1) in ('0','1','2','3','4','5','6','7','8','9')&lt;BR /&gt;and substr(rec_01,69,1) in ('0','1','2','3','4','5','6','7','8','9')&lt;BR /&gt;except&lt;BR /&gt;select 'rowz2del', rec_01 from fis_vers&lt;BR /&gt;where substr(rec_01,66,1) = 'L'&lt;BR /&gt;and substr(rec_01,67,1) in ('0','1','2','3','4','5','6','7','8','9')&lt;BR /&gt;and substr(rec_01,68,1) in ('0','1','2','3','4','5','6','7','8','9')&lt;BR /&gt;and substr(rec_01,69,1) in ('0','1','2','3','4','5','6','7','8','9')&lt;BR /&gt;)&lt;BR /&gt;as tbl01&lt;BR /&gt;(&lt;BR /&gt;action, rec_01&lt;BR /&gt;);&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;&lt;FONT face="andale mono,times" size="3" color="#000000"&gt;My requirement - for every record in the "mismatches" table, its equivalent records to be deleted from the "whole_set" table...&lt;/FONT&gt;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT face="andale mono,times" size="3" color="#000000"&gt;Used the "Delete" query below and it works when the 'Sub-Query" fetches 1 record but flops when there are multiple records:&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="andale mono,times" size="3" color="#0000FF"&gt;delete from whole_set&lt;BR /&gt;where rec_01 = (select rec_01 from mismatches where action = 'rowz2del') ;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT face="andale mono,times" size="3" color="#000000"&gt;Is there a way to iterate (in PROC SQL) through the rows and delete them from the whole_set (something like - get the count from mismatches table and then a 'DO' loop until all the records are deleted) ?&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT face="andale mono,times" size="3" color="#000000"&gt;Thanks,&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="andale mono,times" size="3" color="#000000"&gt;Mohan&lt;/FONT&gt;&lt;/P&gt;</description>
    <pubDate>Mon, 28 Dec 2020 07:09:38 GMT</pubDate>
    <dc:creator>Mohan03</dc:creator>
    <dc:date>2020-12-28T07:09:38Z</dc:date>
    <item>
      <title>PROC SQL - Get count, iterate through a table that many times, delete records based on conditions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-Get-count-iterate-through-a-table-that-many-times/m-p/708387#M217699</link>
      <description>&lt;P&gt;&lt;FONT face="andale mono,times"&gt;Hi,&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="andale mono,times"&gt;I have 2 tables (whole_set &amp;amp; mismatches) derived from 2 data steps (abc_vers &amp;amp; xyz_vers), as below:&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT face="andale mono,times" size="3" color="#0000FF"&gt;title ' full set '; &lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="andale mono,times" size="3" color="#0000FF"&gt;create table whole_set as &lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="andale mono,times" size="3" color="#0000FF"&gt;select source, rec_no, rec_01 from &lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="andale mono,times" size="3" color="#0000FF"&gt;( &lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="andale mono,times" size="3" color="#0000FF"&gt;select 'abc', monotonic() as rec_no, rec_01 from abc_vers&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="andale mono,times" size="3" color="#0000FF"&gt;union &lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="andale mono,times" size="3" color="#0000FF"&gt;select 'xyz', monotonic() as rec_no, rec_01 from xyz_vers&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="andale mono,times" size="3" color="#0000FF"&gt;) &lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="andale mono,times" size="3" color="#0000FF"&gt;as tbl01 (source, rec_no, rec_01) &lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="andale mono,times" size="3" color="#0000FF"&gt;where source = 'abc' &lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="andale mono,times" size="3" color="#0000FF"&gt;group by rec_01 &lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="andale mono,times" size="3" color="#0000FF"&gt;having count(*) &amp;lt;= 2 &lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="andale mono,times" size="3" color="#0000FF"&gt;order by rec_no &lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="andale mono,times" size="3" color="#0000FF"&gt;;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT face="andale mono,times" size="3" color="#0000FF"&gt;title ' mismatch set ';&lt;BR /&gt;create table mismatches as&lt;BR /&gt;select * from&lt;BR /&gt;(&lt;BR /&gt;select 'rowz2add', rec_01 from fis_vers&lt;BR /&gt;except&lt;BR /&gt;select 'rowz2add', rec_01 from nab_vers&lt;BR /&gt;union&lt;BR /&gt;select 'rowz2del', rec_01 from nab_vers&lt;BR /&gt;where substr(rec_01,66,1) = 'L'&lt;BR /&gt;and substr(rec_01,67,1) in ('0','1','2','3','4','5','6','7','8','9')&lt;BR /&gt;and substr(rec_01,68,1) in ('0','1','2','3','4','5','6','7','8','9')&lt;BR /&gt;and substr(rec_01,69,1) in ('0','1','2','3','4','5','6','7','8','9')&lt;BR /&gt;except&lt;BR /&gt;select 'rowz2del', rec_01 from fis_vers&lt;BR /&gt;where substr(rec_01,66,1) = 'L'&lt;BR /&gt;and substr(rec_01,67,1) in ('0','1','2','3','4','5','6','7','8','9')&lt;BR /&gt;and substr(rec_01,68,1) in ('0','1','2','3','4','5','6','7','8','9')&lt;BR /&gt;and substr(rec_01,69,1) in ('0','1','2','3','4','5','6','7','8','9')&lt;BR /&gt;)&lt;BR /&gt;as tbl01&lt;BR /&gt;(&lt;BR /&gt;action, rec_01&lt;BR /&gt;);&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;&lt;FONT face="andale mono,times" size="3" color="#000000"&gt;My requirement - for every record in the "mismatches" table, its equivalent records to be deleted from the "whole_set" table...&lt;/FONT&gt;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT face="andale mono,times" size="3" color="#000000"&gt;Used the "Delete" query below and it works when the 'Sub-Query" fetches 1 record but flops when there are multiple records:&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="andale mono,times" size="3" color="#0000FF"&gt;delete from whole_set&lt;BR /&gt;where rec_01 = (select rec_01 from mismatches where action = 'rowz2del') ;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT face="andale mono,times" size="3" color="#000000"&gt;Is there a way to iterate (in PROC SQL) through the rows and delete them from the whole_set (something like - get the count from mismatches table and then a 'DO' loop until all the records are deleted) ?&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT face="andale mono,times" size="3" color="#000000"&gt;Thanks,&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="andale mono,times" size="3" color="#000000"&gt;Mohan&lt;/FONT&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 28 Dec 2020 07:09:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-Get-count-iterate-through-a-table-that-many-times/m-p/708387#M217699</guid>
      <dc:creator>Mohan03</dc:creator>
      <dc:date>2020-12-28T07:09:38Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SQL - Get count, iterate through a table that many times, delete records based on condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-Get-count-iterate-through-a-table-that-many-times/m-p/708391#M217700</link>
      <description>&lt;P&gt;Just use the IN operator:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;where rec_01 in (select rec_01 from mismatches where action = 'rowz2del')&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 28 Dec 2020 08:02:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-Get-count-iterate-through-a-table-that-many-times/m-p/708391#M217700</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-12-28T08:02:25Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SQL - Get count, iterate through a table that many times, delete records based on condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-Get-count-iterate-through-a-table-that-many-times/m-p/708395#M217701</link>
      <description>&lt;P&gt;&lt;FONT face="andale mono,times"&gt;It worked perfectly as expected.&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT face="andale mono,times"&gt;Thank you&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt;&amp;nbsp;for the sleek solution.&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT face="andale mono,times"&gt;Thanks,&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="andale mono,times"&gt;Mohan.&lt;/FONT&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 28 Dec 2020 08:26:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-Get-count-iterate-through-a-table-that-many-times/m-p/708395#M217701</guid>
      <dc:creator>Mohan03</dc:creator>
      <dc:date>2020-12-28T08:26:04Z</dc:date>
    </item>
  </channel>
</rss>

