<?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: Create a mapping table itterative process (loop?) in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Create-a-mapping-table-itterative-process-loop/m-p/495883#M130990</link>
    <description>&lt;P&gt;Give a look to the allChains macro here:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;A href="https://communities.sas.com/t5/SAS-Communities-Library/Finding-all-single-linked-chains-the-allChains-macro/ta-p/327252" target="_self"&gt;https://communities.sas.com/t5/SAS-Communities-Library/Finding-all-single-linked-chains-the-allChains-macro/ta-p/327252&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It is meant specifically for this kind of processing.&lt;/P&gt;</description>
    <pubDate>Sat, 15 Sep 2018 04:52:01 GMT</pubDate>
    <dc:creator>PGStats</dc:creator>
    <dc:date>2018-09-15T04:52:01Z</dc:date>
    <item>
      <title>Create a mapping table itterative process (loop?)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-a-mapping-table-itterative-process-loop/m-p/495879#M130988</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a database of two variable: account # and previous account #, where previous account # (previous_lnacctno), refers back&amp;nbsp;to a loan&amp;nbsp;(lnacctno) in the same table.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am currently trying to link every account # to the very &lt;U&gt;&lt;STRONG&gt;first&lt;/STRONG&gt; &lt;/U&gt;account, although I am sure there's a proper way to do it instead of doing iteratively as I am doing below. Ideally I would want to have as a final table every account # (lnacctno) and the first account (to be determined) and the number of accounts in between (there could be up to 7 or 8).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;NOTE: I also noticed that there is a data quality problem where some loans are referencing themselves. I think a simple correction in the initial "previous_acctn" table will fix it (where lnacctno = previous_lnacctno).&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql; 
create table previous_acctn as select 
previous_lnacctno, 
lnacctno 
from have; 
quit; 

proc sql; 
create table previous1 as select 
t1.lnacctno, t1.previous_lnacctno, t2.previous_lnacctno as previous_lnacctno2 
from previous_acctn t1 
left join previous_acctn t2 on (t1.previous_lnacctno = t2.lnacctno);
quit; 

proc sql; 
create table previous2 as select 
t1.lnacctno, t1.previous_lnacctno, t1.previous_lnacctno2, t2.previous_lnacctno as previous_lnacctno3
from previous1 t1 
left join previous_acctn t2 on (t1.previous_lnacctno2 = t2.lnacctno);
quit; 

proc sql; 
create table previous3 as select 
t1.lnacctno, t1.previous_lnacctno, t1.previous_lnacctno2, t1.previous_lnacctno3, t2.previous_lnacctno as previous_lnacctno4
from previous2 t1 
left join previous_acctn t2 on (t1.previous_lnacctno3 = t2.lnacctno);
quit; 

proc sql; 
create table previous4 as select 
t1.lnacctno, t1.previous_lnacctno, t1.previous_lnacctno2, t1.previous_lnacctno3, t1.previous_lnacctno4, t2.previous_lnacctno as previous_lnacctno5
from previous3 t1 
left join previous_acctn t2 on (t1.previous_lnacctno4 = t2.lnacctno);
quit; 


proc sql; 
create table previous5 as select 
t1.lnacctno, t1.previous_lnacctno, t1.previous_lnacctno2, t1.previous_lnacctno3, t1.previous_lnacctno4, t1.previous_lnacctno5, t2.previous_lnacctno as previous_lnacctno6
from previous4 t1 
left join previous_acctn t2 on (t1.previous_lnacctno5 = t2.lnacctno);
quit; 

proc sql; 
create table previous6 as select 
t1.lnacctno, t1.previous_lnacctno, 
t1.previous_lnacctno2, 
t1.previous_lnacctno3, 
t1.previous_lnacctno4, 
t1.previous_lnacctno5, 
t1.previous_lnacctno6,
t2.previous_lnacctno as previous_lnacctno7
from previous5 t1 
left join previous_acctn t2 on (t1.previous_lnacctno6 = t2.lnacctno);
quit; 

