<?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: delete rows with two rows condition after group by in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/delete-rows-with-two-rows-condition-after-group-by/m-p/647190#M78601</link>
    <description>&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/138205"&gt;@novinosrin&lt;/a&gt; I have fully understood your code now! looks like I can't run the part1 only, it will give me a false result which doesn't interpret step 1 correctly.&lt;BR /&gt;&lt;BR /&gt;What you did is a great help to me and my project! Thank you so much! Wish you have a great day!</description>
    <pubDate>Tue, 12 May 2020 18:23:43 GMT</pubDate>
    <dc:creator>yliu1234</dc:creator>
    <dc:date>2020-05-12T18:23:43Z</dc:date>
    <item>
      <title>delete rows with two rows condition after group by</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/delete-rows-with-two-rows-condition-after-group-by/m-p/646696#M78576</link>
      <description>&lt;P&gt;I have a transaction table, for the same account and same amount, if there are one debit and one credit transactions, then delete both.&amp;nbsp; Each debit transaction should cancel out one equal amount credit transaction of the same account, the sequence doesn't matter.&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;TABLE border="1"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;account_id&lt;/TD&gt;&lt;TD&gt;amount&lt;/TD&gt;&lt;TD&gt;type&lt;/TD&gt;&lt;TD&gt;trans_seq&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;001&lt;/TD&gt;&lt;TD&gt;100&lt;/TD&gt;&lt;TD&gt;credit&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;001&lt;/TD&gt;&lt;TD&gt;100&lt;/TD&gt;&lt;TD&gt;credit&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;001&lt;/TD&gt;&lt;TD&gt;100&lt;/TD&gt;&lt;TD&gt;debit&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;001&lt;/TD&gt;&lt;TD&gt;100&lt;/TD&gt;&lt;TD&gt;debit&lt;/TD&gt;&lt;TD&gt;32&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;001&lt;/TD&gt;&lt;TD&gt;50&lt;/TD&gt;&lt;TD&gt;credit&lt;/TD&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;002&lt;/TD&gt;&lt;TD&gt;30&lt;/TD&gt;&lt;TD&gt;credit&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;003&lt;/TD&gt;&lt;TD&gt;50&lt;/TD&gt;&lt;TD&gt;credit&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;003&lt;/TD&gt;&lt;TD&gt;20&lt;/TD&gt;&lt;TD&gt;debit&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;000123abc&lt;/TD&gt;&lt;TD&gt;104.42&lt;/TD&gt;&lt;TD&gt;credit&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;000123abc&lt;/TD&gt;&lt;TD&gt;104.42&lt;/TD&gt;&lt;TD&gt;credit&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;000123abc&lt;/TD&gt;&lt;TD&gt;104.42&lt;/TD&gt;&lt;TD&gt;debit&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;004&lt;/TD&gt;&lt;TD&gt;10&lt;/TD&gt;&lt;TD&gt;credit&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;004&lt;/TD&gt;&lt;TD&gt;10&lt;/TD&gt;&lt;TD&gt;credit&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;005&lt;/TD&gt;&lt;TD&gt;10&lt;/TD&gt;&lt;TD&gt;debit&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;005&lt;/TD&gt;&lt;TD&gt;10&lt;/TD&gt;&lt;TD&gt;debit&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;005&lt;/TD&gt;&lt;TD&gt;10&lt;/TD&gt;&lt;TD&gt;debit&lt;/TD&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data tran;&lt;BR /&gt;input account_id $ amount type $ trans_seq;&lt;BR /&gt;cards;&lt;BR /&gt;001 100 credit 1&lt;BR /&gt;001 100 credit 2&lt;BR /&gt;001 100 debit 1&lt;BR /&gt;001 100 debit 32&lt;BR /&gt;001 50 credit 3&lt;BR /&gt;002 30 credit 1&lt;BR /&gt;003 50 credit 1&lt;BR /&gt;003 20 debit 1&lt;/P&gt;&lt;P&gt;000123abc 104.42 credit 2&lt;BR /&gt;000123abc 104.42 credit 14&lt;BR /&gt;000123abc 104.42 debit 2&lt;/P&gt;&lt;P&gt;004 10 credit 1&lt;/P&gt;&lt;P&gt;004 10 credit 2&lt;/P&gt;&lt;P&gt;005 10 debit 1&lt;/P&gt;&lt;P&gt;005 10 debit 2&lt;/P&gt;&lt;P&gt;005 10 debit 3;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;the table I want&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;TABLE border="1"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;account_id&lt;/TD&gt;&lt;TD&gt;amount&lt;/TD&gt;&lt;TD&gt;type&lt;/TD&gt;&lt;TD&gt;trans_seq&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;001&lt;/TD&gt;&lt;TD&gt;50&lt;/TD&gt;&lt;TD&gt;credit&lt;/TD&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;002&lt;/TD&gt;&lt;TD&gt;30&lt;/TD&gt;&lt;TD&gt;credit&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;003&lt;/TD&gt;&lt;TD&gt;50&lt;/TD&gt;&lt;TD&gt;credit&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;003&lt;/TD&gt;&lt;TD&gt;20&lt;/TD&gt;&lt;TD&gt;debit&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;000123abc&lt;/TD&gt;&lt;TD&gt;104.42&lt;/TD&gt;&lt;TD&gt;credit&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;004&lt;/TD&gt;&lt;TD&gt;10&lt;/TD&gt;&lt;TD&gt;credit&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;004&lt;/TD&gt;&lt;TD&gt;10&lt;/TD&gt;&lt;TD&gt;credit&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;005&lt;/TD&gt;&lt;TD&gt;10&lt;/TD&gt;&lt;TD&gt;debit&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;005&lt;/TD&gt;&lt;TD&gt;10&lt;/TD&gt;&lt;TD&gt;debit&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;005&lt;/TD&gt;&lt;TD&gt;10&lt;/TD&gt;&lt;TD&gt;debit&lt;/TD&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;</description>
      <pubDate>Mon, 11 May 2020 18:19:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/delete-rows-with-two-rows-condition-after-group-by/m-p/646696#M78576</guid>
      <dc:creator>yliu1234</dc:creator>
      <dc:date>2020-05-11T18:19:52Z</dc:date>
    </item>
    <item>
      <title>Re: delete rows with two rows condition after group by</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/delete-rows-with-two-rows-condition-after-group-by/m-p/646736#M78580</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input account_id	amount	type $	trans_seq;
