<?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: Hash programing - removdup question in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Hash-programing-removdup-question/m-p/470860#M284910</link>
    <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/201239"&gt;@Fae&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am having trouble with the removedup command in Hash.&amp;nbsp; From what I understand, it remove the current record being pointed in the hash table(memory) and set the pointer to null.&amp;nbsp; Am I missing anything?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BR /&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Yes, you are. There are, unfortunately, even more details about the REMOVEDUP method than can be found in the &lt;A href="http://documentation.sas.com/?docsetId=lecompobjref&amp;amp;docsetTarget=n0ok7sej0uwin8n13ti53el0inta.htm&amp;amp;docsetVersion=9.4&amp;amp;locale=en" target="_blank"&gt;documentation&lt;/A&gt;. It took me quite some testing to investigate these details (using SAS 9.4 TS1M2).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For example, you may be wondering what the purpose of the "&lt;FONT face="courier new,courier"&gt;key: ...&lt;/FONT&gt;" argument of the method is, when&amp;nbsp;actually the pointer specifies the hash entry to be removed, as you mentioned.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Indeed, the pointer must be set, regardless of the argument (as long as it is valid) and regardless of values of key variables. (Otherwise you get the "&lt;FONT face="courier new,courier"&gt;WARNING: No current list item has been set ...&lt;/FONT&gt;".) The effect of the REMOVEDUP method then depends on&amp;nbsp;the type of entry the pointer points to:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;U&gt;Case 1: It belongs to a group of &amp;gt;=2 entries with the same key.&lt;/U&gt;&lt;/P&gt;
&lt;P&gt;Effect: This entry is removed, unless an error occurs because the group (!) is locked by an iterator or the key argument is invalid (type mismatch or incorrect number of values.) Valid key arguments or values of key variables are ignored, including the case of non-existing key values. The return code is 0.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;U&gt;Case 2:&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;Its key occurs only once in the hash object (still assuming &lt;FONT face="courier new,courier"&gt;MULTIDATA: 'YES'&lt;/FONT&gt;).&lt;/U&gt;&lt;/P&gt;
&lt;P&gt;Effect: In this case REMOVEDUP acts in the same way as REMOVE! Which means: Now it is the pointer which is ignored henceforth. If an existing key is specified in the argument or (with empty argument) in the key variables, all (!) hash items &lt;EM&gt;with this key&lt;/EM&gt; are removed (and the return code is set to 0), again unless locked by an iterator. If a non-existing (but valid) key is specified, nothing is removed and only in this case the return code is different from 0 (causing an error message if not written to a variable). An invalid key causes error messages.&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>Sun, 17 Jun 2018 12:08:11 GMT</pubDate>
    <dc:creator>FreelanceReinh</dc:creator>
    <dc:date>2018-06-17T12:08:11Z</dc:date>
    <item>
      <title>Hash programing - removdup question</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Hash-programing-removdup-question/m-p/462822#M284908</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am having trouble with the removedup command in Hash.&amp;nbsp; From what I understand, it remove the current record being pointed in the hash table(memory) and set the pointer to null.&amp;nbsp; Am I missing anything?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Basically I have two data set, sales and refunds.&amp;nbsp; I need to merge the refund data back to the original purchase by matching&amp;nbsp;customer_id, product_id in a FIFO basis as long as refund is &amp;lt;= sales which then will proceed to the next purchase.&amp;nbsp; The record will also have all the refund_id added up.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;But I couldn't get it working properly, if i use removedup, each purchase will just take 1 refund and move to the next.&amp;nbsp; Could someone help me?&amp;nbsp; Thanks.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;*****&amp;nbsp; Code&amp;nbsp; ****&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;data Sales;&lt;BR /&gt;infile datalines dsd;&lt;BR /&gt;input Customer_id Product_Id $ sales transaction_id $;&lt;BR /&gt;cards;&lt;BR /&gt;1,A1,2,1&lt;BR /&gt;1,A1,4,2&lt;BR /&gt;1,A2,5,3&lt;BR /&gt;2,A1,2,4&lt;BR /&gt;2,A2,2,5&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;data Refund;&lt;BR /&gt;infile datalines dsd;&lt;BR /&gt;input Customer_id Product_Id $ refunds refund_id $;&lt;BR /&gt;cards;&lt;BR /&gt;1,A1,1,r1&lt;BR /&gt;1,A1,1,r2&lt;BR /&gt;1,A1,1,r3&lt;BR /&gt;2,A1,1,r4&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data want;&lt;BR /&gt;if _n_=1 then do;&lt;BR /&gt;if 0 then set refund;&lt;BR /&gt;declare hash h(dataset:'refund',multidata:'y');&lt;BR /&gt;h.definekey('Customer_id','Product_Id');&lt;BR /&gt;h.definedata('refunds','refund_id');&lt;BR /&gt;h.definedone();&lt;BR /&gt;end;&lt;BR /&gt;set sales;&lt;BR /&gt;by Customer_id Product_Id;&lt;BR /&gt;format r_id $200.;&lt;BR /&gt;cum_refund = 0;&lt;BR /&gt;r_id = "";&lt;BR /&gt;call missing(refunds,refund_id);&lt;BR /&gt;rc=h.find();&lt;BR /&gt;do while(rc=0);&lt;BR /&gt;if sum(refunds, cum_refund) &amp;lt;= sales then&lt;BR /&gt;do;&lt;BR /&gt;cum_refund = sum(refunds,cum_refund);&lt;BR /&gt;r_id = catx("-",r_id,refund_id);&lt;BR /&gt;*h.removedup();&lt;BR /&gt;call missing(refunds,refund_id);&lt;BR /&gt;rc=h.find_next();&lt;BR /&gt;end;&lt;BR /&gt;else&lt;BR /&gt;do;&lt;BR /&gt;leave;&lt;/P&gt;&lt;P&gt;end;&lt;BR /&gt;end;&lt;BR /&gt;drop refunds refund_id rc;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;******&amp;nbsp; &amp;nbsp;Data&amp;nbsp; &amp;nbsp;********&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Have:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Sales:&lt;/P&gt;&lt;TABLE border="0" cellspacing="0" cellpadding="0"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;Customer_ID&lt;/TD&gt;&lt;TD&gt;Product_id&lt;/TD&gt;&lt;TD&gt;sales&lt;/TD&gt;&lt;TD&gt;Transaction_ID&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;A1&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;A1&lt;/TD&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;A2&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;5&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&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;</description>
      <pubDate>Wed, 16 May 2018 19:40:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Hash-programing-removdup-question/m-p/462822#M284908</guid>
      <dc:creator>Fae</dc:creator>
      <dc:date>2018-05-16T19:40:30Z</dc:date>
    </item>
    <item>
      <title>Re: Hash programing - removdup question</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Hash-programing-removdup-question/m-p/462906#M284909</link>
      <description>&lt;P&gt;I'm afraid this is not quite a removedup question but doesn't matter.&amp;nbsp; I honestly think you can try much more simpler straight forward solutions than hash but that's up to you. Please find the corrected below:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data Sales;
