Greetings,
I'm looking for a methodology to identify "Unique" Networks. Example Below:
1. Let's assume I have the connection between "From" and "To":
From | To |
A | 1 |
B | 2 |
C | 1 |
A | 2 |
D | 3 |
E | 3 |
2. I'd be happy to build a program that identifies that the first 4 lines belongs to Network 1, and the last 2 lines belongs to Network 2. These 2 Networks are disconnected from each other.
From | To | Network |
A | 1 | 1 |
B | 2 | 1 |
C | 1 | 1 |
A | 2 | 1 |
D | 3 | 2 |
E | 3 | 2 |
Thanks!
D
So it appears that sasutils solved it using the subnet.sas macro here: https://github.com/sasutils/macros/blob/master/subnet.sas
So it appears that sasutils solved it using the subnet.sas macro here: https://github.com/sasutils/macros/blob/master/subnet.sas
Interesting. Thanks, @DanielDor, for providing the link. So, that's a PROC SQL approach. Other options include PROC OPTNET (SAS/OR) as recommended in Re: Create clusters from pairs and, requiring only Base SAS, DATA step (hash object) approaches as presented in the Communities Library article How to find all connected components in a graph: the SubGraphs macro by PGStats and the code provided by KSharp in his comment.
/*
You want this ?
If you have SAS/OR, the code could be more compact and less.
*/
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;
Available on demand!
Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Select SAS Training centers are offering in-person courses. View upcoming courses for: