<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: conditional combinations in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/conditional-combinations/m-p/735835#M229233</link>
    <description>&lt;P&gt;Just to add a data step solution.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;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):&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want (keep=set1-set4);
  sum0=0;
  do set1=0 to 5;
    sum1=set1+sum0; 
    if sum1&amp;gt;5 then continue;
    do set2=0 to 5;
      sum2=sum1+set2;
      if sum2&amp;gt;5 then continue;
      do set3=0 to 5;
        sum3=sum2+set3; 
        if sum3&amp;gt;5 then continue;
        do set4=0 to 5;
          sum4=sum3+set4;  
          if sum4&amp;gt;5 then continue;
          if sum4=5 then output;
        end;
      end;
    end;
  end;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;As I said, this solution doesn't require data set TEMP.&amp;nbsp;&amp;nbsp;But let's say data set TEMP has some "holes", i.e. not every SET has every COUNT from 0 through 5:&amp;nbsp; Then you need to read TEMP to determine which combinations to skip (the &lt;EM&gt;&lt;STRONG&gt;if x{n,setn}=. then continue;&lt;/STRONG&gt;&lt;/EM&gt; statements below):&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;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&amp;gt;5 then continue;
    do set2=0 to 5;
      if x{2,set2}=. then continue;
      sum2=sum1+set2;
      if sum2&amp;gt;5 then continue;
      do set3=0 to 5;
        if x{3,set3}=. then continue;
        sum3=sum2+set3; 
        if sum3&amp;gt;5 then continue;
        do set4=0 to 5;
          if x{4,set4}=. then continue;
          sum4=sum3+set4;  
          if sum4&amp;gt;5 then continue;
          if sum4=5 then output;
        end;
      end;
    end;
  end;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Conversion to macro code can shorten the above.&amp;nbsp; And it would be more amenable to a larger number of sets (just change the &lt;EM&gt;&lt;STRONG&gt;%let nsets=4;&lt;/STRONG&gt;&lt;/EM&gt; statement below), or a change in the count range, or target value:&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro test;
  %let nsets=4;
  %let countrange=0:5;
  %let target=5;

  data want (keep=set1-set&amp;amp;nsets);
    array x {&amp;amp;nsets,&amp;amp;countrange} _temporary_;
    set temp end=end_of_temp;
    x{set,count}=1;
    if end_of_temp;

    sum0=0;
    %do s=1 %to &amp;amp;nsets;
      do set&amp;amp;s = lbound(x,2) to hbound(x,2);
        if x{&amp;amp;s,set&amp;amp;s}=. then continue;
        sum&amp;amp;s=set&amp;amp;s + sum%eval(&amp;amp;s-1);
        if sum&amp;amp;s&amp;gt;&amp;amp;target then continue;
    %end;

      if sum&amp;amp;nsets=&amp;amp;target then output;

    %do s=1 %to &amp;amp;nsets;
      end;
    %end;
  run;
%mend test;
%test;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Note&lt;/P&gt;
&lt;P&gt;&amp;nbsp; the statement&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;&amp;nbsp; sum&amp;amp;s=set&amp;amp;s + sum%eval(&amp;amp;s-1);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp; is converted to&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;&amp;nbsp; &amp;nbsp;sum1=set1 + sum0;
&amp;nbsp; &amp;nbsp;sum2=set2 + sum1;
 &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;etc.&lt;/P&gt;</description>
    <pubDate>Wed, 21 Apr 2021 06:51:10 GMT</pubDate>
    <dc:creator>mkeintz</dc:creator>
    <dc:date>2021-04-21T06:51:10Z</dc:date>
    <item>
      <title>conditional combinations</title>
      <link>https://communities.sas.com/t5/SAS-Programming/conditional-combinations/m-p/735290#M229056</link>
      <description>&lt;P&gt;Suppose that you have the following data set:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data temp;
	do set = 1 to 4;
		do count = 0 to 5;
			output;
		end;
	end;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Now, you want to get all possible combinations of sets from this data set such that the sum of the "count" is 5. You have to include each "set" one time.&lt;/P&gt;
