<?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: Counter Not Working in Hash Table in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Counter-Not-Working-in-Hash-Table/m-p/233579#M42665</link>
    <description>&lt;P&gt;Actually, I'm getting double counting in the relevant hash rows. &amp;nbsp;I'm attaching the code, log, hash table input, hash table output and input data.&lt;/P&gt;</description>
    <pubDate>Fri, 06 Nov 2015 21:50:36 GMT</pubDate>
    <dc:creator>mikersas</dc:creator>
    <dc:date>2015-11-06T21:50:36Z</dc:date>
    <item>
      <title>Counter Not Working in Hash Table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Counter-Not-Working-in-Hash-Table/m-p/233551#M42655</link>
      <description>&lt;P&gt;&lt;STRONG&gt;New to Hash Tables. &amp;nbsp;Trying to tally mutilple fields in a table that are in error. I'm not getting there.. This is the code&lt;/STRONG&gt;:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Data _null_;&lt;BR /&gt;format DATA_name $32.;&lt;BR /&gt;format counter_save 8.;&lt;BR /&gt;If _N_ = 1 then do; &lt;BR /&gt; &lt;BR /&gt; declare Hash FieldCounters(HashExp:8, multidata&amp;amp;colon; 'YES'); &lt;BR /&gt; rc = FieldCounters.DefineKey ('DATA_name'); &lt;BR /&gt; rc = FieldCounters.DefineData ('DATA_name','counter'); &lt;BR /&gt; rc = FieldCounters.DefineDone (); &lt;BR /&gt; declare hiter hifc("FieldCounters");&lt;BR /&gt; call missing(DATA_name);&lt;BR /&gt; &lt;BR /&gt; do until (eof1);&lt;BR /&gt; set work.DEFAULTVALUESHASH end= eof1;&lt;BR /&gt; rc = FieldCounters.add();&lt;BR /&gt; end;&lt;/P&gt;
&lt;P&gt;end;&lt;BR /&gt;set targt.OD001(keep=ENTITY_CRE_FLG ERROR_HOLD) end=end1;&lt;/P&gt;
&lt;P&gt;If ENTITY_CRE_FLG ne 'Y' then do;&lt;BR /&gt; DATA_name = 'ENTITY_CRE_FLG';&lt;BR /&gt; rc = FieldCounters.find(key:'DATA_name');&lt;BR /&gt; counter_save = counter + 1;&lt;BR /&gt; counter = counter_save;&lt;BR /&gt; rc = FieldCounters.replace();&lt;BR /&gt;end;&lt;BR /&gt;If missing(ERROR_HOLD) = 0 then do;&lt;BR /&gt; DATA_name = 'ERROR_HOLD';&lt;BR /&gt; rc = FieldCounters.find(key:'DATA_name');&lt;BR /&gt; counter_save = counter + 1;&lt;BR /&gt; counter = counter_save;&lt;BR /&gt; rc = FieldCounters.replace();&lt;BR /&gt;end;&lt;/P&gt;
&lt;P&gt;if end1 then do;&lt;BR /&gt; put '######### Got here';&lt;BR /&gt; rc=FieldCounters.output(dataset: 'work.hashout');&lt;BR /&gt;end;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 06 Nov 2015 20:49:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Counter-Not-Working-in-Hash-Table/m-p/233551#M42655</guid>
      <dc:creator>mikersas</dc:creator>
      <dc:date>2015-11-06T20:49:41Z</dc:date>
    </item>
    <item>
      <title>Re: Counter Not Working in Hash Table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Counter-Not-Working-in-Hash-Table/m-p/233553#M42656</link>
      <description>Suggest you include a small amount of sample data and the desired output, and also describe what happens when you run your code (getting errors in the log or just getting the wrong result, how is it wrong, etc.)</description>
      <pubDate>Fri, 06 Nov 2015 20:52:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Counter-Not-Working-in-Hash-Table/m-p/233553#M42656</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2015-11-06T20:52:57Z</dc:date>
    </item>
    <item>
      <title>Re: Counter Not Working in Hash Table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Counter-Not-Working-in-Hash-Table/m-p/233575#M42662</link>
      <description>&lt;P&gt;Without example data,the desired output and log information, it is hard to give good suggestions. Just from codes, there are keys in your definekey, but only one key was used, and key was quoted by single quote, correct use is FieldCounters.find(key: DATA_name,key: counter), or just FieldCounters.find() if key variables were the same; In addition, with rc = FieldCounters.find(), if then condition should be used.&lt;/P&gt;</description>
      <pubDate>Fri, 06 Nov 2015 21:41:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Counter-Not-Working-in-Hash-Table/m-p/233575#M42662</guid>
      <dc:creator>slchen</dc:creator>
      <dc:date>2015-11-06T21:41:14Z</dc:date>
    </item>
    <item>
      <title>Re: Counter Not Working in Hash Table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Counter-Not-Working-in-Hash-Table/m-p/233579#M42665</link>
      <description>&lt;P&gt;Actually, I'm getting double counting in the relevant hash rows. &amp;nbsp;I'm attaching the code, log, hash table input, hash table output and input data.&lt;/P&gt;</description>
      <pubDate>Fri, 06 Nov 2015 21:50:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Counter-Not-Working-in-Hash-Table/m-p/233579#M42665</guid>
      <dc:creator>mikersas</dc:creator>
      <dc:date>2015-11-06T21:50:36Z</dc:date>
    </item>
    <item>
      <title>Re: Counter Not Working in Hash Table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Counter-Not-Working-in-Hash-Table/m-p/233590#M42667</link>
      <description>&lt;P&gt;&lt;EM&gt;"I'm attaching the code, log, hash table input, hash table output and input data"&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I can't find all this information in your attachment. Anyway: If you are just after a simple count then below should work.&lt;/P&gt;
