<?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: Collapsing multiple equalities into one line in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Collapsing-multiple-equalities-into-one-line/m-p/280633#M269761</link>
    <description>&lt;P&gt;It sounds like you want search a tree and find out who belong to the same sub-graph .&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;
infile cards ;
input from   to  ;
cards;
1 3 
2 3
1 4
;
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;



proc transpose data=want out=final_want(drop=_name_) prefix=ID;
by household;
var node;
run;

&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Tue, 28 Jun 2016 03:07:18 GMT</pubDate>
    <dc:creator>Ksharp</dc:creator>
    <dc:date>2016-06-28T03:07:18Z</dc:date>
    <item>
      <title>Collapsing multiple equalities into one line</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Collapsing-multiple-equalities-into-one-line/m-p/280472#M269760</link>
      <description>&lt;P&gt;I have a SAS Datasets with multiple equalities: i.e. A=C, B=C etc. and I would like to make concatenate it into one line to A=B=C.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For example, IDs in 1 and 2 represent equality in the following dataset:&lt;/P&gt;&lt;P&gt;ID1, ID2;&lt;/P&gt;&lt;P&gt;1,3;&lt;/P&gt;&lt;P&gt;2,3;&lt;/P&gt;&lt;P&gt;1,4;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I would like to&amp;nbsp;convert this into:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;ID1, ID2, ID3 ID4;&lt;/P&gt;&lt;P&gt;1, 2, 3, 4;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Would there be a good algorithm to do this?&lt;/P&gt;</description>
      <pubDate>Mon, 27 Jun 2016 16:14:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Collapsing-multiple-equalities-into-one-line/m-p/280472#M269760</guid>
      <dc:creator>kimih0223</dc:creator>
      <dc:date>2016-06-27T16:14:41Z</dc:date>
    </item>
    <item>
      <title>Re: Collapsing multiple equalities into one line</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Collapsing-multiple-equalities-into-one-line/m-p/280633#M269761</link>
      <description>&lt;P&gt;It sounds like you want search a tree and find out who belong to the same sub-graph .&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;
infile cards ;
input from   to  ;
cards;
1 3 
2 3
1 4
;
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;



proc transpose data=want out=final_want(drop=_name_) prefix=ID;
by household;
var node;
run;

&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 28 Jun 2016 03:07:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Collapsing-multiple-equalities-into-one-line/m-p/280633#M269761</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2016-06-28T03:07:18Z</dc:date>
    </item>
  </channel>
</rss>

