Just to add a data step solution.
First, if you really have count=0 through 5 for each and every set, then you don't need data set TEMP (as in the proc optmodel solutions):
data want (keep=set1-set4);
sum0=0;
do set1=0 to 5;
sum1=set1+sum0;
if sum1>5 then continue;
do set2=0 to 5;
sum2=sum1+set2;
if sum2>5 then continue;
do set3=0 to 5;
sum3=sum2+set3;
if sum3>5 then continue;
do set4=0 to 5;
sum4=sum3+set4;
if sum4>5 then continue;
if sum4=5 then output;
end;
end;
end;
end;
run;
As I said, this solution doesn't require data set TEMP. But let's say data set TEMP has some "holes", i.e. not every SET has every COUNT from 0 through 5: Then you need to read TEMP to determine which combinations to skip (the if x{n,setn}=. then continue; statements below):
data want (keep=set1-set4);
array x {4,0:5} _temporary_;
set temp end=end_of_temp;
x{set,count}=1;
if end_of_temp;
sum0=0;
do set1=0 to 5;
if x{1,set1}=. then continue;
sum1=set1+sum0;
if sum1>5 then continue;
do set2=0 to 5;
if x{2,set2}=. then continue;
sum2=sum1+set2;
if sum2>5 then continue;
do set3=0 to 5;
if x{3,set3}=. then continue;
sum3=sum2+set3;
if sum3>5 then continue;
do set4=0 to 5;
if x{4,set4}=. then continue;
sum4=sum3+set4;
if sum4>5 then continue;
if sum4=5 then output;
end;
end;
end;
end;
run;
Conversion to macro code can shorten the above. And it would be more amenable to a larger number of sets (just change the %let nsets=4; statement below), or a change in the count range, or target value:
%macro test;
%let nsets=4;
%let countrange=0:5;
%let target=5;
data want (keep=set1-set&nsets);
array x {&nsets,&countrange} _temporary_;
set temp end=end_of_temp;
x{set,count}=1;
if end_of_temp;
sum0=0;
%do s=1 %to &nsets;
do set&s = lbound(x,2) to hbound(x,2);
if x{&s,set&s}=. then continue;
sum&s=set&s + sum%eval(&s-1);
if sum&s>&target then continue;
%end;
if sum&nsets=&target then output;
%do s=1 %to &nsets;
end;
%end;
run;
%mend test;
%test;
Note
the statement
sum&s=set&s + sum%eval(&s-1);
is converted to
sum1=set1 + sum0;
sum2=set2 + sum1;
etc.
... View more