<?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: Do loop until end? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Do-loop-until-end/m-p/446435#M112002</link>
    <description>&lt;P&gt;Thanks&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13674"&gt;@LinusH&lt;/a&gt;&amp;nbsp;rooting for me.&lt;/P&gt;
&lt;P&gt;The best choice is using SAS/OR if you have it,&amp;nbsp;@RobPratt might give you some code.&lt;/P&gt;
&lt;P&gt;Or check SAS/OR documentation ,I remembered there is already an example about Connection Component.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data have;
infile cards ;
input from $  to $ ;
cards;
1     2
1     3
4     5
5     2
9     4
6     7
8     7
;
run;
data full;
  set have end=last;
  if _n_ eq 1 then do;
   declare hash h();
    h.definekey('node');
     h.definedata('node');
     h.definedone();
  end;
  output;
  node=from; h.replace();
  from=to; to=node;
  output;
  node=from; h.replace();
  if last then h.output(dataset:'node');
  drop node;
run;


data want(keep=node household);
declare hash ha(ordered:'a');
declare hiter hi('ha');
ha.definekey('count');
ha.definedata('last');
ha.definedone();
declare hash _ha(hashexp: 20);
_ha.definekey('key');
_ha.definedone();

if 0 then set full;
declare hash from_to(dataset:'full(where=(from is not missing and to is not missing))',hashexp:20,multidata:'y');
 from_to.definekey('from');
 from_to.definedata('to');
 from_to.definedone();

if 0 then set node;
declare hash no(dataset:'node');
declare hiter hi_no('no');
 no.definekey('node');
 no.definedata('node');
 no.definedone();
 

do while(hi_no.next()=0);
 household+1; output;
 count=1;
 key=node;_ha.add();
 last=node;ha.add();
 rc=hi.first();
 do while(rc=0);
   from=last;rx=from_to.find();
   do while(rx=0);
     key=to;ry=_ha.check();
      if ry ne 0 then do;
       node=to;output;rr=no.remove(key:node);
       key=to;_ha.add();
       count+1;
       last=to;ha.add();
      end;
      rx=from_to.find_next();
   end;
   rc=hi.next();
end;
ha.clear();_ha.clear();
end;
stop;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Sat, 17 Mar 2018 12:39:43 GMT</pubDate>
    <dc:creator>Ksharp</dc:creator>
    <dc:date>2018-03-17T12:39:43Z</dc:date>
    <item>
      <title>Do loop until end?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-loop-until-end/m-p/446426#M111999</link>
      <description>Hello all,&lt;BR /&gt;&lt;BR /&gt;I am really struggling with this one. I’m not sure if a macro would be the best option. Here is my issue.&lt;BR /&gt;&lt;BR /&gt;I have 1 table with two columns. Parent and component. I am trying to write a program that does the following.&lt;BR /&gt;&lt;BR /&gt;Beginning table&lt;BR /&gt;Parent component&lt;BR /&gt;1 2&lt;BR /&gt;1 3&lt;BR /&gt;1 4&lt;BR /&gt;2. 5&lt;BR /&gt;2. 6&lt;BR /&gt;6. 7&lt;BR /&gt;&lt;BR /&gt;Into this&lt;BR /&gt;Parent component&lt;BR /&gt;1. 2&lt;BR /&gt;1. 3&lt;BR /&gt;1. 4&lt;BR /&gt;1. 5&lt;BR /&gt;1. 6&lt;BR /&gt;1. 7&lt;BR /&gt;&lt;BR /&gt;Basically if a component is in the parent column (like 2) I want to pull any components of 2.&lt;BR /&gt;&lt;BR /&gt;The part that I am really struggling with is getting this to loop. I need sas to look for 6 in the parent column since it was a component of 2 and continue until there are no matches.&lt;BR /&gt;&lt;BR /&gt;This table has about 1.2 million records. Some parents have 30 components and some have much less.&lt;BR /&gt;&lt;BR /&gt;Any help is appreciated. I hope I explained this well enough.</description>
      <pubDate>Sat, 17 Mar 2018 10:10:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-loop-until-end/m-p/446426#M111999</guid>
      <dc:creator>Minutemaid23</dc:creator>
      <dc:date>2018-03-17T10:10:43Z</dc:date>
    </item>
    <item>
      <title>Re: Do loop until end?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-loop-until-end/m-p/446428#M112000</link>
      <description>I don't think that a macro would help here, since I believe that dynamic syntax will soulve the problem.&lt;BR /&gt;I had quite a similar task a couple of years back, and I used a format based on the table (source value component, formatted value patent).&lt;BR /&gt;And the a do while/until loop where you apply the format until you reach the top node (that is when you get the "other" value returned).&lt;BR /&gt;&lt;BR /&gt;I believe you could solve this with a hash table in a similar way, but that is unfortunately not a part of my arsenal. &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/18408"&gt;@Ksharp&lt;/a&gt; to the rescue? &lt;span class="lia-unicode-emoji" title=":winking_face:"&gt;😉&lt;/span&gt;</description>
      <pubDate>Sat, 17 Mar 2018 11:58:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-loop-until-end/m-p/446428#M112000</guid>
      <dc:creator>LinusH</dc:creator>
      <dc:date>2018-03-17T11:58:19Z</dc:date>
    </item>
    <item>
      <title>Re: Do loop until end?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-loop-until-end/m-p/446435#M112002</link>
      <description>&lt;P&gt;Thanks&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13674"&gt;@LinusH&lt;/a&gt;&amp;nbsp;rooting for me.&lt;/P&gt;
