<?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 Duplicate Error - NO DUPS! in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/HASH-Duplicate-Error-NO-DUPS/m-p/396195#M95627</link>
    <description>&lt;P&gt;SAS 9.4, running on a Windows Server.&lt;/P&gt;&lt;P&gt;I have also tried closing and starting in a new session, but that made no difference at all.&lt;/P&gt;</description>
    <pubDate>Fri, 15 Sep 2017 08:01:49 GMT</pubDate>
    <dc:creator>JW2702</dc:creator>
    <dc:date>2017-09-15T08:01:49Z</dc:date>
    <item>
      <title>HASH Duplicate Error - NO DUPS!</title>
      <link>https://communities.sas.com/t5/SAS-Programming/HASH-Duplicate-Error-NO-DUPS/m-p/396191#M95624</link>
      <description>&lt;P&gt;I keep getting this error when I try to use a hash table:&lt;/P&gt;&lt;P&gt;ERROR: Duplicate key.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have other tables that run fine, so presumably the code is fine, but this particular table will not work. I have done a proc sort nodupkey on exactly the same keys I am using and there are no duplicates in the table at all. So I don't understand why it says this particular table has a duplicate key.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This is the code:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data workjw.base2;&lt;BR /&gt;set workjw.base;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;if _n_ = 0 then do;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;set workjw.acct_tpe_bin;&lt;/P&gt;&lt;P&gt;end;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;if _n_ = 1 then do;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;declare hash b(dataset: "workjw.acct_tpe_bin", duplicate: "r");&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;rc2=b.definekey("CH_ACCT_NO_BIN", "ACCT_TPE_CDE");&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;rc2=b.definedata("ACC_DESC_BIN");&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;rc2=b.definedone();&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;b.add();&lt;/P&gt;&lt;P&gt;end;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;rc2=b.find();&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;if rc2 ne 0 then do;&lt;BR /&gt;ACC_DESC_BIN="";&lt;BR /&gt;end;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 15 Sep 2017 07:54:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/HASH-Duplicate-Error-NO-DUPS/m-p/396191#M95624</guid>
      <dc:creator>JW2702</dc:creator>
      <dc:date>2017-09-15T07:54:29Z</dc:date>
    </item>
    <item>
      <title>Re: HASH Duplicate Error - NO DUPS!</title>
      <link>https://communities.sas.com/t5/SAS-Programming/HASH-Duplicate-Error-NO-DUPS/m-p/396195#M95627</link>
      <description>&lt;P&gt;SAS 9.4, running on a Windows Server.&lt;/P&gt;&lt;P&gt;I have also tried closing and starting in a new session, but that made no difference at all.&lt;/P&gt;</description>
      <pubDate>Fri, 15 Sep 2017 08:01:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/HASH-Duplicate-Error-NO-DUPS/m-p/396195#M95627</guid>
      <dc:creator>JW2702</dc:creator>
      <dc:date>2017-09-15T08:01:49Z</dc:date>
    </item>
    <item>
      <title>Re: HASH Duplicate Error - NO DUPS!</title>
      <link>https://communities.sas.com/t5/SAS-Programming/HASH-Duplicate-Error-NO-DUPS/m-p/396219#M95633</link>
      <description>&lt;P&gt;Using the following statement,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;&amp;nbsp;declare hash b(dataset: "workjw.acct_tpe_bin", duplicate: "r");&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;you are asking to add the last value, replacing the previous values. This means that if there are duplicate records, they will be happily replaced and no ERROR message will come.&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Your Duplicate Error message is strange!&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Place your LOG to see any clues.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 15 Sep 2017 09:20:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/HASH-Duplicate-Error-NO-DUPS/m-p/396219#M95633</guid>
      <dc:creator>KachiM</dc:creator>
      <dc:date>2017-09-15T09:20:41Z</dc:date>
    </item>
    <item>
      <title>Re: HASH Duplicate Error - NO DUPS!</title>
      <link>https://communities.sas.com/t5/SAS-Programming/HASH-Duplicate-Error-NO-DUPS/m-p/396227#M95636</link>
      <description>&lt;P&gt;I believe the 'duplicate: "r"' statement only refers to the initial load of the data set. The error is coming on the add() method. If you supply a return value for the error code on that e.g. rc=b.add() you won't get the error. Although you appear to be loading the same information twice anyway so I'm not sure why you need to call b.add() at all.&lt;/P&gt;</description>
      <pubDate>Fri, 15 Sep 2017 09:37:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/HASH-Duplicate-Error-NO-DUPS/m-p/396227#M95636</guid>
      <dc:creator>ChrisBrooks</dc:creator>
      <dc:date>2017-09-15T09:37:20Z</dc:date>
    </item>
    <item>
      <title>Re: HASH Duplicate Error - NO DUPS!</title>
      <link>https://communities.sas.com/t5/SAS-Programming/HASH-Duplicate-Error-NO-DUPS/m-p/396228#M95637</link>
      <description>&lt;P&gt;It just has this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;NOTE: There were 147&amp;nbsp;observations read from the data set &lt;SPAN&gt;workjw.acct_tpe_bin&lt;/SPAN&gt;&lt;BR /&gt;ERROR: Duplicate key.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If I try sorting with nodupkey, it gives no errors:&lt;/P&gt;&lt;P&gt;2590 proc sort data=workjw.acct_tpe_bin force nodupkey; by ACCT_TPE_CDE CH_ACCT_NO_BIN ; run;&lt;/P&gt;&lt;P&gt;NOTE: There were 147 observations read from the data set WORKJW.ACCT_TPE_BIN.&lt;BR /&gt;NOTE: 0 observations with duplicate key values were deleted.&lt;BR /&gt;NOTE: The data set WORKJW.ACCT_TPE_BIN has 147 observations and 3 variables.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I checked that the field I am using as a key is the same length and format on both tables. They are.&lt;/P&gt;</description>
      <pubDate>Fri, 15 Sep 2017 09:37:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/HASH-Duplicate-Error-NO-DUPS/m-p/396228#M95637</guid>
      <dc:creator>JW2702</dc:creator>
      <dc:date>2017-09-15T09:37:42Z</dc:date>
    </item>
    <item>
      <title>Re: HASH Duplicate Error - NO DUPS!</title>
      <link>https://communities.sas.com/t5/SAS-Programming/HASH-Duplicate-Error-NO-DUPS/m-p/396229#M95638</link>
      <description>&lt;P&gt;As I said above - the whole data set is loaded into the hash table when it is declared - you don't need to exclude the duplicate key records. You are then trying to add the first record into the hash again with b.add() which is where the error comes&lt;/P&gt;</description>
      <pubDate>Fri, 15 Sep 2017 09:43:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/HASH-Duplicate-Error-NO-DUPS/m-p/396229#M95638</guid>
      <dc:creator>ChrisBrooks</dc:creator>
      <dc:date>2017-09-15T09:43:15Z</dc:date>
    </item>
    <item>
      <title>Re: HASH Duplicate Error - NO DUPS!</title>
      <link>https://communities.sas.com/t5/SAS-Programming/HASH-Duplicate-Error-NO-DUPS/m-p/396237#M95640</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/165214"&gt;@JW2702&lt;/a&gt;&lt;/P&gt;
&lt;P&gt;I'm repeating here what&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/32246"&gt;@ChrisBrooks&lt;/a&gt;&amp;nbsp;already wrote but may be posting a bit of code will make the issue clearer to you.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Below statement...&lt;/P&gt;
&lt;PRE&gt;declare hash b(dataset: "workjw.acct_tpe_bin");&lt;/PRE&gt;
&lt;P&gt;...loads the whole source table into the hash. The default for a source table having duplicate keys is: Only the first occurence of a key combination will get loaded into the hash. Any later duplicate key records will be ignored. The process will run without error or warning.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The problematic part in your code is: &lt;STRONG&gt;b.add();&lt;/STRONG&gt;&lt;/P&gt;
&lt;PRE&gt;  if _n_ = 1 then
    do;
      declare hash b(dataset: "workjw.acct_tpe_bin");
      .....
      b.add();
    end;&lt;/PRE&gt;
&lt;P&gt;You've already loaded the whole table deduped into the hash. Now with this&lt;EM&gt; b.add()&lt;/EM&gt; you're instructing SAS to load observation 1 (if _n_=1) once more into the hash. ...but you've loaded the table already via the &lt;EM&gt;dataset:&amp;lt;dataset&amp;gt; directive&lt;/EM&gt; in the&amp;nbsp;&lt;EM&gt;declare&lt;/EM&gt; statement so&amp;nbsp;&lt;EM&gt;b.add()&lt;/EM&gt; is now trying to load a duplicate. That's what causes the error.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The default setting for a hash table is to not accept duplicate keys:&lt;/P&gt;
&lt;P&gt;- Remove command &lt;EM&gt;b.add()&lt;/EM&gt; and everything will work as expected.&lt;/P&gt;
&lt;P&gt;- No need to pre-sort and dedupe the source table for the hash prior to loading. Statement &lt;EM&gt;Declare Hash&lt;/EM&gt; will take care of this.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And here a bit a shorter and working code version:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data workjw.base2;
  set workjw.base;

  if _n_ = 1 then
    do;
      if 0 then set workjw.acct_tpe_bin(keep=CH_ACCT_NO_BIN ACCT_TPE_CDE ACC_DESC_BIN);
      declare hash b(dataset: "workjw.acct_tpe_bin");
      b.definekey("CH_ACCT_NO_BIN", "ACCT_TPE_CDE");
      b.definedata("ACC_DESC_BIN");
      b.definedone();
    end;

  if b.find() then call missing(ACC_DESC_BIN);