cards;
001	100	credit	1
001	100	credit	2
001	100	debit	1
001	100	debit	32
001	50	credit	3
002	30	credit	1
003	50	credit	1
003	20	debit	1
;

data want;
 if _n_=1 then do;
  dcl hash H (multidata:'y') ;
  h.definekey  ("amount") ;
  h.definedone () ;
 end;
 do _n_=1 by 1 until(last.account_id);
  set have;
  by account_id;
  if type="credit" then h.add();
  else if type="debit" then do;
   if h.check()=0 then rc=h.removedup();
   else h.add();
  end;
 end;
 do _n_=1 to _n_;
  set have;
  if h.check()= 0 then output;
 end;
 h.clear();
 drop rc;
run;

proc print noobs;run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;DIV class="branch"&gt;
&lt;DIV&gt;
&lt;DIV align="center"&gt;
&lt;TABLE class="table" summary="Procedure Print: Data Set WORK.WANT" frame="box" rules="all" cellspacing="0" cellpadding="5"&gt;
&lt;THEAD&gt;
&lt;TR&gt;
&lt;TH class="r header" scope="col"&gt;account_id&lt;/TH&gt;
&lt;TH class="r header" scope="col"&gt;amount&lt;/TH&gt;
&lt;TH class="l header" scope="col"&gt;type&lt;/TH&gt;
&lt;TH class="r header" scope="col"&gt;trans_seq&lt;/TH&gt;
&lt;/TR&gt;
&lt;/THEAD&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD class="r data"&gt;1&lt;/TD&gt;
&lt;TD class="r data"&gt;50&lt;/TD&gt;
&lt;TD class="l data"&gt;credit&lt;/TD&gt;
&lt;TD class="r data"&gt;3&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class="r data"&gt;2&lt;/TD&gt;
&lt;TD class="r data"&gt;30&lt;/TD&gt;
&lt;TD class="l data"&gt;credit&lt;/TD&gt;
&lt;TD class="r data"&gt;1&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class="r data"&gt;3&lt;/TD&gt;
&lt;TD class="r data"&gt;50&lt;/TD&gt;
&lt;TD class="l data"&gt;credit&lt;/TD&gt;
&lt;TD class="r data"&gt;1&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class="r data"&gt;3&lt;/TD&gt;
&lt;TD class="r data"&gt;20&lt;/TD&gt;
&lt;TD class="l data"&gt;debit&lt;/TD&gt;
&lt;TD class="r data"&gt;1&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;</description>
      <pubDate>Mon, 11 May 2020 14:54:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/delete-rows-with-two-rows-condition-after-group-by/m-p/646736#M78580</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2020-05-11T14:54:38Z</dc:date>
    </item>
    <item>
      <title>Re: delete rows with two rows condition after group by</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/delete-rows-with-two-rows-condition-after-group-by/m-p/646758#M78581</link>
      <description>novinosrin, thank you very much. I am very new to SAS, could you add a little bit explanation of your code or your logic?&lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Mon, 11 May 2020 15:17:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/delete-rows-with-two-rows-condition-after-group-by/m-p/646758#M78581</guid>
      <dc:creator>yliu1234</dc:creator>
      <dc:date>2020-05-11T15:17:40Z</dc:date>
    </item>
    <item>
      <title>Re: delete rows with two rows condition after group by</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/delete-rows-with-two-rows-condition-after-group-by/m-p/646764#M78584</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/328276"&gt;@yliu1234&lt;/a&gt;&amp;nbsp;It's a simple look up exercise to begin with. The logic construct deals with &lt;EM&gt;pairwise&lt;/EM&gt; look-up. A pair is defined as a combination of credit, debit for a particular amount. You can imagine this as one group. The parent group is your account_id within which each pair of equal credit and debit amount is constituted as child group.&amp;nbsp; In other words, you have something like sets of&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Group by Accountid, Amount, (credit &amp;amp; debit) &amp;nbsp;=&amp;gt;sets&amp;nbsp;of&amp;nbsp;two&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You create a memory resident(Hash) look up table and retain the records that do not conform to the defined pairing logic i.e. by excluding the pairs. You write the retained records to the resulting dataset and clear the contents of Hash memory once the processing for each by group is complete. The same is repeated for each Accountid and we are done!&lt;/P&gt;</description>
      <pubDate>Mon, 11 May 2020 15:29:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/delete-rows-with-two-rows-condition-after-group-by/m-p/646764#M78584</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2020-05-11T15:29:07Z</dc:date>
    </item>
    <item>
      <title>Re: delete rows with two rows condition after group by</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/delete-rows-with-two-rows-condition-after-group-by/m-p/646811#M78586</link>
      <description>&lt;P&gt;my account_id is saved as character, I add three rows here, it will not work.&amp;nbsp; Should I add a number index for my account_id? I wonder how to do this?&lt;/P&gt;</description>
      <pubDate>Mon, 11 May 2020 17:19:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/delete-rows-with-two-rows-condition-after-group-by/m-p/646811#M78586</guid>
      <dc:creator>yliu1234</dc:creator>
      <dc:date>2020-05-11T17:19:59Z</dc:date>
    </item>
    <item>
      <title>Re: delete rows with two rows condition after group by</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/delete-rows-with-two-rows-condition-after-group-by/m-p/646819#M78587</link>
      <description>&lt;P&gt;Okay, It's not about character or numeric that caused the problem. It's about the duplicate credit of 104.42. No worries, I am a little busy now. I will respond to you by the evening.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also,&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/328276"&gt;@yliu1234&lt;/a&gt;&amp;nbsp; I have understood that one scenario. However, Can you please post a comprehensive sample of &lt;STRONG&gt;all &lt;U&gt;possible scenarios of transaction pattern&amp;nbsp;&lt;/U&gt;&lt;/STRONG&gt;with the expected output?&lt;/P&gt;</description>
      <pubDate>Mon, 11 May 2020 17:44:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/delete-rows-with-two-rows-condition-after-group-by/m-p/646819#M78587</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2020-05-11T17:44:11Z</dc:date>
    </item>
    <item>
      <title>Re: delete rows with two rows condition after group by</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/delete-rows-with-two-rows-condition-after-group-by/m-p/646843#M78588</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/328276"&gt;@yliu1234&lt;/a&gt;&amp;nbsp; Please try the modified code below and let me know. I believe this should do it.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data tran;
