<?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 object data definition in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Hash-object-data-definition/m-p/547576#M151756</link>
    <description>Thank you &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13593"&gt;@Andreas&lt;/a&gt;_Id. Its working. Can you please tell me why this is not recommendable. Does this because of using _n_=1, do until and set statements together? Will affect the process speed and memory at anyway?</description>
    <pubDate>Mon, 01 Apr 2019 09:11:15 GMT</pubDate>
    <dc:creator>DayaG</dc:creator>
    <dc:date>2019-04-01T09:11:15Z</dc:date>
    <item>
      <title>Hash object data definition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Hash-object-data-definition/m-p/544790#M150673</link>
      <description>&lt;P&gt;Hi there,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I recently started learning hash objects, while I'm surfing net, I found we can also use set statement to pass the data and key values into hash objects. I have been trying to use this approach as shown below. What i'm looking is, i want to use set statement instead of using dataset tag. And would like to get merge both datasets (forget about the duplicates in hash object keys and values).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Raw datasets:&lt;/P&gt;&lt;P&gt;data participants;&lt;BR /&gt;input name $ gender:$1. treatment $;&lt;BR /&gt;datalines;&lt;BR /&gt;John M Placebo&lt;BR /&gt;Ronald M Drug-A&lt;BR /&gt;Barbara F Drug-B&lt;BR /&gt;Alice F Drug-A&lt;BR /&gt;;&lt;/P&gt;&lt;P&gt;data weight(drop=i);&lt;BR /&gt;input date:DATE9. @;&lt;BR /&gt;do i = 1 to 4;&lt;BR /&gt;input name $ weight @;&lt;BR /&gt;output;&lt;BR /&gt;end;&lt;BR /&gt;/* For brevity, only two dates are listed below */&lt;BR /&gt;datalines;&lt;BR /&gt;05May2006 Barbara 125 Alice 130 Ronald 170 John 160&lt;BR /&gt;04Jun2006 Barbara 122 Alice 133 Ronald 168 John 155&lt;BR /&gt;;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;U&gt;Tried Code:&lt;/U&gt;&lt;/P&gt;&lt;P&gt;data results ;&lt;/P&gt;&lt;P&gt;if _n_=1 then do ;&lt;BR /&gt;declare hash h( ) ;&lt;BR /&gt;h.definekey('name') ;&lt;BR /&gt;h.definedata('name','weight') ;&lt;BR /&gt;h.definedone() ;&lt;BR /&gt;end;&lt;/P&gt;&lt;P&gt;set weight end=eof ;&lt;BR /&gt;if h.find() ne 0 then&lt;BR /&gt;h.add() ;&lt;/P&gt;&lt;P&gt;set participants ;&lt;BR /&gt;if h.find()=0 then&lt;BR /&gt;output ;&lt;/P&gt;&lt;P&gt;h.output(dataset: 'a');&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Using this, am getting only two records, as shown in the trial image.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Im getting correct results using dataset tag argument:&lt;BR /&gt;&lt;U&gt;Code:&lt;/U&gt;&lt;/P&gt;&lt;P&gt;data results1 ;&lt;/P&gt;&lt;P&gt;length weight 8;&lt;BR /&gt;/*attrib weight length=8;*/&lt;BR /&gt;/*retain weight . ;*/&lt;BR /&gt;if _n_=1 then do ;&lt;BR /&gt;declare hash h(dataset: 'weight') ;&lt;BR /&gt;h.definekey('name') ;&lt;BR /&gt;h.definedata('weight') ;&lt;BR /&gt;h.definedone() ;&lt;BR /&gt;call missing(weight) ;&lt;BR /&gt;end;&lt;BR /&gt;set participants ;&lt;BR /&gt;if h.find()=0 then output ;&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;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;PLEASE HELP ME IN UNDERSTANDING BOTH&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks in advance&lt;/P&gt;</description>
      <pubDate>Thu, 21 Mar 2019 10:19:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Hash-object-data-definition/m-p/544790#M150673</guid>
      <dc:creator>DayaG</dc:creator>
      <dc:date>2019-03-21T10:19:35Z</dc:date>
    </item>
    <item>
      <title>Re: Hash object data definition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Hash-object-data-definition/m-p/544811#M150676</link>
      <description>&lt;P&gt;Please post code using the running-man icon in Rich Text view to preserve formatting.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The first set statement reads just one observation from work.weight (Barbara) and adds it to the hash. Before any other obs are read from work.weight the first obs from work.participants (John) is read. At this point "John" is not in the hash, so no observation is written to work.results. To solve the problem you have to move the code filling the hash into if _n_ = 1 and write an explicit loop to read all obs before work.participants is processed. BUT i really can't recommend doing this.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data work.narf;
    if _n_=1 then
        do;
            declare hash h();
            h.definekey('name');
            h.definedata('name', 'weight');
            h.definedone();

            do until (jobDone);
                set work.weight end=jobDone;

                if h.check() ^=0 then
                    do;
                        h.add();
                    end;
            end;
        end;
        
    set work.participants;
    
    if h.find() = 0 then output;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 21 Mar 2019 11:06:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Hash-object-data-definition/m-p/544811#M150676</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2019-03-21T11:06:46Z</dc:date>
    </item>
    <item>
      <title>Re: Hash object data definition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Hash-object-data-definition/m-p/544897#M150700</link>
      <description>&lt;P&gt;The first code gives you an incorrect result because you fill up the hash object with values from &lt;STRONG&gt;weight&lt;/STRONG&gt; as you look for values in&amp;nbsp;&lt;STRONG&gt;participants&lt;/STRONG&gt;. You have to fill up your hash object lookup table entirely before you do the actual lookup.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;As an example, in the first iteration of the data step you correctly declare the hash object. Then you add the key value&amp;nbsp;&lt;SPAN&gt;&lt;STRONG&gt;Barbara&amp;nbsp;&lt;/STRONG&gt;to the hash object. Next, you&amp;nbsp;&amp;nbsp;read in the first observation from&amp;nbsp;&lt;STRONG&gt;participants&lt;/STRONG&gt; (John) and look for &lt;STRONG&gt;John&amp;nbsp;&lt;/STRONG&gt;in the hash object. However, &lt;STRONG&gt;John&lt;/STRONG&gt; is not there yet because you only added&amp;nbsp;&lt;STRONG&gt;Barbara&lt;/STRONG&gt; so far.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;It seems like you are well on your way to learning hash objects and hats off to you because it is an advance topic and the syntax is funky at first &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt; For an excellent&amp;nbsp;book, check out&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN class="a-size-extra-large"&gt;&lt;A href="https://www.amazon.com/Management-Solutions-Using-Table-Operations-ebook/dp/B07FCNMQL1" target="_self"&gt;Data Management Solutions Using SAS Hash Table Operations&lt;/A&gt;.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN class="a-size-extra-large"&gt;The code can also be written like this&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data results;
