<?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: Find the oldest account in all the pre-accounts in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Find-the-oldest-account-in-all-the-pre-accounts/m-p/727381#M226217</link>
    <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/202610"&gt;@Emjay&lt;/a&gt;&amp;nbsp;I will try to address all your questions &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;U&gt;&lt;EM&gt;"And how come the ouput is only the desired&amp;nbsp;&lt;STRONG&gt;Account&lt;/STRONG&gt;&amp;nbsp;44 and 45? Which never enter the if or the do?"&lt;/EM&gt;&lt;/U&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This is ensured by (4). Think of it like this. If the current&amp;nbsp;&lt;SPAN&gt;value of&amp;nbsp;&lt;/SPAN&gt;&lt;STRONG&gt;Account&lt;/STRONG&gt;&lt;SPAN&gt;&amp;nbsp;exists &lt;U&gt;&lt;EM&gt;anywhere&lt;/EM&gt;&lt;/U&gt; as an&amp;nbsp;&lt;/SPAN&gt;&lt;STRONG&gt;Account_old&lt;/STRONG&gt;. Then we're not interested because then the Account is a&amp;nbsp;&lt;SPAN&gt;pre-account to another account.&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;U&gt;&lt;EM&gt;How will this become a loop, as there is only one Account_old to the just read Account?&lt;/EM&gt;&lt;/U&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;U&gt;&lt;/U&gt;It becomes a loop that can iterate more than once because after the lookup, I set Account_Old = Account_Oldest and use that as the lookup value. We continue that process until we reach the end of the "chain". Meaning that the lookup is unsuccesful.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Try adding a Put Statement to the loop like this and check the log. That will give you a nice overview of the iterations.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;   do until (rc);
      rc = hh.find(key : Account_old);
      put _all_;
      Account_old = Account_oldest;
   end;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;U&gt;&lt;EM&gt;The first time the&amp;nbsp;&lt;STRONG&gt;rc&lt;/STRONG&gt;&amp;nbsp;is a sucess is when&amp;nbsp;&lt;STRONG&gt;Account&lt;/STRONG&gt;&amp;nbsp;is 41 and&amp;nbsp;&lt;STRONG&gt;Account_old&lt;/STRONG&gt;&amp;nbsp;is 40. And there&amp;nbsp;&lt;STRONG&gt;Account_old&lt;/STRONG&gt;&amp;nbsp;is set to 2008.&lt;/EM&gt;&lt;/U&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Strictly speaking: No. We do not even go past step (4) for Account = 41. Because 41 &lt;U&gt;&lt;EM&gt;does&lt;/EM&gt;&lt;/U&gt;&amp;nbsp;exist as a pre-account to another account.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;EM&gt;&lt;U&gt;In the end this will lead to the desired&amp;nbsp;&lt;STRONG&gt;Account&lt;/STRONG&gt;&amp;nbsp;44 and&amp;nbsp;&lt;STRONG&gt;Open_old&lt;/STRONG&gt;&amp;nbsp;2008, but only because&amp;nbsp;&lt;STRONG&gt;Account_old&lt;/STRONG&gt;&amp;nbsp;38 shows up first in the list and has the oldest date. If&amp;nbsp;&lt;STRONG&gt;Account_old&lt;/STRONG&gt;&amp;nbsp;39 would start in 2007, the final result would still be&amp;nbsp;&lt;STRONG&gt;Account&lt;/STRONG&gt;&amp;nbsp;44 and&amp;nbsp;&lt;STRONG&gt;Open_old&lt;/STRONG&gt;&amp;nbsp;2008, right?&lt;/U&gt;&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Correct. Good catch &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt; But only because by default, the hash object allows only 1 item per key value. This can be altered, but for simplicity, this is enough.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also, please accept my code as the accepted solution if it solves your problem. And do not hesitate to ask further questions.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Kind Regards.&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 18 Mar 2021 12:35:26 GMT</pubDate>
    <dc:creator>PeterClemmensen</dc:creator>
    <dc:date>2021-03-18T12:35:26Z</dc:date>
    <item>
      <title>Find the oldest account in all the pre-accounts</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-the-oldest-account-in-all-the-pre-accounts/m-p/727212#M226121</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;I have a list of accounts and their pre-accounts with the opening date of the old account.&lt;/P&gt;
