<?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 Hash of hashes: Load identical file multiple times with different where-clause in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Hash-of-hashes-Load-identical-file-multiple-times-with-different/m-p/664019#M198327</link>
    <description>&lt;P&gt;I try to set up several hash objects and be able to iterate through them with a "hash of hashes" object. I want to use the same dataset, but with different subsettings (where-clause empty or with some modifications). So I expect to load datasets with different sizes.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For some reason the loaded "where"-clause is not in effect (see output screenshot below). I can't find the reason what I'm missing.&amp;nbsp;&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;data a;
    do id=1 to 50 by 1;
        value=19;
        output;
    end;
run;

data _null_;
    if 0 then set a;
    if _n_=1 then do;
        declare hash hoh(multidata: "yes");
        hoh.defineKey("table", "where");
        hoh.defineData("h", "table", "where");
        hoh.defineDone();
        declare hiter i_hoh("hoh");

        declare hash h();

        * For comparisons with input dataset --&amp;gt; "h" = rp19;
        table="a";
        where=" ";
        h = _new_ hash(dataset: catx(" ", table, where),
                       multidata: "no");
        h.defineKey("id");
        h.defineData(all: "yes");
        h.defineDone();
        hoh.add();

        table="a";
        where="(where=(id&amp;lt;20))";
        h = _new_ hash(dataset: catx(" ", table, where),
                       multidata: "no");
        h.defineKey("id");
        h.defineData(all: "yes");
        h.defineDone();
        hoh.add();

        table="a (where=(id&amp;gt;40))";
        where=" ";
        h = _new_ hash(dataset: catx(" ", table, where),
                       multidata: "no");
        h.defineKey("id");
        h.defineData(all: "yes");
        h.defineDone();
        hoh.add();
    end;
    * Iterate over "hash of hashes" object to reference the hash objects (rp19, rp23, rp49, rp79) like in an array;
    do while (i_hoh.next() = 0);
        rows = h.num_items;
        put (table rows) (=);
    end;
    stop;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;The log window gives me the following output:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="image.png" style="width: 457px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/46449i077BC73B68FA60C6/image-size/large?v=v2&amp;amp;px=999" role="button" title="image.png" alt="image.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 22 Jun 2020 16:28:29 GMT</pubDate>
    <dc:creator>left</dc:creator>
    <dc:date>2020-06-22T16:28:29Z</dc:date>
    <item>
      <title>Hash of hashes: Load identical file multiple times with different where-clause</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Hash-of-hashes-Load-identical-file-multiple-times-with-different/m-p/664019#M198327</link>
      <description>&lt;P&gt;I try to set up several hash objects and be able to iterate through them with a "hash of hashes" object. I want to use the same dataset, but with different subsettings (where-clause empty or with some modifications). So I expect to load datasets with different sizes.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For some reason the loaded "where"-clause is not in effect (see output screenshot below). I can't find the reason what I'm missing.&amp;nbsp;&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;data a;
    do id=1 to 50 by 1;
        value=19;
        output;
    end;
run;

data _null_;
    if 0 then set a;
    if _n_=1 then do;
        declare hash hoh(multidata: "yes");
        hoh.defineKey("table", "where");
        hoh.defineData("h", "table", "where");
        hoh.defineDone();
        declare hiter i_hoh("hoh");

        declare hash h();

        * For comparisons with input dataset --&amp;gt; "h" = rp19;
        table="a";
        where=" ";
        h = _new_ hash(dataset: catx(" ", table, where),
                       multidata: "no");
        h.defineKey("id");
        h.defineData(all: "yes");
        h.defineDone();
        hoh.add();

        table="a";
        where="(where=(id&amp;lt;20))";
        h = _new_ hash(dataset: catx(" ", table, where),
                       multidata: "no");
        h.defineKey("id");
        h.defineData(all: "yes");
        h.defineDone();
        hoh.add();

        table="a (where=(id&amp;gt;40))";
        where=" ";
        h = _new_ hash(dataset: catx(" ", table, where),
                       multidata: "no");
        h.defineKey("id");
        h.defineData(all: "yes");
        h.defineDone();
        hoh.add();
    end;
    * Iterate over "hash of hashes" object to reference the hash objects (rp19, rp23, rp49, rp79) like in an array;
    do while (i_hoh.next() = 0);
        rows = h.num_items;
        put (table rows) (=);
    end;
    stop;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;The log window gives me the following output:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="image.png" style="width: 457px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/46449i077BC73B68FA60C6/image-size/large?v=v2&amp;amp;px=999" role="button" title="image.png" alt="image.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 22 Jun 2020 16:28:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Hash-of-hashes-Load-identical-file-multiple-times-with-different/m-p/664019#M198327</guid>
      <dc:creator>left</dc:creator>
      <dc:date>2020-06-22T16:28:29Z</dc:date>
    </item>
    <item>
      <title>Re: Hash of hashes: Load identical file multiple times with different where-clause</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Hash-of-hashes-Load-identical-file-multiple-times-with-different/m-p/664020#M198328</link>
      <description>The comment lines/ part of the comment lines can be ignored (non-relevant and maybe misleading):&lt;BR /&gt;--&amp;gt; * For comparisons with input dataset --&amp;gt; "h" = rp19;&lt;BR /&gt;--&amp;gt; (rp19, rp23, rp49, rp79)</description>
      <pubDate>Mon, 22 Jun 2020 16:32:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Hash-of-hashes-Load-identical-file-multiple-times-with-different/m-p/664020#M198328</guid>
      <dc:creator>left</dc:creator>
      <dc:date>2020-06-22T16:32:35Z</dc:date>
    </item>
    <item>
      <title>Re: Hash of hashes: Load identical file multiple times with different where-clause</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Hash-of-hashes-Load-identical-file-multiple-times-with-different/m-p/664021#M198329</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/315410"&gt;@left&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You just forgot to specify sufficient lengths for the character variables &lt;FONT face="courier new,courier"&gt;table&lt;/FONT&gt; and &lt;FONT face="courier new,courier"&gt;where&lt;/FONT&gt;, so that the intended values are truncated to &lt;STRONG&gt;one&lt;/STRONG&gt; character.&lt;/P&gt;
