First a clarification about BY-group processing for other users who do have SAS Viya: the BY statement for PROC OPTNETWORK does not apply when there are multiple input tables, as in this use case. The groupBy parameter for runOptmodel applies for any number of tables.
Regarding the objective being a sum of groups, when you have a bunch of disjoint problems like this, it is mathematically equivalent to solve them individually or together as one big problem. But it is usually computationally better (both time and memory) to solve them individually, and the decomp=(method=concomp), algorithm=ns, and solve with network approaches all automatically split the big problem into one problem per group. All the approaches I have illustrated yield the same optimal objective value of 9069.4.
In SAS 9.4, the most memory-efficient approach is to use PROC OPTNET (which for minimum-cost network flow problems calls the same network simplex algorithm):
/* node data */
proc sql;
create table lib.group_subject as
select distinct group, subject, cat(group,'_',subject) as node, 1 as weight
from lib.have;
quit;
data lib.group_area;
set lib.have_constraints;
node = cat(group,'_',area);
weight = -n;
run;
data NodeSetIn;
set lib.group_subject lib.group_area;
run;
/* link data */
data LinkSetIn;
set lib.have;
from = cat(group,'_',subject);
to = cat(group,'_',area);
run;
proc optnet direction=directed nodes=NodeSetIn links=LinkSetIn out_links=out_links;
links_var weight=distance;
mincostflow;
run;
But for large enough data, you will run out of memory. So I recommend splitting your 10k groups into smaller batches and calling PROC OPTNET once per batch.
... View more