input account_id $ amount type $ trans_seq;
cards;
001 100 credit 1
001 100 credit 2
001 100 debit 1
001 100 debit 32
001 50 credit 3
002 30 credit 1
003 50 credit 1
003 20 debit 1
000123abc 104.42 credit 2
000123abc 104.42 credit 14
000123abc 104.42 debit 2
;
RUN;

data want;
 if _n_=1 then do;
   dcl hash H (multidata:'y') ;
   h.definekey  ("_amount") ;
   h.definedone () ;
 end;
 do _n_=1 by 1 until(last.account_id);
  set tran;
  by account_id notsorted;
  _amount=ifn(type='credit',amount,-amount);
  if type="credit" then h.add();
  else if type="debit" then do;
   if h.check(key:abs(_amount))=0 then rc=h.removedup(key:abs(_amount));
   else h.add();
  end;
 end;
 do _n_=1 to _n_;
  set tran;
  _amount=ifn(type='credit',amount,-amount);
  if h.check()=0 then do;
   output;
   rc=h.removedup();
  end;
 end;
 h.clear();
 drop rc _:;
run;
proc print noobs;run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 11 May 2020 18:18:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/delete-rows-with-two-rows-condition-after-group-by/m-p/646843#M78588</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2020-05-11T18:18:02Z</dc:date>
    </item>
    <item>
      <title>Re: delete rows with two rows condition after group by</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/delete-rows-with-two-rows-condition-after-group-by/m-p/646875#M78589</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/138205"&gt;@novinosrin&lt;/a&gt;&amp;nbsp;you code works very well in my sample, however, my account_id is very long, it has 32 digit. like this abc12345678901234567890123456789. I applied your logic to my real data, it gives me a empty table as result.&lt;/P&gt;</description>
      <pubDate>Mon, 11 May 2020 20:17:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/delete-rows-with-two-rows-condition-after-group-by/m-p/646875#M78589</guid>
      <dc:creator>yliu1234</dc:creator>
      <dc:date>2020-05-11T20:17:17Z</dc:date>
    </item>
    <item>
      <title>Re: delete rows with two rows condition after group by</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/delete-rows-with-two-rows-condition-after-group-by/m-p/646878#M78590</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/328276"&gt;@yliu1234&lt;/a&gt;&amp;nbsp; I really am not sure if the long account_id has anything to do with it unless of course if your sample is not good representative of the real. The logic essentially is still the same with just minor tweaks. Hmm, it may be likely that your amount decimal value perhaps is the causing the problem and may need rounding?You could however round the value to the nearest 100th with round function like &lt;EM&gt;new_amount=round(amount,.01).&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also, try to debug using just one account_id and see what works and what isn't working. I would like to work on a simpler multistep logic but I am a little way of what's in your data at this point. Is it something slipping through the cracks in the communication?&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 11 May 2020 20:28:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/delete-rows-with-two-rows-condition-after-group-by/m-p/646878#M78590</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2020-05-11T20:28:49Z</dc:date>
    </item>
    <item>
      <title>Re: delete rows with two rows condition after group by</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/delete-rows-with-two-rows-condition-after-group-by/m-p/647128#M78598</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/138205"&gt;@novinosrin&lt;/a&gt;&amp;nbsp;the code works good now! The error previously was due to failure of changing 'type' to the real column name .&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have studied some Dash document to help me understand your code.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt; do _n_=1 by 1 until(last.account_id);
  set tran;
  by account_id notsorted;
  _amount=ifn(type='credit',amount,-amount);
  if type="credit" then h.add();
  else if type="debit" then do;
   if h.check(key:abs(_amount))=0 then rc=h.removedup(key:abs(_amount));
   else h.add();&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Then I ran this part only to understand your&amp;nbsp; logic.&lt;/P&gt;&lt;P&gt;if you find a credit, you add it to hash table,&lt;/P&gt;&lt;P&gt;if you find a debit, you check whether there is a credit or debit already exist, if exist, delete all previous ones with removedup(), if not, add it here? However, the result I get is like this, it doesn't follow my logic.&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;account_id&lt;/TD&gt;&lt;TD&gt;amount&lt;/TD&gt;&lt;TD&gt;type&lt;/TD&gt;&lt;TD&gt;trans_seq&lt;/TD&gt;&lt;TD&gt;_amount&lt;/TD&gt;&lt;TD&gt;rc&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;000123ab&lt;/TD&gt;&lt;TD&gt;104.42&lt;/TD&gt;&lt;TD&gt;debit&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;-104.42&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;001&lt;/TD&gt;&lt;TD&gt;100&lt;/TD&gt;&lt;TD&gt;debit&lt;/TD&gt;&lt;TD&gt;32&lt;/TD&gt;&lt;TD&gt;-100&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;002&lt;/TD&gt;&lt;TD&gt;30&lt;/TD&gt;&lt;TD&gt;credit&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;30&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;003&lt;/TD&gt;&lt;TD&gt;50&lt;/TD&gt;&lt;TD&gt;credit&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;50&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;004&lt;/TD&gt;&lt;TD&gt;10&lt;/TD&gt;&lt;TD&gt;credit&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;10&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;005&lt;/TD&gt;&lt;TD&gt;10&lt;/TD&gt;&lt;TD&gt;debit&lt;/TD&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;-10&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;do _n_=1 to _n_;
  set tran;
  _amount=ifn(type='credit',amount,-amount);
  if h.check()=0 then do;
   output;
   rc=h.removedup();&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;the second part of your code, you checked the tran table again, but without groupby account_id? if one _amount(credit positive, debit negative) exist already in &lt;STRONG&gt;the previous hash table&lt;/STRONG&gt;, then output -- write it to want table? then remove previous record with the same amount? Since it is not grouped by account_id, which means the same amount of different user will works here too.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Could you correct me where did I read wrong? since your code works very well.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;by the way I add this before your code, to make sure that credit always shows first than debit.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;PROC SORT data=tran;
    BY account_id amount type;