&lt;P&gt;For example:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;length table where $20;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;(but I'm sure you actually know that).&lt;/P&gt;</description>
      <pubDate>Mon, 22 Jun 2020 16:59:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Hash-of-hashes-Load-identical-file-multiple-times-with-different/m-p/664021#M198329</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2020-06-22T16:59:00Z</dc:date>
    </item>
    <item>
      <title>Re: Hash of hashes: Load identical file multiple times with different where-clause</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Hash-of-hashes-Load-identical-file-multiple-times-with-different/m-p/664032#M198331</link>
      <description>&lt;P&gt;Also, I don't think you have any need for &lt;EM&gt;&lt;STRONG&gt;multidata:"yes"&lt;/STRONG&gt;&lt;/EM&gt; for hoh, once you have corrected the lengths of table and where.&lt;/P&gt;</description>
      <pubDate>Mon, 22 Jun 2020 17:48:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Hash-of-hashes-Load-identical-file-multiple-times-with-different/m-p/664032#M198331</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2020-06-22T17:48:07Z</dc:date>
    </item>
    <item>
      <title>Re: Hash of hashes: Load identical file multiple times with different where-clause</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Hash-of-hashes-Load-identical-file-multiple-times-with-different/m-p/664096#M198359</link>
      <description>&lt;P&gt;You create a hash instance that memory leaks (albeit very small leak)&lt;/P&gt;
&lt;PRE&gt;declare hash h();   * create uninitialized instance whilst adding to pdv;
&lt;/PRE&gt;
&lt;P&gt;In HoH the hash data element &lt;CODE&gt;h&lt;/CODE&gt;&amp;nbsp;only needs to be declared.&amp;nbsp; Of course, the _new_ instance must have occurred prior to&amp;nbsp;&lt;CODE&gt;.add()&lt;/CODE&gt; or &lt;CODE&gt;.replace()&lt;/CODE&gt;&amp;nbsp;time. So, simply declare the host reference, h, that will interact with your anonymous hashes.&lt;/P&gt;
&lt;PRE&gt;declare hash h;    * add h, a hash reference, to PDV;
&lt;/PRE&gt;
&lt;P&gt;The&amp;nbsp;&lt;CODE&gt;table&lt;/CODE&gt;&amp;nbsp;variable should be $41 to accommodate longest plain &amp;lt;libref&amp;gt;.&amp;lt;dataset&amp;gt;, that being $8 + $1 + $32.&amp;nbsp; If the table value is to further accommodate data set options you might want the length to be more like $200.&amp;nbsp; However, since the&amp;nbsp;&lt;CODE&gt;where&lt;/CODE&gt;&amp;nbsp;clause is part of the key, you should exhibit discipline and assign only plain data set names to&amp;nbsp;&lt;CODE&gt;table&lt;/CODE&gt;&amp;nbsp;and use the separate&amp;nbsp;&lt;CODE&gt;where&lt;/CODE&gt;, say $200, for loading subsets.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;As stated elsewhere, the 'master' hash&amp;nbsp;&lt;CODE&gt;hoh&amp;nbsp;&lt;/CODE&gt;does not need to be multidata in your situation.&amp;nbsp; Also, you do not need to repeatedly code&amp;nbsp;&lt;CODE&gt;table="&amp;lt;whatever&amp;gt;";&lt;/CODE&gt;&amp;nbsp;The value will not be altered by a prior&amp;nbsp;&lt;CODE&gt;hoh.add()&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Example&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;data have;
    do id=1 to 50 by 1;
        value=19;
        output;
    end;
run;

data _null_;
    if 0 then set have;

    length table $41;
    length where $200;

    if _n_=1 then do;
        declare hash hoh();
        hoh.defineKey("table", "where");
        hoh.defineData("h", "table", "where");
        hoh.defineDone();

        declare hiter i_hoh("hoh");

        declare hash h;

        table="have";
        where=" ";
        h = _new_ hash(dataset: catx(" ", table, where), multidata: "no");
        h.defineKey("id");
        h.defineData(all: "yes");
        h.defineDone();
        hoh.add();

        where="(where=(id&amp;lt;20))";
        h = _new_ hash(dataset: catx(" ", table, where), multidata: "no");
        h.defineKey("id");
        h.defineData(all: "yes");
        h.defineDone();
        hoh.add();

        where="(where=(id&amp;gt;40))"; 
        h = _new_ hash(dataset: catx(" ", table, where), multidata: "no");
        h.defineKey("id");
        h.defineData(all: "yes");
        h.defineDone();
        hoh.add();
    end;

    do while (i_hoh.next() = 0);
        rows = h.num_items;
        put (table where rows) (=);
    end;

    stop;
run;&lt;/LI-CODE&gt;
&lt;P&gt;Log&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;NOTE: There were 50 observations read from the data set WORK.HAVE.
NOTE: There were 19 observations read from the data set WORK.HAVE.
      WHERE id&amp;lt;20;
NOTE: There were 10 observations read from the data set WORK.HAVE.
      WHERE id&amp;gt;40;
table=have where=(where=(id&amp;lt;20)) rows=19
table=have where=(where=(id&amp;gt;40)) rows=10
table=have where=  rows=50
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 22 Jun 2020 23:43:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Hash-of-hashes-Load-identical-file-multiple-times-with-different/m-p/664096#M198359</guid>
      <dc:creator>RichardDeVen</dc:creator>
      <dc:date>2020-06-22T23:43:03Z</dc:date>
    </item>
    <item>
      <title>Re: Hash of hashes: Load identical file multiple times with different where-clause</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Hash-of-hashes-Load-identical-file-multiple-times-with-different/m-p/664149#M198382</link>
      <description>&lt;P&gt;Hi &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31461"&gt;@mkeintz&lt;/a&gt;,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In my first attempt I built the "hoh" in the following manner:&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;declare hash hoh(multidata: "no");
hoh.defineKey("table");
hoh.defineData("h", "table");
hoh.defineDone();&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;That resulted in an error in the log file:&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;&lt;FONT color="#0000FF"&gt;NOTE: There were 50 observations read from the data set WORK.A.
NOTE: There were 19 observations read from the data set WORK.A.
      WHERE id&amp;lt;20;&lt;/FONT&gt;
&lt;FONT color="#FF0000"&gt;ERROR: Duplicate key.&lt;/FONT&gt;
&lt;FONT color="#0000FF"&gt;NOTE: There were 10 observations read from the data set WORK.A.
      WHERE id&amp;gt;40;&lt;/FONT&gt;
&lt;FONT color="#FF0000"&gt;ERROR: Duplicate key.&lt;/FONT&gt;
table=a rows=50
&lt;FONT color="#0000FF"&gt;NOTE: The SAS System stopped processing this step because of errors.
NOTE: DATA statement used (Total process time):
      real time           0.07 seconds
      cpu time            0.04 seconds&lt;/FONT&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Therefore I changed the multidata option from "no" to "yes":&lt;/P&gt;&lt;PRE&gt;declare hash hoh(multidata: "&lt;FONT color="#000000"&gt;&lt;STRONG&gt;yes&lt;/STRONG&gt;&lt;/FONT&gt;");
hoh.defineKey("table");
hoh.defineData("h", "table");
hoh.defineDone();&lt;/PRE&gt;&lt;P&gt;This results in the following log output:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;&lt;FONT color="#0000FF"&gt;NOTE: There were 50 observations read from the data set WORK.A.
NOTE: There were 19 observations read from the data set WORK.A.
      WHERE id&amp;lt;20;
NOTE: There were 10 observations read from the data set WORK.A.
      WHERE id&amp;gt;40;&lt;/FONT&gt;
table=a rows=50
table=a rows=19
table=a rows=10
&lt;FONT color="#0000FF"&gt;NOTE: DATA statement used (Total process time):
      real time           0.06 seconds
      cpu time            0.04 seconds&lt;/FONT&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;However, splitting the key "table" into 2 parts ("table" + "where") requires no more multidata: "yes" as you highlighted:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;declare hash hoh(multidata: "no");
hoh.defineKey("table", "where");
hoh.defineData("h", "table", "where");
hoh.defineDone();&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Therefore multidata option can be omitted:&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;declare hash hoh();
hoh.defineKey("table", "where");
hoh.defineData("h", "table", "where");
hoh.defineDone();&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 23 Jun 2020 06:08:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Hash-of-hashes-Load-identical-file-multiple-times-with-different/m-p/664149#M198382</guid>
      <dc:creator>left</dc:creator>
      <dc:date>2020-06-23T06:08:38Z</dc:date>
    </item>
  </channel>
</rss>

