It is helpful with 10 or 15 variables - then the full number of combinations would be 10^10 and 10^15 respectively and with given sum the number decreases dramatically.
Actually, the number of combinations to consider is 2^9 or 2^14. Large numbers indeed, but feasable. There is a way to generate unique combinations directly, I'm quite sure. But that's a lot of intricate programming.
hm..
variable 1 - 10 combinations
variable 2 - 10 combinations
...
variable n - 10 combinations
then alltogether 10*10 * .... = 10^n without sum condition ? Am I wrong?
Look at it this way, any solution to the problem, say 20+60+20=100, has the form 10+10,10+10+10+10+10+10,10+10. Between each 10 there is either a + or a comma; there are 9 such positions, thus 2^9 possible solutions. You may retain all these 512 solutions, or decide to eliminate permutations such as (60,20,20), (20,60,20) and (20,20,60), in which case you are left with 42 unique solutions (see my first post).
PG
A slight rearrangement of my first proposal will give you all the 512 permutations at once :
data comb(keep=j n:);
array n{*} n1-n10;
format n: 3.;
length c $9 b $1;
do k = 0 to 2**(10-2)-1;
c = put(k, binary9.);
do b = "0", "1";
do i = 1 to 10; n{i} = 0; end;
j = 1;
do i = 1 to 9;
n{j} + 1;
if char(c, i) = b then j + 1;
end;
n{j} + 1;
do i = 1 to j; n{i} = 10*n{i}; end;
do i = j+1 to 10; n{i} = .; end;
output;
end;
end;
run;
proc sort data=comb; by j n:; run;
proc print data=comb; run;
PG
The simplest way is using Array+ do loop .
data have; input v1 v2 v3 ; cards; 10 80 10 10 90 10 30 30 40 ; run; data want; set have; array _v{*} v1 v2 v3; do i1=1 to dim(_v); do i2=i1+1 to dim(_v); do i3=i2+1 to dim(_v); if sum(_v{i1},_v{i2},_v{i3}) eq 100 then output; end; end; end; run;
Ksharp
Hi,
Try this
/*Example from SAS documentation*/
data test1;
array x[11] (0 10 20 30 40 50 60 70 80 90 100);
array c[3] ;
array i[3];
n=dim(x);
k=dim(i);
/* i[1]=0;*/
ncomb=comb(n,k);
do j=1 to ncomb+1;
call allcombi(n, k, of i
do h=1 to k;
c
end;
if sum(of c1-c3)=100 then output;
end;
keep c1 c2 c3;
run;
or...
data test2;
do i =0 to 100 by 10;
do j=0 to 100 by 10;
do k=0 to 100 by 10;
if sum(i,j,k) =100 then output;
end;end;end;
run;
Thanks,
Shiva
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.