proc sql; 
create table previous7 as select 
t1.lnacctno, t1.previous_lnacctno, 
t1.previous_lnacctno2, 
t1.previous_lnacctno3, 
t1.previous_lnacctno4, 
t1.previous_lnacctno5, 
t1.previous_lnacctno6,
t1.previous_lnacctno7,
t2.previous_lnacctno as previous_lnacctno8
from previous6 t1 
left join previous_acctn t2 on (t1.previous_lnacctno7 = t2.lnacctno);
quit; &lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 15 Sep 2018 02:50:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-a-mapping-table-itterative-process-loop/m-p/495879#M130988</guid>
      <dc:creator>camfarrell25</dc:creator>
      <dc:date>2018-09-15T02:50:24Z</dc:date>
    </item>
    <item>
      <title>Re: Create a mapping table itterative process (loop?)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-a-mapping-table-itterative-process-loop/m-p/495880#M130989</link>
      <description>&lt;P&gt;If you have SAS/OR then PROC BOM works or there's some tree algorithms out there that do the recursive search.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here's an example that another user on here wrote (not my solution).&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It's a macro and there's some sample data at the bottom that shows how it works. It adds a variable to identify which are grouped together and then you can find the earliest value and assign that in a second step.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;A href="https://gist.github.com/statgeek/14e3aa2a9f718f551cd98134e9ceed30#file-subgraph_macro" target="_blank"&gt;https://gist.github.com/statgeek/14e3aa2a9f718f551cd98134e9ceed30#file-subgraph_macro&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/81770"&gt;@camfarrell25&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have a database of two variable: account # and previous account #, where previous account # (previous_lnacctno), refers back&amp;nbsp;to a loan&amp;nbsp;(lnacctno) in the same table.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am currently trying to link every account # to the very &lt;U&gt;&lt;STRONG&gt;first&lt;/STRONG&gt; &lt;/U&gt;account, although I am sure there's a proper way to do it instead of doing iteratively as I am doing below. Ideally I would want to have as a final table every account # (lnacctno) and the first account (to be determined) and the number of accounts in between (there could be up to 7 or 8).&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql; 
create table previous_acctn as select 
previous_lnacctno, 
lnacctno 
from have; 
quit; 

proc sql; 
create table previous1 as select 
t1.lnacctno, t1.previous_lnacctno, t2.previous_lnacctno as previous_lnacctno2 
from previous_acctn t1 
left join previous_acctn t2 on (t1.previous_lnacctno = t2.lnacctno);
quit; 

proc sql; 
create table previous2 as select 
t1.lnacctno, t1.previous_lnacctno, t1.previous_lnacctno2, t2.previous_lnacctno as previous_lnacctno3
from previous1 t1 
left join previous_acctn t2 on (t1.previous_lnacctno2 = t2.lnacctno);
quit; 

proc sql; 
create table previous3 as select 
t1.lnacctno, t1.previous_lnacctno, t1.previous_lnacctno2, t1.previous_lnacctno3, t2.previous_lnacctno as previous_lnacctno4
from previous2 t1 
left join previous_acctn t2 on (t1.previous_lnacctno3 = t2.lnacctno);
quit; 

proc sql; 
create table previous4 as select 
t1.lnacctno, t1.previous_lnacctno, t1.previous_lnacctno2, t1.previous_lnacctno3, t1.previous_lnacctno4, t2.previous_lnacctno as previous_lnacctno5
from previous3 t1 
left join previous_acctn t2 on (t1.previous_lnacctno4 = t2.lnacctno);
quit; 


proc sql; 
create table previous5 as select 
t1.lnacctno, t1.previous_lnacctno, t1.previous_lnacctno2, t1.previous_lnacctno3, t1.previous_lnacctno4, t1.previous_lnacctno5, t2.previous_lnacctno as previous_lnacctno6
from previous4 t1 
left join previous_acctn t2 on (t1.previous_lnacctno5 = t2.lnacctno);
quit; 

proc sql; 
create table previous6 as select 
t1.lnacctno, t1.previous_lnacctno, 
t1.previous_lnacctno2, 
t1.previous_lnacctno3, 
t1.previous_lnacctno4, 
t1.previous_lnacctno5, 
t1.previous_lnacctno6,
t2.previous_lnacctno as previous_lnacctno7
from previous5 t1 
left join previous_acctn t2 on (t1.previous_lnacctno6 = t2.lnacctno);
quit; 