&lt;P&gt;One account can have more than one pre-account and and one pre-account can also be split into more than one new account.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is an example:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input Account Account_old Open_old;
cards;
40 38 2008
40 39 2009
41 40 2010
42 41 2011
43 42 2012
44 43 2013
45 43 2013
;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;So account 38 and 39 are combined together to form account 40. Account 40 is then transformed to account 41 and so on.&lt;/P&gt;
&lt;P&gt;Finally account 43 is split to account 44 and 45.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The number of transformations is unknow.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In the end I want a list of all the active accounts (=accounts that are no pre-account to others) and their oldest pre-account.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
input Account_Active Account_oldest Open_since;
cards;
44 38 2008
45 38 2008
;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I can not think of a way without knowing how many tranformations must be done.&lt;/P&gt;
&lt;P&gt;I hope you can... &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 17 Mar 2021 18:43:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-the-oldest-account-in-all-the-pre-accounts/m-p/727212#M226121</guid>
      <dc:creator>Emjay</dc:creator>
      <dc:date>2021-03-17T18:43:44Z</dc:date>
    </item>
    <item>
      <title>Re: Find the oldest account in all the pre-accounts</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-the-oldest-account-in-all-the-pre-accounts/m-p/727220#M226126</link>
      <description>&lt;P&gt;How about this&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input Account Account_old Open_old;
cards;
40 38 2008
40 39 2009
41 40 2010
42 41 2011
43 42 2012
44 43 2013
45 43 2013
;

data want(keep = Account Account_oldest Open_old);

   if _N_ = 1 then do;
      dcl hash h(dataset : "have");
      h.definekey("Account_old");
      h.definedone();

      dcl hash hh(dataset : "have(rename = Account_old = Account_oldest");
      hh.definekey("Account");
      hh.definedata("Account_oldest", "Open_old");
      hh.definedone();
   end;

   set have;

   if h.check(key : Account);

   do until (rc);
      rc = hh.find(key : Account_old);
      Account_old = Account_oldest;
   end;

run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Result:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;Account Open_old Account_oldest 
44      2008     38 
45      2008     38 &amp;nbsp;&amp;nbsp;&lt;/PRE&gt;</description>
      <pubDate>Wed, 17 Mar 2021 19:28:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-the-oldest-account-in-all-the-pre-accounts/m-p/727220#M226126</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2021-03-17T19:28:17Z</dc:date>
    </item>
    <item>
      <title>Re: Find the oldest account in all the pre-accounts</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-the-oldest-account-in-all-the-pre-accounts/m-p/727308#M226186</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31304"&gt;@PeterClemmensen&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Wow, what the hell is THIS??&lt;/P&gt;
&lt;P&gt;I've never seen anything like that before...&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Could you please enlightening me a little? Looks like I have a lot of reading coming up... &lt;span class="lia-unicode-emoji" title=":winking_face:"&gt;😉&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 18 Mar 2021 06:35:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-the-oldest-account-in-all-the-pre-accounts/m-p/727308#M226186</guid>
      <dc:creator>Emjay</dc:creator>
      <dc:date>2021-03-18T06:35:07Z</dc:date>
    </item>
    <item>
      <title>Re: Find the oldest account in all the pre-accounts</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-the-oldest-account-in-all-the-pre-accounts/m-p/727310#M226188</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/202610"&gt;@Emjay&lt;/a&gt;&amp;nbsp;I like your enthusiasm &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt; This is a hash object.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Shortly put, a hash object is a dynamic in-memory data structure available in the SAS data step. The hash object contains key and data values. Therefore, it is often used as an effective lookup tool. However, due to its dynamic nature, it has many other purposes. This is a good example of one of them.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P data-unlink="true"&gt;Speaking of reading. If you want to learn more, read&amp;nbsp;&lt;A href="https://www.sas.com/store/da_dk/books/categories/examples/data-management-solutions-using-sas-hash-table-operations-a-business-intelligence-case-study/prodBK_69153_en.html" target="_self"&gt;Data Management Solutions Using SAS Hash Table Operations&lt;/A&gt;&amp;nbsp;by&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/21262"&gt;@hashman&lt;/a&gt;&amp;nbsp;and&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13569"&gt;@DonH&lt;/a&gt;.&amp;nbsp;&lt;/P&gt;