RUN;&lt;/CODE&gt;&amp;nbsp;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 12 May 2020 15:25:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/delete-rows-with-two-rows-condition-after-group-by/m-p/647128#M78598</guid>
      <dc:creator>yliu1234</dc:creator>
      <dc:date>2020-05-12T15:25:42Z</dc:date>
    </item>
    <item>
      <title>Re: delete rows with two rows condition after group by</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/delete-rows-with-two-rows-condition-after-group-by/m-p/647142#M78599</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/328276"&gt;@yliu1234&lt;/a&gt;&amp;nbsp; Thank you for the responses and I'm very glad you are being receptive. Basically your understanding of the idea behind the logic is correct.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The initial processing below,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt; do _n_=1 by 1 until(last.account_id);
  set tran;
  by account_id;&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;keeps a count of the the number of records for the by group processed for an account_id and the records are read/saved in a memory area called page buffer. Therefore&amp;nbsp; the need for another &lt;EM&gt;&lt;STRONG&gt;by account_id&amp;nbsp;&lt;/STRONG&gt;&lt;/EM&gt;isn't required when we &lt;STRONG&gt;re-read&lt;/STRONG&gt; the observations from the same group. This is where we move to recollecting the concepts of general &lt;STRONG&gt;"computer science programming"&lt;/STRONG&gt; so to speak where how an &lt;STRONG&gt;I/O&lt;/STRONG&gt;(Input/output) occurs and measured. During the 1st read, of course the dataset have to be opened, By group(first and last obs) markers have to be set by compiler and then observations are read into the page buffer, and from there into the program data vector and eventually to output buffer and dataset.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Once the 1st processing is complete, the count value of &lt;STRONG&gt;_n_&amp;nbsp;&lt;/STRONG&gt;has already determined how many records have been read into &lt;EM&gt;page buffer(cache)&lt;/EM&gt; and so all we need to do is re-read the same number of records that exist in the page buffer that is holding it. At the end of the datastep iteration, the contents of the page buffer memory is released and waits for the next set of data to be read.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Therefore, in a nutshell, we read the contents of the page buffer twice or in other words to say the equivalent we process a By group twice (smiles)&amp;nbsp; with increased level of efficiency&lt;STRONG&gt; in avoiding the compiler&lt;/STRONG&gt; to do its work twice.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Example:&lt;/P&gt;