proc sql; 
create table previous7 as select 
t1.lnacctno, t1.previous_lnacctno, 
t1.previous_lnacctno2, 
t1.previous_lnacctno3, 
t1.previous_lnacctno4, 
t1.previous_lnacctno5, 
t1.previous_lnacctno6,
t1.previous_lnacctno7,
t2.previous_lnacctno as previous_lnacctno8
from previous6 t1 
left join previous_acctn t2 on (t1.previous_lnacctno7 = t2.lnacctno);
quit; &lt;/CODE&gt;&lt;/PRE&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&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;</description>
      <pubDate>Sat, 15 Sep 2018 02:50:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-a-mapping-table-itterative-process-loop/m-p/495880#M130989</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2018-09-15T02:50:15Z</dc:date>
    </item>
    <item>
      <title>Re: Create a mapping table itterative process (loop?)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-a-mapping-table-itterative-process-loop/m-p/495883#M130990</link>
      <description>&lt;P&gt;Give a look to the allChains macro here:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;A href="https://communities.sas.com/t5/SAS-Communities-Library/Finding-all-single-linked-chains-the-allChains-macro/ta-p/327252" target="_self"&gt;https://communities.sas.com/t5/SAS-Communities-Library/Finding-all-single-linked-chains-the-allChains-macro/ta-p/327252&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It is meant specifically for this kind of processing.&lt;/P&gt;</description>
      <pubDate>Sat, 15 Sep 2018 04:52:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-a-mapping-table-itterative-process-loop/m-p/495883#M130990</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2018-09-15T04:52:01Z</dc:date>
    </item>
    <item>
      <title>Re: Create a mapping table itterative process (loop?)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-a-mapping-table-itterative-process-loop/m-p/495923#M131013</link>
      <description>&lt;P&gt;Post your data and output .&lt;/P&gt;</description>
      <pubDate>Sat, 15 Sep 2018 14:13:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-a-mapping-table-itterative-process-loop/m-p/495923#M131013</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2018-09-15T14:13:50Z</dc:date>
    </item>
    <item>
      <title>Re: Create a mapping table itterative process (loop?)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-a-mapping-table-itterative-process-loop/m-p/495964#M131031</link>
      <description>&lt;P&gt;This is a problem ready made for hash table use, as you will see in &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/462"&gt;@PGStats&lt;/a&gt;'s link.&amp;nbsp;&amp;nbsp; The code below also uses hash table:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want (keep=lnacctno steps previous_lnacctno
          rename=(previous_lnacctno=orig_lnacctno));

  if _n_=1 then do;
    if 0 then set have (keep=lnacctno previous_lnacctno);  ** Editted line **;
 
    declare hash h (dataset:'have (keep=lnacctno previous_lnacctno)');
      h.definekey('lnacctno');
      h.definedata('previous_lnacctno');
      h.definedone();
  end;

  set have;

  do steps=1 by 1 until (h.find(key:previous_lnacctno)^=0);
  end;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This code will work if a "parent" has more then one "child", but not if a child has more than one parent (i.e. multiple previous_lnacctno).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For each acctnum it will step through iterative lookups [h.find()] until the lookup fails (i.e. h.find() not equal 0 meaning an account has no parent).&amp;nbsp; That approach could be inefficient if you have long chains, because it ignores any acctnum's for which an intermediate acctnum has already been resolved.&amp;nbsp; It just steps all the way up to the first account.&amp;nbsp; But for chains of 7 or 8, I don't think there would be much impact.&lt;/P&gt;</description>
      <pubDate>Sat, 15 Sep 2018 16:52:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-a-mapping-table-itterative-process-loop/m-p/495964#M131031</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2018-09-15T16:52:30Z</dc:date>
    </item>
    <item>
      <title>Re: Create a mapping table itterative process (loop?)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-a-mapping-table-itterative-process-loop/m-p/496018#M131062</link>
      <description>&lt;P&gt;Create a format that gives you a previous account for a given account, and a missing value for "others".&lt;/P&gt;
&lt;P&gt;Then you only have to loop until you arrive at missing.&lt;/P&gt;
&lt;P&gt;As with the hash object, all account combinations have to fit into available memory.&lt;/P&gt;</description>
      <pubDate>Sun, 16 Sep 2018 08:32:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-a-mapping-table-itterative-process-loop/m-p/496018#M131062</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2018-09-16T08:32:12Z</dc:date>
    </item>
    <item>
      <title>Re: Create a mapping table itterative process (loop?)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-a-mapping-table-itterative-process-loop/m-p/496760#M131466</link>
      <description>&lt;P&gt;You might want to put a limit on that loop, just in case lnacctno = previous_lnacctno somewhere.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;do steps=1 to 999 by 1 until (h.find(key:previous_lnacctno)^=0);
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 18 Sep 2018 22:02:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-a-mapping-table-itterative-process-loop/m-p/496760#M131466</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2018-09-18T22:02:03Z</dc:date>
    </item>
    <item>
      <title>Re: Create a mapping table itterative process (loop?)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-a-mapping-table-itterative-process-loop/m-p/496768#M131471</link>
      <description>&lt;P&gt;Thank you!&amp;nbsp;&lt;/P&gt;&lt;P&gt;I tried it and it seems to work, but for some, it returns a missing value instead of a previous account number.. i'm not quite familiar with hash tables so I'm not sure where the bug is. The number of steps is greater than 1 but the&amp;nbsp;previous loan account number is missing. Any ideas?&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 18 Sep 2018 22:08:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-a-mapping-table-itterative-process-loop/m-p/496768#M131471</guid>
      <dc:creator>camfarrell25</dc:creator>
      <dc:date>2018-09-18T22:08:02Z</dc:date>
    </item>
    <item>
      <title>Re: Create a mapping table itterative process (loop?)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-a-mapping-table-itterative-process-loop/m-p/496807#M131495</link>
      <description>&lt;P&gt;This will happen if the previous account number is missing for some account number. You could simply filter these cases out to fix the problem.&lt;/P&gt;</description>
      <pubDate>Wed, 19 Sep 2018 03:13:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-a-mapping-table-itterative-process-loop/m-p/496807#M131495</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2018-09-19T03:13:59Z</dc:date>
    </item>
  </channel>
</rss>