&lt;P data-unlink="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P data-unlink="true"&gt;Regarding the code, I have added points and explanations below.&lt;/P&gt;
&lt;P data-unlink="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;Declare the hash object &lt;STRONG&gt;h&lt;/STRONG&gt;. Read the data set have into &lt;STRONG&gt;h&lt;/STRONG&gt; and use &lt;STRONG&gt;Account_old&amp;nbsp;&lt;/STRONG&gt;as key.&lt;/LI&gt;
&lt;LI&gt;Declare the hash object &lt;STRONG&gt;hh&lt;/STRONG&gt;. Read the data set have into &lt;STRONG&gt;h&amp;nbsp;&lt;/STRONG&gt;and use &lt;STRONG&gt;Account&amp;nbsp;&lt;/STRONG&gt;as key and&amp;nbsp;&lt;STRONG&gt;Account_oldest&lt;/STRONG&gt;&amp;nbsp;(renamed) and &lt;STRONG&gt;Open_old&lt;/STRONG&gt; as data variables.&lt;/LI&gt;
&lt;LI&gt;Read the have data set sequentially.&lt;/LI&gt;
&lt;LI&gt;Proceed only if the current value of &lt;STRONG&gt;Account&lt;/STRONG&gt;&amp;nbsp;exists as an&amp;nbsp;&lt;STRONG&gt;Account_old&lt;/STRONG&gt;. Meaning, we want only&amp;nbsp;&lt;SPAN&gt;accounts that are no pre-account to others.&lt;/SPAN&gt;&lt;/LI&gt;
&lt;LI&gt;I use a &lt;A href="https://documentation.sas.com/?cdcId=pgmsascdc&amp;amp;cdcVersion=9.4_3.5&amp;amp;docsetId=lestmtsref&amp;amp;docsetTarget=p1021qt3a3n8m2n1vh11ggzhu57n.htm&amp;amp;locale=en" target="_self"&gt;Do Until&lt;/A&gt; Loop to perform a series of lookups. For example, for Account 44, we want to follow the path 44-43-42-41-40-38. This is what is ensured in (5), (6) and (7).&lt;/LI&gt;
&lt;LI&gt;Perform the actual lookup using the &lt;A href="https://documentation.sas.com/?cdcId=pgmsascdc&amp;amp;cdcVersion=9.4_3.5&amp;amp;docsetId=lecompobjref&amp;amp;docsetTarget=p0v7y0he52atdan1iwtpwryes1dw.htm&amp;amp;locale=en" target="_self"&gt;Find() Method&lt;/A&gt;.&amp;nbsp;&lt;/LI&gt;
&lt;LI&gt;Set Prepare the next lookup in the series by setting Account_Old =&amp;nbsp;Account_Oldest.&lt;/LI&gt;
&lt;/OL&gt;
&lt;P data-unlink="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P data-unlink="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want(keep = Account Account_oldest Open_old);

   if _N_ = 1 then do;
      dcl hash h(dataset : "have");                                         /* 1 */
      h.definekey("Account_old");
      h.definedone();

      dcl hash hh(dataset : "have(rename = Account_old = Account_oldest");  /* 2 */
      hh.definekey("Account");
      hh.definedata("Account_oldest", "Open_old");
      hh.definedone();
   end;

   set have;                                                                /* 3 */

   if h.check(key : Account);                                               /* 4 */

   do until (rc);                                                           /* 5 */
      rc = hh.find(key : Account_old);                                      /* 6 */
      Account_old = Account_oldest;                                         /* 7 */
   end;