if _n_=1 then do;
   declare hash h();
   h.definekey('name');
   h.definedata('name','weight');
   h.definedone();

   do until (eof);
      set weight end=eof;
      h.replace();
   end;
end;

set participants;
if h.find()=0;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 21 Mar 2019 15:23:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Hash-object-data-definition/m-p/544897#M150700</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2019-03-21T15:23:54Z</dc:date>
    </item>
    <item>
      <title>Re: Hash object data definition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Hash-object-data-definition/m-p/547576#M151756</link>
      <description>Thank you &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13593"&gt;@Andreas&lt;/a&gt;_Id. Its working. Can you please tell me why this is not recommendable. Does this because of using _n_=1, do until and set statements together? Will affect the process speed and memory at anyway?</description>
      <pubDate>Mon, 01 Apr 2019 09:11:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Hash-object-data-definition/m-p/547576#M151756</guid>
      <dc:creator>DayaG</dc:creator>
      <dc:date>2019-04-01T09:11:15Z</dc:date>
    </item>
    <item>
      <title>Re: Hash object data definition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Hash-object-data-definition/m-p/547579#M151757</link>
      <description>Thank you for your clear cut answer and for the book suggestion. It is working. Your positive complement made me more enthusiastic learner towards Advance programming techniques.</description>
      <pubDate>Mon, 01 Apr 2019 09:23:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Hash-object-data-definition/m-p/547579#M151757</guid>
      <dc:creator>DayaG</dc:creator>
      <dc:date>2019-04-01T09:23:33Z</dc:date>
    </item>
  </channel>
</rss>