run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 15 Sep 2017 10:33:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/HASH-Duplicate-Error-NO-DUPS/m-p/396237#M95640</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2017-09-15T10:33:42Z</dc:date>
    </item>
    <item>
      <title>Re: HASH Duplicate Error - NO DUPS!</title>
      <link>https://communities.sas.com/t5/SAS-Programming/HASH-Duplicate-Error-NO-DUPS/m-p/396240#M95641</link>
      <description>&lt;P&gt;In addition to the observations made by others, why not you try the following stripped code and see whether you still get the ERROR.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data workjw.base2;
 
if _n_ = 0 then do;
     set workjw.acct_tpe_bin;
end;
 
if _n_ = 1 then do;
     declare hash b(dataset: "workjw.acct_tpe_bin");
     rc2=b.definekey("CH_ACCT_NO_BIN", "ACCT_TPE_CDE");
     rc2=b.definedata("ACC_DESC_BIN");
     rc2=b.definedone();
end;
 
rc = b.output(dataset:'out_01');
 
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 15 Sep 2017 10:20:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/HASH-Duplicate-Error-NO-DUPS/m-p/396240#M95641</guid>
      <dc:creator>KachiM</dc:creator>
      <dc:date>2017-09-15T10:20:25Z</dc:date>
    </item>
    <item>
      <title>Re: HASH Duplicate Error - NO DUPS!</title>
      <link>https://communities.sas.com/t5/SAS-Programming/HASH-Duplicate-Error-NO-DUPS/m-p/396242#M95642</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/17813"&gt;@KachiM&lt;/a&gt;&lt;/P&gt;
&lt;P&gt;As soon as you remove &lt;EM&gt;b.add()&lt;/EM&gt; things will work.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;libname workjw (work);

data workjw.acct_tpe_bin;
  CH_ACCT_NO_BIN=1;
  ACCT_TPE_CDE=1;
  ACC_DESC_BIN=1;
  output;
  CH_ACCT_NO_BIN=1;
  ACCT_TPE_CDE=1;
  ACC_DESC_BIN=1;
  output;
run;

data workjw.base;
  CH_ACCT_NO_BIN=1;
  ACCT_TPE_CDE=1;
  output;

  CH_ACCT_NO_BIN=2;
  ACCT_TPE_CDE=2;
  output;

run;



data workjw.base2;
  set workjw.base;

  if _n_ = 0 then
    do;
      set workjw.acct_tpe_bin;
    end;

  if _n_ = 1 then
    do;
      declare hash b(dataset: "workjw.acct_tpe_bin");
      rc2=b.definekey("CH_ACCT_NO_BIN", "ACCT_TPE_CDE");
      rc2=b.definedata("ACC_DESC_BIN");
      rc2=b.definedone();
/*      b.add();*/
    end;

  rc2=b.find();

  if rc2 ne 0 then
    do;
      ACC_DESC_BIN="";
    end;
run;

&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 15 Sep 2017 10:22:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/HASH-Duplicate-Error-NO-DUPS/m-p/396242#M95642</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2017-09-15T10:22:16Z</dc:date>
    </item>
    <item>
      <title>Re: HASH Duplicate Error - NO DUPS!</title>
      <link>https://communities.sas.com/t5/SAS-Programming/HASH-Duplicate-Error-NO-DUPS/m-p/396254#M95646</link>
      <description>&lt;P&gt;Thanks. I was playing around and changed the add to say "rc2=b.add();" and that worked.&lt;/P&gt;</description>
      <pubDate>Fri, 15 Sep 2017 10:54:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/HASH-Duplicate-Error-NO-DUPS/m-p/396254#M95646</guid>
      <dc:creator>JW2702</dc:creator>
      <dc:date>2017-09-15T10:54:52Z</dc:date>
    </item>
  </channel>
</rss>

