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

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.

PGStats
Opal | Level 21

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.

PG
tom12122
Obsidian | Level 7

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?

PGStats
Opal | Level 21

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

PG
PGStats
Opal | Level 21

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

PG
Ksharp
Super User

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

shivas
Pyrite | Level 9

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=x[i];

    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

    sas-innovate-2024.png

    Don't miss out on SAS Innovate - Register now for the FREE Livestream!

    Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

     

    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.

    Click image to register for webinarClick image to register for webinar

    Classroom Training Available!

    Select SAS Training centers are offering in-person courses. View upcoming courses for:

    View all other training opportunities.

    Discussion stats
    • 21 replies
    • 5571 views
    • 0 likes
    • 8 in conversation