&lt;PRE class="language-sas"&gt;&lt;CODE&gt;do _n_=1 to _n_;
  set tran;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Logic:&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Part A&lt;/STRONG&gt;- When processing by group for the 1st time&lt;/P&gt;
&lt;P&gt;1. Impute a temp variable as (-ve) for debit and (+ve) for positive. This makes it easy to have the values in the form of binary values(+ vs -).&amp;nbsp;&lt;/P&gt;
&lt;P&gt;2.Check for pairwise look up when processing -ve subsequent to adding +ve first in the hash table.&lt;/P&gt;
&lt;P&gt;3. When 2 is true, eliminate each pair one by one.&lt;/P&gt;
&lt;P&gt;4. The remaining contents are the singles that will not have its partner pair.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Part B&lt;/STRONG&gt;-&amp;nbsp;When processing by group for the 2nd time&lt;/P&gt;
&lt;P&gt;1. Check for the remaining singles in the hash table and write that to the resulting table.&lt;/P&gt;
&lt;P&gt;2. If 1 is true, make sure the matching single is cleared from the Hash table&lt;/P&gt;
&lt;P&gt;3. Repeat the same for the entire by group processing&lt;/P&gt;
&lt;P&gt;and we are done &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;span class="lia-unicode-emoji" title=":smiling_face_with_smiling_eyes:"&gt;😊&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I hope the above helps? Please let me know. Have fun!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 12 May 2020 16:49:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/delete-rows-with-two-rows-condition-after-group-by/m-p/647142#M78599</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2020-05-12T16:49:33Z</dc:date>
    </item>
    <item>
      <title>Re: delete rows with two rows condition after group by</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/delete-rows-with-two-rows-condition-after-group-by/m-p/647166#M78600</link>
      <description>&lt;P&gt;A low-tech solution would be:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;roc sort data=tran; 