&lt;P&gt;Run the code and let us know if this returns the result you're after.&lt;/P&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;libname targt (work);
data targt.OD001;
  input ENTITY_CRE_FLG :$1. ERROR_HOLD @@;
  output;
  datalines;
Y 1 N 1 N . N 5 Y 5 Y . 
  ;
run;

Data _null_;
  length DATA_name $32.;

  If _N_ = 1 then
    do;
      declare Hash FieldCounters(HashExp:4, multidata&amp;amp;colon; 'NO');
      rc = FieldCounters.DefineKey ('DATA_name');
      rc = FieldCounters.DefineData ('DATA_name','counter');
      rc = FieldCounters.DefineDone ();
      call missing(DATA_name);
    end;

  set targt.OD001(keep=ENTITY_CRE_FLG ERROR_HOLD) end=end1;

  If ENTITY_CRE_FLG ne 'Y' then
    do;
      DATA_name = 'ENTITY_CRE_FLG';
      rc = FieldCounters.find();
      counter=sum(counter,1);
      rc = FieldCounters.replace();
    end;

  If missing(ERROR_HOLD) = 0 then
    do;
      DATA_name = 'ERROR_HOLD';
      rc = FieldCounters.find();
      counter=sum(counter,1);
      rc = FieldCounters.replace();
    end;

  if end1 then
    do;
      put '######### Got here';
      rc=FieldCounters.output(dataset:'work.hashout');
    end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In case you want to pre-populate your hash with a list of expected values with a counter initiated to 0 then below code should do.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;libname targt (work);
data targt.OD001;
  input ENTITY_CRE_FLG :$1. ERROR_HOLD @@;
  output;
  datalines;
Y 1 N 1 N . N 5 Y 5 Y . 
  ;
run;

data work.DEFAULTVALUESHASH;
  length DATA_name $32.;
  counter=0;
  DATA_name='ENTITY_CRE_FLG';
  output;
  DATA_name='ERROR_HOLD';
  output;
  DATA_name='SOMETHING ELSE';
  output;
run;

Data _null_;
  If _N_ = 1 then
    do;
      if 0 then set work.DEFAULTVALUESHASH;
      declare Hash FieldCounters(dataset:'work.DEFAULTVALUESHASH');
      rc = FieldCounters.DefineKey ('DATA_name');
      rc = FieldCounters.DefineData (all:'y');
      rc = FieldCounters.DefineDone ();
      call missing(of _all_);
    end;

  set targt.OD001(keep=ENTITY_CRE_FLG ERROR_HOLD) end=end1;

  If ENTITY_CRE_FLG ne 'Y' then
    do;
      DATA_name = 'ENTITY_CRE_FLG';
      rc = FieldCounters.find();
      counter=sum(counter,1);
      rc = FieldCounters.replace();
    end;

  If missing(ERROR_HOLD) = 0 then
    do;
      DATA_name = 'ERROR_HOLD';
      rc = FieldCounters.find();
      counter=sum(counter,1);
      rc = FieldCounters.replace();
    end;

  if end1 then
    do;
      put '######### Got here';
      rc=FieldCounters.output(dataset:'work.hashout');
    end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 07 Nov 2015 01:27:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Counter-Not-Working-in-Hash-Table/m-p/233590#M42667</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2015-11-07T01:27:39Z</dc:date>
    </item>
  </channel>
</rss>