&lt;P&gt;The best choice is using SAS/OR if you have it,&amp;nbsp;@RobPratt might give you some code.&lt;/P&gt;
&lt;P&gt;Or check SAS/OR documentation ,I remembered there is already an example about Connection Component.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data have;
infile cards ;
input from $  to $ ;
cards;
1     2
1     3
4     5
5     2
9     4
6     7
8     7
;
run;
data full;
  set have end=last;
  if _n_ eq 1 then do;
   declare hash h();
    h.definekey('node');
     h.definedata('node');
     h.definedone();
  end;
  output;
  node=from; h.replace();
  from=to; to=node;
  output;
  node=from; h.replace();
  if last then h.output(dataset:'node');
  drop node;
run;


data want(keep=node household);
declare hash ha(ordered:'a');
declare hiter hi('ha');
ha.definekey('count');
ha.definedata('last');
ha.definedone();
declare hash _ha(hashexp: 20);
_ha.definekey('key');
_ha.definedone();

if 0 then set full;
declare hash from_to(dataset:'full(where=(from is not missing and to is not missing))',hashexp:20,multidata:'y');
 from_to.definekey('from');
 from_to.definedata('to');
 from_to.definedone();

if 0 then set node;
declare hash no(dataset:'node');
declare hiter hi_no('no');
 no.definekey('node');
 no.definedata('node');
 no.definedone();
 

do while(hi_no.next()=0);
 household+1; output;
 count=1;
 key=node;_ha.add();
 last=node;ha.add();
 rc=hi.first();
 do while(rc=0);
   from=last;rx=from_to.find();
   do while(rx=0);
     key=to;ry=_ha.check();
      if ry ne 0 then do;
       node=to;output;rr=no.remove(key:node);
       key=to;_ha.add();
       count+1;
       last=to;ha.add();
      end;
      rx=from_to.find_next();
   end;
   rc=hi.next();
end;
ha.clear();_ha.clear();
end;
stop;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 17 Mar 2018 12:39:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-loop-until-end/m-p/446435#M112002</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2018-03-17T12:39:43Z</dc:date>
    </item>
    <item>
      <title>Re: Do loop until end?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-loop-until-end/m-p/446437#M112003</link>
      <description>&lt;P&gt;This works . The question will be how this performs over your 1.2 million records, and if there is a more efficient way.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input parent component;
datalines;
1 2
1 3
1 4
2. 5
2. 6
6. 7
;


%macro loop;
%do %until (&amp;amp;count=0);

	proc sql;
	create table have as
	select   coalesce(b.parent,a.parent) as parent , coalesce(a.component,b.component) as component
	from have a  left join have b on b.component=a.parent;
	run;
	* if there are no components in parent then done;
	proc sql ;
	select count(*) into :count
	from have a   join have b on b.component=a.parent;
	run;

