BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
DanielDor
Obsidian | Level 7

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":

FromTo
A1
B2
C1
A2
D3
E3

 

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.

 

FromToNetwork
A11
B21
C11
A21
D32
E32

 

Thanks!

 

D

1 ACCEPTED SOLUTION

Accepted Solutions
DanielDor
Obsidian | Level 7

So it appears that sasutils solved it using the subnet.sas macro here: https://github.com/sasutils/macros/blob/master/subnet.sas

 

 

View solution in original post

3 REPLIES 3
DanielDor
Obsidian | Level 7

So it appears that sasutils solved it using the subnet.sas macro here: https://github.com/sasutils/macros/blob/master/subnet.sas

 

 

FreelanceReinh
Jade | Level 19

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.

Ksharp
Super User
/*
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;

sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


Register now!

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 587 views
  • 2 likes
  • 3 in conversation