&lt;P&gt;For example, I want:&lt;/P&gt;
&lt;TABLE style="border-collapse: collapse; width: 144pt;" border="0" width="192" cellspacing="0" cellpadding="0"&gt;
&lt;TBODY&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD width="64" height="20" class="xl63" style="height: 15.0pt; width: 48pt;"&gt;combination&lt;/TD&gt;
&lt;TD width="64" class="xl63" style="width: 48pt;"&gt;set&lt;/TD&gt;
&lt;TD width="64" class="xl63" style="width: 48pt;"&gt;count&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD height="20" align="right" style="height: 15.0pt;"&gt;1&lt;/TD&gt;
&lt;TD align="right"&gt;1&lt;/TD&gt;
&lt;TD align="right"&gt;5&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD height="20" align="right" style="height: 15.0pt;"&gt;1&lt;/TD&gt;
&lt;TD align="right"&gt;2&lt;/TD&gt;
&lt;TD align="right"&gt;0&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD height="20" align="right" style="height: 15.0pt;"&gt;1&lt;/TD&gt;
&lt;TD align="right"&gt;3&lt;/TD&gt;
&lt;TD align="right"&gt;0&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD height="20" align="right" style="height: 15.0pt;"&gt;1&lt;/TD&gt;
&lt;TD align="right"&gt;4&lt;/TD&gt;
&lt;TD align="right"&gt;0&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD height="20" align="right" style="height: 15.0pt;"&gt;2&lt;/TD&gt;
&lt;TD align="right"&gt;1&lt;/TD&gt;
&lt;TD align="right"&gt;4&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD height="20" align="right" style="height: 15.0pt;"&gt;2&lt;/TD&gt;
&lt;TD align="right"&gt;2&lt;/TD&gt;
&lt;TD align="right"&gt;1&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD height="20" align="right" style="height: 15.0pt;"&gt;2&lt;/TD&gt;
&lt;TD align="right"&gt;3&lt;/TD&gt;
&lt;TD align="right"&gt;0&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD height="20" align="right" style="height: 15.0pt;"&gt;2&lt;/TD&gt;
&lt;TD align="right"&gt;4&lt;/TD&gt;
&lt;TD align="right"&gt;0&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD height="20" align="right" style="height: 15.0pt;"&gt;3&lt;/TD&gt;
&lt;TD align="right"&gt;1&lt;/TD&gt;
&lt;TD align="right"&gt;4&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD height="20" align="right" style="height: 15.0pt;"&gt;3&lt;/TD&gt;
&lt;TD align="right"&gt;2&lt;/TD&gt;
&lt;TD align="right"&gt;0&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD height="20" align="right" style="height: 15.0pt;"&gt;3&lt;/TD&gt;
&lt;TD align="right"&gt;3&lt;/TD&gt;
&lt;TD align="right"&gt;1&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD height="20" align="right" style="height: 15.0pt;"&gt;3&lt;/TD&gt;
&lt;TD align="right"&gt;4&lt;/TD&gt;
&lt;TD align="right"&gt;0&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD height="20" style="height: 15.0pt;"&gt;etc&lt;/TD&gt;
&lt;TD&gt;etc&lt;/TD&gt;
&lt;TD&gt;etc&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Any suggestions on how to accomplish this would be appreciated. TIA!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 19 Apr 2021 15:22:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/conditional-combinations/m-p/735290#M229056</guid>
      <dc:creator>cminard</dc:creator>
      <dc:date>2021-04-19T15:22:29Z</dc:date>
    </item>
    <item>
      <title>Re: conditional combinations</title>
      <link>https://communities.sas.com/t5/SAS-Programming/conditional-combinations/m-p/735294#M229059</link>
      <description>&lt;P&gt;I am not seeing how your "want" complies with " sum of the "count" is 5." . You likely have to go into a bit more detail of exactly what "You have to include each "set" one time." means as well.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you mean that you have other data than your example data set it is quite possible the "sum of count=5" may not occur for "set". So what would be the rule for selecting the one that does not match.&lt;/P&gt;</description>
      <pubDate>Mon, 19 Apr 2021 15:40:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/conditional-combinations/m-p/735294#M229059</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2021-04-19T15:40:59Z</dc:date>
    </item>
    <item>
      <title>Re: conditional combinations</title>
      <link>https://communities.sas.com/t5/SAS-Programming/conditional-combinations/m-p/735303#M229063</link>
      <description>&lt;P&gt;In the example data set I have:&lt;/P&gt;