%end;
%mend;
%loop;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 17 Mar 2018 12:46:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-loop-until-end/m-p/446437#M112003</guid>
      <dc:creator>JohnHoughton</dc:creator>
      <dc:date>2018-03-17T12:46:54Z</dc:date>
    </item>
    <item>
      <title>Re: Do loop until end?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-loop-until-end/m-p/446463#M112015</link>
      <description>&lt;P&gt;Only a real world test will tell, but I would be REALLY surprised if a loop of SQL joins would beat a hash table look-up, especially if the depth &amp;gt; 3 levels.&lt;/P&gt;</description>
      <pubDate>Sat, 17 Mar 2018 17:21:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-loop-until-end/m-p/446463#M112015</guid>
      <dc:creator>LinusH</dc:creator>
      <dc:date>2018-03-17T17:21:49Z</dc:date>
    </item>
    <item>
      <title>Re: Do loop until end?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-loop-until-end/m-p/446467#M112016</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/18408"&gt;@Ksharp&lt;/a&gt;&amp;nbsp;. It may be just when I run it, but the hash table solution doesn't match the output described in the original post with&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/199633"&gt;@Minutemaid23&lt;/a&gt;'s data.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 17 Mar 2018 17:56:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-loop-until-end/m-p/446467#M112016</guid>
      <dc:creator>JohnHoughton</dc:creator>
      <dc:date>2018-03-17T17:56:12Z</dc:date>
    </item>
    <item>
      <title>Re: Do loop until end?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-loop-until-end/m-p/446475#M112020</link>
      <description>Thanks so much! I am not able to run until I’m back in the office on Monday. I will provide feedback as soon as possible.</description>
      <pubDate>Sat, 17 Mar 2018 19:06:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-loop-until-end/m-p/446475#M112020</guid>
      <dc:creator>Minutemaid23</dc:creator>
      <dc:date>2018-03-17T19:06:45Z</dc:date>
    </item>
    <item>
      <title>Re: Do loop until end?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-loop-until-end/m-p/446488#M112025</link>
      <description>&lt;P&gt;&lt;SPAN&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13674"&gt;@LinusH&lt;/a&gt;&amp;nbsp;wrote&lt;/SPAN&gt;&lt;/P&gt;&lt;BLOCKQUOTE&gt;&lt;P&gt;&lt;SPAN&gt;Only a real world test will tell, but I would be REALLY surprised if a loop of SQL joins would beat a hash table look-up, especially if the depth &amp;gt; 3 levels&lt;/SPAN&gt;&lt;/P&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;&lt;SPAN&gt;Code below generates a test data set with a million observations . The depth depends on the probabilities used in the select statement. With the numbers below I consistently get 6 levels.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Using this as input data , the SQL loop runs in a few seconds. I can't give a precise benchmark as I an running on SAS On Demand.&lt;/SPAN&gt;&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 have;
parent=1;component=2;output;
do i=1 to 1000000;
	c=rand("uniform");
	select;
		when (c&amp;lt;0.05) do;parent=component+1; component=parent+1;end; *new family;
		when (c&amp;lt;0.8) do;parent=parent+1; component=component+1;end; *new generation, same family;
		otherwise component=component+1;* new child, same parent;
	end;
	output;
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 17 Mar 2018 21:32:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-loop-until-end/m-p/446488#M112025</guid>
      <dc:creator>JohnHoughton</dc:creator>
      <dc:date>2018-03-17T21:32:09Z</dc:date>
    </item>
    <item>
      <title>Re: Do loop until end?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-loop-until-end/m-p/447873#M112585</link>
      <description>&lt;P&gt;Well, after a lengthy battle with my IT department i discovered that we do not have a SAS metadata server that i can connect to with JMP. Is there anyway to run macro's in SAS JMP? I haven't found a way with my research so far, and i have been looking everywhere.&lt;/P&gt;</description>
      <pubDate>Thu, 22 Mar 2018 18:14:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-loop-until-end/m-p/447873#M112585</guid>
      <dc:creator>Minutemaid23</dc:creator>
      <dc:date>2018-03-22T18:14:11Z</dc:date>
    </item>
  </channel>
</rss>

