Here is an approach that uses Viya's OPTNETWORK to solve it. This uses the same algorithms that @RobPratt shows through the OPTMODEL interface. It finds the connected components and then runs cycle detection by group (where group = connected component). For large scale this approach would scale nicely as it does all the heavy work on the server (either shared or distributed memory).
/* Find weakly connected components (ignore singletons) */
proc optnetwork
links = sascas1.have(where=(col2 ne ''))
outLinks = sascas1.ConCompLinks;
connectedComponents;
linksVar from=col1 to=col2;
run;
/* Determine if the component is directed acyclic. */
proc optnetwork
direction = directed
links = sascas1.ConCompLinks;
linksVar from=col1 to=col2;
cycle;
displayout
SolutionSummary = SolutionSummary;
by concomp;
run;
/* For each component that is not acyclic, its nodes are NotOk (and append singletons as Ok). */
proc sql;
create table tmp as
select a.*, b.numCycles as hasCycle
from sascas1.ConCompLinks as a left join sascas1.SolutionSummary as b
on a.concomp = b.concomp;
quit;
data want;
set tmp sascas1.have(where=(col2 eq ''));
if(hasCycle > 0) then flag="NotOk"; else flag="Ok";
run;
proc print data=want; run;
... View more