&lt;P&gt;Set 1: 5+0+0+0=5&lt;/P&gt;
&lt;P&gt;Set 2: 4+1+0+0=5&lt;/P&gt;
&lt;P&gt;Set 3: 4+0+1+0=5&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;No, there is no other data.&lt;/P&gt;</description>
      <pubDate>Mon, 19 Apr 2021 15:56:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/conditional-combinations/m-p/735303#M229063</guid>
      <dc:creator>cminard</dc:creator>
      <dc:date>2021-04-19T15:56:36Z</dc:date>
    </item>
    <item>
      <title>Re: conditional combinations</title>
      <link>https://communities.sas.com/t5/SAS-Programming/conditional-combinations/m-p/735326#M229075</link>
      <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/27112"&gt;@cminard&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can create a Cartesian product (view) with PROC SQL and then transpose the result with a DATA step.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create view _tmp as
select t1.count as a1,
       t2.count as a2,
       t3.count as a3,
       t4.count as a4
from temp(where=(set=1)) t1,
     temp(where=(set=2)) t2,
     temp(where=(set=3)) t3,
     temp(where=(set=4)) t4
having a1+a2+a3+a4=5
order by 1 desc, 2 desc, 3 desc, 4 desc;
quit;

data want(drop=a:);
set _tmp;
array a[*] a:;
combination=_n_;
do set=1 to dim(a);
  count=a[set];
  output;
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 19 Apr 2021 17:32:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/conditional-combinations/m-p/735326#M229075</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2021-04-19T17:32:23Z</dc:date>
    </item>
    <item>
      <title>Re: conditional combinations</title>
      <link>https://communities.sas.com/t5/SAS-Programming/conditional-combinations/m-p/735559#M229145</link>
      <description>&lt;P&gt;It is OR things. Try post it at OR forum and calling&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/1636"&gt;@RobPratt&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;proc optmodel;
num numLists=4;
set LIST{1..numLists};
LIST[1]=0..5;
LIST[2]=0..5;
LIST[3]=0..5;
LIST[4]=0..5;

num target=5;
var x{i in 1..numLIsts,j in LIST[i]} binary;
con oneitemperlist{i in 1..numLists} : sum{j in LIST[i]} x[i,j]=1;
con sumtotarget:sum{i in 1..numLists,j in LIST[i]} j*x[i,j]=target;
solve with clp/findallsolns;
set SUPPORT{s in 1.._NSOL_}={i in 1..numLists,j in LIST[i]:x[i,j].sol[s]&amp;gt;0.5};
put SUPPORT[*];
quit;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;{&amp;lt;1,5&amp;gt;,&amp;lt;2,0&amp;gt;,&amp;lt;3,0&amp;gt;,&amp;lt;4,0&amp;gt;} {&amp;lt;1,4&amp;gt;,&amp;lt;2,0&amp;gt;,&amp;lt;3,0&amp;gt;,&amp;lt;4,1&amp;gt;} {&amp;lt;1,4&amp;gt;,&amp;lt;2,1&amp;gt;,&amp;lt;3,0&amp;gt;,&amp;lt;4,0&amp;gt;} {&amp;lt;1,4&amp;gt;,&amp;lt;2,0&amp;gt;,&amp;lt;3,1&amp;gt;,&amp;lt;4,0&amp;gt;} {&amp;lt;1,3&amp;gt;,&amp;lt;2,1&amp;gt;,&amp;lt;3,0&amp;gt;,&amp;lt;4,1&amp;gt;} {&amp;lt;
 1,3&amp;gt;,&amp;lt;2,0&amp;gt;,&amp;lt;3,1&amp;gt;,&amp;lt;4,1&amp;gt;} {&amp;lt;1,3&amp;gt;,&amp;lt;2,1&amp;gt;,&amp;lt;3,1&amp;gt;,&amp;lt;4,0&amp;gt;} {&amp;lt;1,3&amp;gt;,&amp;lt;2,0&amp;gt;,&amp;lt;3,0&amp;gt;,&amp;lt;4,2&amp;gt;} {&amp;lt;1,3&amp;gt;,&amp;lt;2,2&amp;gt;,&amp;lt;3,0&amp;gt;,&amp;lt;4,0&amp;gt;} {&amp;lt;1,3&amp;gt;,&amp;lt;2,0&amp;gt;,&amp;lt;3,2&amp;gt;,&amp;lt;4,0&amp;gt;} {&amp;lt;1,
 2&amp;gt;,&amp;lt;2,1&amp;gt;,&amp;lt;3,1&amp;gt;,&amp;lt;4,1&amp;gt;} {&amp;lt;1,2&amp;gt;,&amp;lt;2,1&amp;gt;,&amp;lt;3,0&amp;gt;,&amp;lt;4,2&amp;gt;} {&amp;lt;1,2&amp;gt;,&amp;lt;2,0&amp;gt;,&amp;lt;3,1&amp;gt;,&amp;lt;4,2&amp;gt;} {&amp;lt;1,2&amp;gt;,&amp;lt;2,2&amp;gt;,&amp;lt;3,0&amp;gt;,&amp;lt;4,1&amp;gt;} {&amp;lt;1,2&amp;gt;,&amp;lt;2,2&amp;gt;,&amp;lt;3,1&amp;gt;,&amp;lt;4,0&amp;gt;} {&amp;lt;1,2&amp;gt;
 ,&amp;lt;2,0&amp;gt;,&amp;lt;3,2&amp;gt;,&amp;lt;4,1&amp;gt;} {&amp;lt;1,2&amp;gt;,&amp;lt;2,1&amp;gt;,&amp;lt;3,2&amp;gt;,&amp;lt;4,0&amp;gt;} {&amp;lt;1,2&amp;gt;,&amp;lt;2,0&amp;gt;,&amp;lt;3,3&amp;gt;,&amp;lt;4,0&amp;gt;} {&amp;lt;1,2&amp;gt;,&amp;lt;2,3&amp;gt;,&amp;lt;3,0&amp;gt;,&amp;lt;4,0&amp;gt;} {&amp;lt;1,2&amp;gt;,&amp;lt;2,0&amp;gt;,&amp;lt;3,0&amp;gt;,&amp;lt;4,3&amp;gt;} {&amp;lt;1,1&amp;gt;,&amp;lt;
 2,1&amp;gt;,&amp;lt;3,1&amp;gt;,&amp;lt;4,2&amp;gt;} {&amp;lt;1,1&amp;gt;,&amp;lt;2,2&amp;gt;,&amp;lt;3,0&amp;gt;,&amp;lt;4,2&amp;gt;} {&amp;lt;1,1&amp;gt;,&amp;lt;2,2&amp;gt;,&amp;lt;3,1&amp;gt;,&amp;lt;4,1&amp;gt;} {&amp;lt;1,1&amp;gt;,&amp;lt;2,0&amp;gt;,&amp;lt;3,2&amp;gt;,&amp;lt;4,2&amp;gt;} {&amp;lt;1,1&amp;gt;,&amp;lt;2,2&amp;gt;,&amp;lt;3,2&amp;gt;,&amp;lt;4,0&amp;gt;} {&amp;lt;1,1&amp;gt;,&amp;lt;2,
 1&amp;gt;,&amp;lt;3,2&amp;gt;,&amp;lt;4,1&amp;gt;} {&amp;lt;1,1&amp;gt;,&amp;lt;2,0&amp;gt;,&amp;lt;3,3&amp;gt;,&amp;lt;4,1&amp;gt;} {&amp;lt;1,1&amp;gt;,&amp;lt;2,1&amp;gt;,&amp;lt;3,3&amp;gt;,&amp;lt;4,0&amp;gt;} {&amp;lt;1,1&amp;gt;,&amp;lt;2,3&amp;gt;,&amp;lt;3,0&amp;gt;,&amp;lt;4,1&amp;gt;} {&amp;lt;1,1&amp;gt;,&amp;lt;2,3&amp;gt;,&amp;lt;3,1&amp;gt;,&amp;lt;4,0&amp;gt;} {&amp;lt;1,1&amp;gt;,&amp;lt;2,0&amp;gt;
 ,&amp;lt;3,1&amp;gt;,&amp;lt;4,3&amp;gt;} {&amp;lt;1,1&amp;gt;,&amp;lt;2,1&amp;gt;,&amp;lt;3,0&amp;gt;,&amp;lt;4,3&amp;gt;} {&amp;lt;1,1&amp;gt;,&amp;lt;2,0&amp;gt;,&amp;lt;3,4&amp;gt;,&amp;lt;4,0&amp;gt;} {&amp;lt;1,1&amp;gt;,&amp;lt;2,0&amp;gt;,&amp;lt;3,0&amp;gt;,&amp;lt;4,4&amp;gt;} {&amp;lt;1,1&amp;gt;,&amp;lt;2,4&amp;gt;,&amp;lt;3,0&amp;gt;,&amp;lt;4,0&amp;gt;} {&amp;lt;1,0&amp;gt;,&amp;lt;2,2&amp;gt;,&amp;lt;
 3,1&amp;gt;,&amp;lt;4,2&amp;gt;} {&amp;lt;1,0&amp;gt;,&amp;lt;2,1&amp;gt;,&amp;lt;3,2&amp;gt;,&amp;lt;4,2&amp;gt;} {&amp;lt;1,0&amp;gt;,&amp;lt;2,2&amp;gt;,&amp;lt;3,2&amp;gt;,&amp;lt;4,1&amp;gt;} {&amp;lt;1,0&amp;gt;,&amp;lt;2,0&amp;gt;,&amp;lt;3,3&amp;gt;,&amp;lt;4,2&amp;gt;} {&amp;lt;1,0&amp;gt;,&amp;lt;2,1&amp;gt;,&amp;lt;3,3&amp;gt;,&amp;lt;4,1&amp;gt;} {&amp;lt;1,0&amp;gt;,&amp;lt;2,2&amp;gt;,&amp;lt;3,
 3&amp;gt;,&amp;lt;4,0&amp;gt;} {&amp;lt;1,0&amp;gt;,&amp;lt;2,3&amp;gt;,&amp;lt;3,0&amp;gt;,&amp;lt;4,2&amp;gt;} {&amp;lt;1,0&amp;gt;,&amp;lt;2,3&amp;gt;,&amp;lt;3,1&amp;gt;,&amp;lt;4,1&amp;gt;} {&amp;lt;1,0&amp;gt;,&amp;lt;2,3&amp;gt;,&amp;lt;3,2&amp;gt;,&amp;lt;4,0&amp;gt;} {&amp;lt;1,0&amp;gt;,&amp;lt;2,2&amp;gt;,&amp;lt;3,0&amp;gt;,&amp;lt;4,3&amp;gt;} {&amp;lt;1,0&amp;gt;,&amp;lt;2,0&amp;gt;,&amp;lt;3,2&amp;gt;
 ,&amp;lt;4,3&amp;gt;} {&amp;lt;1,0&amp;gt;,&amp;lt;2,1&amp;gt;,&amp;lt;3,1&amp;gt;,&amp;lt;4,3&amp;gt;} {&amp;lt;1,0&amp;gt;,&amp;lt;2,1&amp;gt;,&amp;lt;3,4&amp;gt;,&amp;lt;4,0&amp;gt;} {&amp;lt;1,0&amp;gt;,&amp;lt;2,0&amp;gt;,&amp;lt;3,4&amp;gt;,&amp;lt;4,1&amp;gt;} {&amp;lt;1,0&amp;gt;,&amp;lt;2,0&amp;gt;,&amp;lt;3,1&amp;gt;,&amp;lt;4,4&amp;gt;} {&amp;lt;1,0&amp;gt;,&amp;lt;2,1&amp;gt;,&amp;lt;3,0&amp;gt;,&amp;lt;
 4,4&amp;gt;} {&amp;lt;1,0&amp;gt;,&amp;lt;2,4&amp;gt;,&amp;lt;3,1&amp;gt;,&amp;lt;4,0&amp;gt;} {&amp;lt;1,0&amp;gt;,&amp;lt;2,4&amp;gt;,&amp;lt;3,0&amp;gt;,&amp;lt;4,1&amp;gt;} {&amp;lt;1,0&amp;gt;,&amp;lt;2,0&amp;gt;,&amp;lt;3,0&amp;gt;,&amp;lt;4,5&amp;gt;} {&amp;lt;1,0&amp;gt;,&amp;lt;2,0&amp;gt;,&amp;lt;3,5&amp;gt;,&amp;lt;4,0&amp;gt;} {&amp;lt;1,0&amp;gt;,&amp;lt;2,5&amp;gt;,&amp;lt;3,0&amp;gt;,&amp;lt;4,
 0&amp;gt;}&lt;/PRE&gt;