run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 18 Mar 2021 07:11:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-the-oldest-account-in-all-the-pre-accounts/m-p/727310#M226188</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2021-03-18T07:11:15Z</dc:date>
    </item>
    <item>
      <title>Re: Find the oldest account in all the pre-accounts</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-the-oldest-account-in-all-the-pre-accounts/m-p/727357#M226205</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31304"&gt;@PeterClemmensen&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks for all the explantions. But I'm not sure if I understand it correctly:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So if the data is read sequentially it means in&amp;nbsp; the first line:&lt;/P&gt;
&lt;P&gt;/* 4 */&lt;/P&gt;
&lt;LI-CODE lang="sas"&gt;if h.check(key : Account);  /* if current account 40 is in the account_old_key of h -&amp;gt; yes */&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;/* 5 */&lt;/P&gt;
&lt;LI-CODE lang="sas"&gt;do until (rc); /* If rc is a success it's 0. So I guess in this case&amp;nbsp;this means 'Do until rc = 0' */
      rc = hh.find(key : Account_old);  /* Is Account_old(=38) in the Account_key -&amp;gt; no */
      Account_old = Account_oldest; /* Account_old = 2008 */
end;&lt;/LI-CODE&gt;
&lt;P&gt;How will this become a loop, as there is only one Account_old to the just read Account?&lt;/P&gt;
&lt;P&gt;Or will it loop over all possible Account_olds? I'm a bit confused by "data is read sequnetially".&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The first time the &lt;STRONG&gt;rc&lt;/STRONG&gt; is a sucess is when &lt;STRONG&gt;Account&lt;/STRONG&gt; is 41 and &lt;STRONG&gt;Account_old&lt;/STRONG&gt; is 40. And there &lt;STRONG&gt;Account_old&lt;/STRONG&gt; is set to 2008.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In the end this will lead to the desired &lt;STRONG&gt;Account&lt;/STRONG&gt; 44 and &lt;STRONG&gt;Open_old&lt;/STRONG&gt; 2008, but only because &lt;STRONG&gt;Account_old&lt;/STRONG&gt; 38 shows up first in the list and has the oldest date. If &lt;STRONG&gt;Account_old&lt;/STRONG&gt; 39 would start in 2007, the final result would still be &lt;STRONG&gt;Account&lt;/STRONG&gt; 44 and &lt;STRONG&gt;Open_old&lt;/STRONG&gt; 2008, right?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And how come the ouput is only the desired &lt;STRONG&gt;Account&lt;/STRONG&gt; 44 and 45? Which never enter the if or the do?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Puh, this hash-thing is quite confusing for me...&lt;/P&gt;</description>
      <pubDate>Thu, 18 Mar 2021 10:56:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-the-oldest-account-in-all-the-pre-accounts/m-p/727357#M226205</guid>
      <dc:creator>Emjay</dc:creator>
      <dc:date>2021-03-18T10:56:18Z</dc:date>
    </item>
    <item>
      <title>Re: Find the oldest account in all the pre-accounts</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-the-oldest-account-in-all-the-pre-accounts/m-p/727381#M226217</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/202610"&gt;@Emjay&lt;/a&gt;&amp;nbsp;I will try to address all your questions &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;U&gt;&lt;EM&gt;"And how come the ouput is only the desired&amp;nbsp;&lt;STRONG&gt;Account&lt;/STRONG&gt;&amp;nbsp;44 and 45? Which never enter the if or the do?"&lt;/EM&gt;&lt;/U&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This is ensured by (4). Think of it like this. If the current&amp;nbsp;&lt;SPAN&gt;value of&amp;nbsp;&lt;/SPAN&gt;&lt;STRONG&gt;Account&lt;/STRONG&gt;&lt;SPAN&gt;&amp;nbsp;exists &lt;U&gt;&lt;EM&gt;anywhere&lt;/EM&gt;&lt;/U&gt; as an&amp;nbsp;&lt;/SPAN&gt;&lt;STRONG&gt;Account_old&lt;/STRONG&gt;. Then we're not interested because then the Account is a&amp;nbsp;&lt;SPAN&gt;pre-account to another account.&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;U&gt;&lt;EM&gt;How will this become a loop, as there is only one Account_old to the just read Account?&lt;/EM&gt;&lt;/U&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;U&gt;&lt;/U&gt;It becomes a loop that can iterate more than once because after the lookup, I set Account_Old = Account_Oldest and use that as the lookup value. We continue that process until we reach the end of the "chain". Meaning that the lookup is unsuccesful.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Try adding a Put Statement to the loop like this and check the log. That will give you a nice overview of the iterations.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;   do until (rc);
      rc = hh.find(key : Account_old);
      put _all_;
      Account_old = Account_oldest;
   end;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;U&gt;&lt;EM&gt;The first time the&amp;nbsp;&lt;STRONG&gt;rc&lt;/STRONG&gt;&amp;nbsp;is a sucess is when&amp;nbsp;&lt;STRONG&gt;Account&lt;/STRONG&gt;&amp;nbsp;is 41 and&amp;nbsp;&lt;STRONG&gt;Account_old&lt;/STRONG&gt;&amp;nbsp;is 40. And there&amp;nbsp;&lt;STRONG&gt;Account_old&lt;/STRONG&gt;&amp;nbsp;is set to 2008.&lt;/EM&gt;&lt;/U&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Strictly speaking: No. We do not even go past step (4) for Account = 41. Because 41 &lt;U&gt;&lt;EM&gt;does&lt;/EM&gt;&lt;/U&gt;&amp;nbsp;exist as a pre-account to another account.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;EM&gt;&lt;U&gt;In the end this will lead to the desired&amp;nbsp;&lt;STRONG&gt;Account&lt;/STRONG&gt;&amp;nbsp;44 and&amp;nbsp;&lt;STRONG&gt;Open_old&lt;/STRONG&gt;&amp;nbsp;2008, but only because&amp;nbsp;&lt;STRONG&gt;Account_old&lt;/STRONG&gt;&amp;nbsp;38 shows up first in the list and has the oldest date. If&amp;nbsp;&lt;STRONG&gt;Account_old&lt;/STRONG&gt;&amp;nbsp;39 would start in 2007, the final result would still be&amp;nbsp;&lt;STRONG&gt;Account&lt;/STRONG&gt;&amp;nbsp;44 and&amp;nbsp;&lt;STRONG&gt;Open_old&lt;/STRONG&gt;&amp;nbsp;2008, right?&lt;/U&gt;&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Correct. Good catch &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt; But only because by default, the hash object allows only 1 item per key value. This can be altered, but for simplicity, this is enough.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also, please accept my code as the accepted solution if it solves your problem. And do not hesitate to ask further questions.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Kind Regards.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 18 Mar 2021 12:35:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-the-oldest-account-in-all-the-pre-accounts/m-p/727381#M226217</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2021-03-18T12:35:26Z</dc:date>
    </item>
    <item>
      <title>Re: Find the oldest account in all the pre-accounts</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-the-oldest-account-in-all-the-pre-accounts/m-p/727504#M226292</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31304"&gt;@PeterClemmensen&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Sorry for my late reply, but this day is just a mess...&lt;/P&gt;
&lt;P&gt;Thanks a lot for your patience with me. &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I've added the &lt;STRONG&gt;put _all_&amp;nbsp;&lt;/STRONG&gt;which leaves me even more confused.&lt;/P&gt;
&lt;P&gt;The sas-documentation that you added states for &lt;STRONG&gt;KEY: keyvalue&lt;/STRONG&gt;:&lt;/P&gt;
&lt;P&gt;&lt;EM&gt;specifies the key value whose type must match the corresponding key variable that is specified in a DEFINEKEY method call.&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Which left me thinking that&lt;/P&gt;
&lt;LI-CODE lang="sas"&gt;if h.check(key : Account);&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;means:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if have.Account in any h.Account_old&amp;nbsp;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;In Account_ols are all the numbers from 38-43 and now I see that the first output is Account 44 so the &lt;STRONG&gt;if&lt;/STRONG&gt; must mean exactly the opposite.&lt;/P&gt;
&lt;P&gt;This leaves me with the assumption that&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;hh.find(key : Account_old);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;also doesn't translate into&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;look for have.Account_old in all the hh.Accounts&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;This combined with the fact, that if I change my original data to&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="sas"&gt;data have;
input Account Account_old Open_old;
cards;
40 38 2008
40 39 2007
41 40 2010
42 41 2011
43 42 2012
44 43 2013
45 43 2013
;
run;&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;so that the oldest is not in &lt;STRONG&gt;Account_old&lt;/STRONG&gt; 38 but 39 a little shattering.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I'm terribly sorry to ask but can you think of a non-hash way? I need to catch a deadline and I'm afraid till then I won't wrap my head around hash-objects...&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 18 Mar 2021 18:16:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-the-oldest-account-in-all-the-pre-accounts/m-p/727504#M226292</guid>
      <dc:creator>Emjay</dc:creator>
      <dc:date>2021-03-18T18:16:26Z</dc:date>
    </item>
    <item>
      <title>Re: Find the oldest account in all the pre-accounts</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-the-oldest-account-in-all-the-pre-accounts/m-p/727619#M226343</link>
      <description>&lt;P&gt;First, thanks&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31304"&gt;@PeterClemmensen&lt;/a&gt;&amp;nbsp;for the reference to the book.&lt;BR /&gt;&lt;BR /&gt;It it not clear what you are asking&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/202610"&gt;@Emjay&lt;/a&gt;. since you reference two code snippets without a specific question.&lt;BR /&gt;&lt;BR /&gt;I suspect your issue is that you don't recognize that the hash object is actually a data table that you can create and access inside a data step.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So what we have here is three distinct tables of data being accessed inside the data step.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;The hash object h is created from your data set and is all the distinct values of account_old.&lt;/LI&gt;