by account_id amount type trans_seq; 
run;

data temp;
set tran; 
by account_id amount type;
if first.type then seq = 0;
seq + 1;
run;

data want;
merge temp(where=(type="credit") in=c) 
      temp(where=(type="debit") in=d);
by account_id amount seq;
if not (c and d);
drop seq;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 12 May 2020 17:09:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/delete-rows-with-two-rows-condition-after-group-by/m-p/647166#M78600</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2020-05-12T17:09:56Z</dc:date>
    </item>
    <item>
      <title>Re: delete rows with two rows condition after group by</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/delete-rows-with-two-rows-condition-after-group-by/m-p/647190#M78601</link>
      <description>&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/138205"&gt;@novinosrin&lt;/a&gt; I have fully understood your code now! looks like I can't run the part1 only, it will give me a false result which doesn't interpret step 1 correctly.&lt;BR /&gt;&lt;BR /&gt;What you did is a great help to me and my project! Thank you so much! Wish you have a great day!</description>
      <pubDate>Tue, 12 May 2020 18:23:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/delete-rows-with-two-rows-condition-after-group-by/m-p/647190#M78601</guid>
      <dc:creator>yliu1234</dc:creator>
      <dc:date>2020-05-12T18:23:43Z</dc:date>
    </item>
    <item>
      <title>Re: delete rows with two rows condition after group by</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/delete-rows-with-two-rows-condition-after-group-by/m-p/647198#M78602</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/328276"&gt;@yliu1234&lt;/a&gt;&amp;nbsp; Yes Part A is preliminary step that we play with in-memory processing within the contents of Hash table. So the communication here nor the processing has anything to do with writing results to an output dataset. It's merely a step that creates &lt;STRONG&gt;&lt;I&gt;interdependence&lt;/I&gt;&lt;/STRONG&gt;&amp;nbsp;for Part B, which writes to the result dataset.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 12 May 2020 18:36:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/delete-rows-with-two-rows-condition-after-group-by/m-p/647198#M78602</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2020-05-12T18:36:28Z</dc:date>
    </item>
    <item>
      <title>Re: delete rows with two rows condition after group by</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/delete-rows-with-two-rows-condition-after-group-by/m-p/648169#M78665</link>
      <description>Hi &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/462"&gt;@PGStats&lt;/a&gt;, thanks for providing your simple solution, I like your code very much and it works very good!&lt;BR /&gt;&lt;BR /&gt;I am new to merge function. I assume, when you merge two tables, it use 'by account_id amount seq' these columns as merge key, all other common columns will use the second table's value if the key is available and both tables are contributors, so both c and d are true.&lt;BR /&gt;Did I understand merge function right?</description>
      <pubDate>Fri, 15 May 2020 21:18:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/delete-rows-with-two-rows-condition-after-group-by/m-p/648169#M78665</guid>
      <dc:creator>yliu1234</dc:creator>
      <dc:date>2020-05-15T21:18:11Z</dc:date>
    </item>
    <item>
      <title>Re: delete rows with two rows condition after group by</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/delete-rows-with-two-rows-condition-after-group-by/m-p/648234#M78668</link>
      <description>&lt;P&gt;That's right! When there are matching keys, an observation is read from both datasets, with the second one overwriting the first one. But you won't see any of that happening in the output from the data step above, since the matching cases (one debit and one credit, with the same amount) are rejected by the condition &lt;EM&gt;not (c and d)&lt;/EM&gt;.&lt;/P&gt;</description>
      <pubDate>Sat, 16 May 2020 03:40:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/delete-rows-with-two-rows-condition-after-group-by/m-p/648234#M78668</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2020-05-16T03:40:48Z</dc:date>
    </item>
  </channel>
</rss>