&lt;PRE&gt;解汇总
Solver	CLP
变量选择	MINR
Objective Function	(0)
Solution Status	All Solutions Found
Objective Value	0
 	 
&lt;FONT color="#FF0000"&gt;&lt;STRONG&gt;Solutions Found	56&lt;/STRONG&gt;&lt;/FONT&gt;
Presolve Time	0.00
Solution Time	0.01&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 20 Apr 2021 14:04:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/conditional-combinations/m-p/735559#M229145</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2021-04-20T14:04:46Z</dc:date>
    </item>
    <item>
      <title>Re: conditional combinations</title>
      <link>https://communities.sas.com/t5/SAS-Programming/conditional-combinations/m-p/735774#M229205</link>
      <description>&lt;P&gt;That works, or you can use 4 integer variables and 1 linear constraint:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc optmodel;
   num numLists = 4;
   num target = 5;
   var X {1..numLists} &amp;gt;= 0 &amp;lt;= target integer;
   con SumToTarget: sum {i in 1..numLists} X[i] = target;
   solve with clp / findallsolns;
   create data want from [combination set]={s in 1.._NSOL_, i in 1..numLists} count=X[i].sol[s];
quit;
proc sort data=want;
   by combination set;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 20 Apr 2021 20:47:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/conditional-combinations/m-p/735774#M229205</guid>
      <dc:creator>RobPratt</dc:creator>
      <dc:date>2021-04-20T20:47:00Z</dc:date>
    </item>
    <item>
      <title>Re: conditional combinations</title>
      <link>https://communities.sas.com/t5/SAS-Programming/conditional-combinations/m-p/735835#M229233</link>
      <description>&lt;P&gt;Just to add a data step solution.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;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):&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want (keep=set1-set4);
  sum0=0;
  do set1=0 to 5;
    sum1=set1+sum0; 
    if sum1&amp;gt;5 then continue;
    do set2=0 to 5;
      sum2=sum1+set2;
      if sum2&amp;gt;5 then continue;
      do set3=0 to 5;
        sum3=sum2+set3; 
        if sum3&amp;gt;5 then continue;
        do set4=0 to 5;
          sum4=sum3+set4;  
          if sum4&amp;gt;5 then continue;
          if sum4=5 then output;
        end;
      end;
    end;
  end;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;As I said, this solution doesn't require data set TEMP.&amp;nbsp;&amp;nbsp;But let's say data set TEMP has some "holes", i.e. not every SET has every COUNT from 0 through 5:&amp;nbsp; Then you need to read TEMP to determine which combinations to skip (the &lt;EM&gt;&lt;STRONG&gt;if x{n,setn}=. then continue;&lt;/STRONG&gt;&lt;/EM&gt; statements below):&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;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&amp;gt;5 then continue;
    do set2=0 to 5;
      if x{2,set2}=. then continue;
      sum2=sum1+set2;
      if sum2&amp;gt;5 then continue;
      do set3=0 to 5;
        if x{3,set3}=. then continue;
        sum3=sum2+set3; 
        if sum3&amp;gt;5 then continue;
        do set4=0 to 5;
          if x{4,set4}=. then continue;
          sum4=sum3+set4;  
          if sum4&amp;gt;5 then continue;
          if sum4=5 then output;
        end;
      end;
    end;
  end;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Conversion to macro code can shorten the above.&amp;nbsp; And it would be more amenable to a larger number of sets (just change the &lt;EM&gt;&lt;STRONG&gt;%let nsets=4;&lt;/STRONG&gt;&lt;/EM&gt; statement below), or a change in the count range, or target value:&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro test;
  %let nsets=4;
  %let countrange=0:5;
  %let target=5;

  data want (keep=set1-set&amp;amp;nsets);
    array x {&amp;amp;nsets,&amp;amp;countrange} _temporary_;
    set temp end=end_of_temp;
    x{set,count}=1;
    if end_of_temp;

    sum0=0;
    %do s=1 %to &amp;amp;nsets;
      do set&amp;amp;s = lbound(x,2) to hbound(x,2);
        if x{&amp;amp;s,set&amp;amp;s}=. then continue;
        sum&amp;amp;s=set&amp;amp;s + sum%eval(&amp;amp;s-1);
        if sum&amp;amp;s&amp;gt;&amp;amp;target then continue;
    %end;

      if sum&amp;amp;nsets=&amp;amp;target then output;

    %do s=1 %to &amp;amp;nsets;
      end;
    %end;
  run;
%mend test;
%test;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Note&lt;/P&gt;
&lt;P&gt;&amp;nbsp; the statement&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;&amp;nbsp; sum&amp;amp;s=set&amp;amp;s + sum%eval(&amp;amp;s-1);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp; is converted to&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;&amp;nbsp; &amp;nbsp;sum1=set1 + sum0;
&amp;nbsp; &amp;nbsp;sum2=set2 + sum1;
 &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;etc.&lt;/P&gt;</description>
      <pubDate>Wed, 21 Apr 2021 06:51:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/conditional-combinations/m-p/735835#M229233</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2021-04-21T06:51:10Z</dc:date>
    </item>
  </channel>
</rss>