&lt;LI&gt;The hash object hh is the list of all account by account_old combinations - with account_old renamed to account_oldest.&lt;/LI&gt;
&lt;LI&gt;Your input data have that is read in the SET statement.&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;The set statement is looping thru each observation in the have data set - on observation per execution of the DATA step.&lt;BR /&gt;&lt;BR /&gt;It then subsets the data (step 4 in the code) to only continue processing if the account number is not found in the hash table h. The check method returns 0 if it is found and returns a non-zero value if not. So this is subsetting the data to just those that were never a pre-account.&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;Next the do until construct is simply used to find the first row in the hash table hh that has the the value of account on the current row equal to the account_old value in HH. Since your data are sorted so the oldest pre-account is listed first, as soon as the find method finds it, the value is copied and the loop ends.&lt;/P&gt;</description>
      <pubDate>Fri, 19 Mar 2021 01:01:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-the-oldest-account-in-all-the-pre-accounts/m-p/727619#M226343</guid>
      <dc:creator>DonH</dc:creator>
      <dc:date>2021-03-19T01:01:40Z</dc:date>
    </item>
    <item>
      <title>Re: Find the oldest account in all the pre-accounts</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-the-oldest-account-in-all-the-pre-accounts/m-p/727782#M226425</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13569"&gt;@DonH&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;thanks for the explanations.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I understood that the hash objects are tables, my main issue was the if-statement.&lt;/P&gt;
&lt;P&gt;The key was this sentence:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;The check method returns 0 if it is found and returns a non-zero value if not. So this is subsetting the data to just those that were never a pre-account.&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 19 Mar 2021 15:31:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-the-oldest-account-in-all-the-pre-accounts/m-p/727782#M226425</guid>
      <dc:creator>Emjay</dc:creator>
      <dc:date>2021-03-19T15:31:45Z</dc:date>
    </item>
  </channel>
</rss>