infile datalines dsd;
input Customer_id Product_Id $ sales transaction_id $;
cards;
1,A1,2,1
1,A1,4,2
1,A2,5,3
2,A1,2,4
2,A2,2,5
;
run;

data Refund;
infile datalines dsd;
input Customer_id Product_Id $ refunds refund_id $;
cards;
1,A1,1,r1
1,A1,1,r2
1,A1,1,r3
2,A1,1,r4
;
run;

 

data want;
if _n_=1 then do;
if 0 then set refund;
declare hash h(dataset:'refund',multidata:'y');
h.definekey('Customer_id','Product_Id');
h.definedata('refunds','refund_id');
h.definedone();
end;
set sales;
by Customer_id Product_Id;
format r_id $200.;
cum_refund = 0;
r_id = "";
call missing(refunds,refund_id);
rc=h.find();
do while(rc=0);
if sum(refunds, cum_refund) &amp;lt;= sales then
do;
if cum_refund=2 then do; cum_refund=0;r_id = "";end;/*here is the correction*/
cum_refund = sum(refunds,cum_refund);
r_id = catx("-",r_id,refund_id);
*h.removedup();/*not needed, so I let it stay commented*/
call missing(refunds,refund_id);
rc=h.find_next();
end;
else
do;
leave;
end;
end;
drop refunds refund_id rc;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Here is the corrected code:&lt;/P&gt;</description>
      <pubDate>Thu, 17 May 2018 00:21:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Hash-programing-removdup-question/m-p/462906#M284909</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2018-05-17T00:21:27Z</dc:date>
    </item>
    <item>
      <title>Re: Hash programing - removdup question</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Hash-programing-removdup-question/m-p/470860#M284910</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/201239"&gt;@Fae&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am having trouble with the removedup command in Hash.&amp;nbsp; From what I understand, it remove the current record being pointed in the hash table(memory) and set the pointer to null.&amp;nbsp; Am I missing anything?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BR /&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Yes, you are. There are, unfortunately, even more details about the REMOVEDUP method than can be found in the &lt;A href="http://documentation.sas.com/?docsetId=lecompobjref&amp;amp;docsetTarget=n0ok7sej0uwin8n13ti53el0inta.htm&amp;amp;docsetVersion=9.4&amp;amp;locale=en" target="_blank"&gt;documentation&lt;/A&gt;. It took me quite some testing to investigate these details (using SAS 9.4 TS1M2).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For example, you may be wondering what the purpose of the "&lt;FONT face="courier new,courier"&gt;key: ...&lt;/FONT&gt;" argument of the method is, when&amp;nbsp;actually the pointer specifies the hash entry to be removed, as you mentioned.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Indeed, the pointer must be set, regardless of the argument (as long as it is valid) and regardless of values of key variables. (Otherwise you get the "&lt;FONT face="courier new,courier"&gt;WARNING: No current list item has been set ...&lt;/FONT&gt;".) The effect of the REMOVEDUP method then depends on&amp;nbsp;the type of entry the pointer points to:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;U&gt;Case 1: It belongs to a group of &amp;gt;=2 entries with the same key.&lt;/U&gt;&lt;/P&gt;
&lt;P&gt;Effect: This entry is removed, unless an error occurs because the group (!) is locked by an iterator or the key argument is invalid (type mismatch or incorrect number of values.) Valid key arguments or values of key variables are ignored, including the case of non-existing key values. The return code is 0.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;U&gt;Case 2:&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;Its key occurs only once in the hash object (still assuming &lt;FONT face="courier new,courier"&gt;MULTIDATA: 'YES'&lt;/FONT&gt;).&lt;/U&gt;&lt;/P&gt;
&lt;P&gt;Effect: In this case REMOVEDUP acts in the same way as REMOVE! Which means: Now it is the pointer which is ignored henceforth. If an existing key is specified in the argument or (with empty argument) in the key variables, all (!) hash items &lt;EM&gt;with this key&lt;/EM&gt; are removed (and the return code is set to 0), again unless locked by an iterator. If a non-existing (but valid) key is specified, nothing is removed and only in this case the return code is different from 0 (causing an error message if not written to a variable). An invalid key causes error messages.&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>Sun, 17 Jun 2018 12:08:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Hash-programing-removdup-question/m-p/470860#M284910</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2018-06-17T12:08:11Z</dc:date>
    </item>
  </channel>
</rss>

