<?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: Identifying combinations of variable that add up to a set amount in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Identifying-combinations-of-variable-that-add-up-to-a-set-amount/m-p/609722#M177516</link>
    <description>&lt;P&gt;Thank you &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/76464"&gt;@s_lassen&lt;/a&gt; and &lt;A href="https://communities.sas.com/t5/user/viewprofilepage/user-id/32733" target="_blank" rel="noopener"&gt;@FreelanceReinhard&lt;/A&gt;, and apologies for not expressing my thanks earlier, I got a bit caught up in other matters.&amp;nbsp;I've tried to resolve my issue using this solution but it appears to be falling down somewhere. Although I now know the figures that make up the total (obtained through other means but would still be great for future reference to get this sorted) are 75.9 and 106.97, the code does not identify this particular combination. In fact, it does not identify any combinations at all. I have tried amending the code using round(,0.1) and this does return some combinations but not the exact one.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I've tried some more testing putting the figures in as datalines to reduce the number of figures but it still isn't working. Would you be able to suggest where I'm going wrong? I'm wondering if it is something to do with the decimal places but have tried multiplying all the figures by 100 to turn them all into integers but am still not arriving at the solution.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This is what I'm using&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="1"&gt;&lt;STRONG&gt;data&lt;/STRONG&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="1"&gt; amounts;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="1"&gt;input&lt;/FONT&gt;&lt;FONT face="Courier New" size="1"&gt; amount;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="1"&gt;datalines&lt;/FONT&gt;&lt;FONT face="Courier New" size="1"&gt;;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;75.9&lt;/P&gt;&lt;P&gt;106.97&lt;/P&gt;&lt;P&gt;100&lt;/P&gt;&lt;P&gt;255&lt;/P&gt;&lt;P&gt;643.59&lt;/P&gt;&lt;P&gt;251.25&lt;/P&gt;&lt;P&gt;;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="1"&gt;&lt;STRONG&gt;run&lt;/STRONG&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="1"&gt;;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="1"&gt;%let&lt;/FONT&gt;&lt;FONT face="Courier New" size="1"&gt; outstanding = 187.87;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;　&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="1"&gt;&lt;STRONG&gt;data&lt;/STRONG&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="1"&gt; want;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="1"&gt;array&lt;/FONT&gt;&lt;FONT face="Courier New" size="1"&gt; payments (&lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT color="#008080" face="Courier New" size="1"&gt;1000&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New" size="1"&gt;) &lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT color="#008080" face="Courier New" size="1"&gt;8&lt;/FONT&gt;&lt;/STRONG&gt; &lt;FONT color="#0000ff" face="Courier New" size="1"&gt;_temporary_&lt;/FONT&gt;&lt;FONT face="Courier New" size="1"&gt;;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="1"&gt;do&lt;/FONT&gt;&lt;FONT face="Courier New" size="1"&gt; _N_= &lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT color="#008080" face="Courier New" size="1"&gt;1&lt;/FONT&gt;&lt;/STRONG&gt; &lt;FONT color="#0000ff" face="Courier New" size="1"&gt;to&lt;/FONT&gt;&lt;FONT face="Courier New" size="1"&gt; nobs;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="1"&gt;set&lt;/FONT&gt;&lt;FONT face="Courier New" size="1"&gt; amounts &lt;/FONT&gt;&lt;FONT color="#0000ff" face="Courier New" size="1"&gt;nobs&lt;/FONT&gt;&lt;FONT face="Courier New" size="1"&gt; = nobs;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;payments(_N_) = amount;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="1"&gt;end&lt;/FONT&gt;&lt;FONT face="Courier New" size="1"&gt;;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="1"&gt;array&lt;/FONT&gt;&lt;FONT face="Courier New" size="1"&gt; idx &lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT color="#008080" face="Courier New" size="1"&gt;8&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New" size="1"&gt; idx1 - idx6;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="1"&gt;&lt;STRONG&gt;%macro&lt;/STRONG&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="1"&gt; &lt;STRONG&gt;&lt;I&gt;check&lt;/I&gt;&lt;/STRONG&gt;;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="1"&gt;%local&lt;/FONT&gt;&lt;FONT face="Courier New" size="1"&gt; comb;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="1"&gt;%do&lt;/FONT&gt;&lt;FONT face="Courier New" size="1"&gt; comb = &lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT color="#008080" face="Courier New" size="1"&gt;2&lt;/FONT&gt;&lt;/STRONG&gt; &lt;FONT color="#0000ff" face="Courier New" size="1"&gt;%to&lt;/FONT&gt; &lt;STRONG&gt;&lt;FONT color="#008080" face="Courier New" size="1"&gt;6&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New" size="1"&gt;;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="1"&gt;call missing(of idx1 - idx&amp;amp;&lt;/FONT&gt;&lt;FONT color="#008080" face="Courier New" size="1"&gt;comb.&lt;/FONT&gt;&lt;FONT face="Courier New" size="1"&gt;);&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="1"&gt;ncomb = comb(nobs, &amp;amp;&lt;/FONT&gt;&lt;FONT color="#008080" face="Courier New" size="1"&gt;comb.&lt;/FONT&gt;&lt;FONT face="Courier New" size="1"&gt;);&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="1"&gt;do i = &lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT color="#008080" face="Courier New" size="1"&gt;1&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New" size="1"&gt; to ncomb;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="1"&gt;rc = lexcombi(nobs, &amp;amp;&lt;/FONT&gt;&lt;FONT color="#008080" face="Courier New" size="1"&gt;comb.&lt;/FONT&gt;&lt;FONT face="Courier New" size="1"&gt;, of idx1 - idx&amp;amp;&lt;/FONT&gt;&lt;FONT color="#008080" face="Courier New" size="1"&gt;comb.&lt;/FONT&gt;&lt;FONT face="Courier New" size="1"&gt;);&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="1"&gt;sum = &lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT color="#008080" face="Courier New" size="1"&gt;0&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New" size="1"&gt;;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="1"&gt;do _N_ = &lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT color="#008080" face="Courier New" size="1"&gt;1&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New" size="1"&gt; to &amp;amp;&lt;/FONT&gt;&lt;FONT color="#008080" face="Courier New" size="1"&gt;comb.&lt;/FONT&gt;&lt;FONT face="Courier New" size="1"&gt;;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;sum + payments(idx(_N_));&lt;/P&gt;&lt;P&gt;end;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="1"&gt;if round(sum,&lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT color="#008080" face="Courier New" size="1"&gt;0.01&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New" size="1"&gt;) &amp;gt; &amp;amp;&lt;/FONT&gt;&lt;FONT color="#008080" face="Courier New" size="1"&gt;outstanding.&lt;/FONT&gt;&lt;FONT face="Courier New" size="1"&gt; then leave;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="1"&gt;if round(sum,&lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT color="#008080" face="Courier New" size="1"&gt;0.01&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New" size="1"&gt;) = &amp;amp;&lt;/FONT&gt;&lt;FONT color="#008080" face="Courier New" size="1"&gt;outstanding.&lt;/FONT&gt;&lt;FONT face="Courier New" size="1"&gt; then output;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;end;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="1"&gt;%end&lt;/FONT&gt;&lt;FONT face="Courier New" size="1"&gt;;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="1"&gt;&lt;STRONG&gt;%mend&lt;/STRONG&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="1"&gt; check;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%&lt;STRONG&gt;&lt;I&gt;check&lt;/I&gt;&lt;/STRONG&gt;;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="1"&gt;keep&lt;/FONT&gt;&lt;FONT face="Courier New" size="1"&gt; idx1 - idx6;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="1"&gt;&lt;STRONG&gt;run&lt;/STRONG&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="1"&gt;; &lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;As always, any advice will be gratefully received.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Rob&lt;/P&gt;</description>
    <pubDate>Thu, 05 Dec 2019 16:07:48 GMT</pubDate>
    <dc:creator>robulon</dc:creator>
    <dc:date>2019-12-05T16:07:48Z</dc:date>
    <item>
      <title>Identifying combinations of variable that add up to a set amount</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Identifying-combinations-of-variable-that-add-up-to-a-set-amount/m-p/607297#M176498</link>
      <description>Hi, I'm hoping for some suggestions on how to go about solving a problem. I have a total of 253 payments due that should add up to £14,843.87. The total payment received is £14,661.00 but I am unable to tell how many individual payments make up this amount. What I am hoping to do is run some code that will identify the possible combinations of the due payments that add up to the missing £182.87. I know the missing amount must be made up of more than one payment (as the biggest single payment is £115.40), and cannot be made up of more than six payments (as the lowest seven payments add up to £189.89). Is there a way I can run some code that will look at the different possible combinations of the payment amounts and tell me which one(s) add up to £182.87. As always, really grateful for any advice on how to go about solving this. Thanks, Rob</description>
      <pubDate>Tue, 26 Nov 2019 12:09:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Identifying-combinations-of-variable-that-add-up-to-a-set-amount/m-p/607297#M176498</guid>
      <dc:creator>robulon</dc:creator>
      <dc:date>2019-11-26T12:09:29Z</dc:date>
    </item>
    <item>
      <title>Re: Identifying combinations of variable that add up to a set amount</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Identifying-combinations-of-variable-that-add-up-to-a-set-amount/m-p/607330#M176515</link>
      <description>&lt;P&gt;You could try something like this, here with example data:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  do id=1 to 253;
    payment=mod(id,19)+1;
    output;
    end;
run;

proc sort data=have;
  by payment;
run;

%let wanted=43; /* the sum that you are looking for */
data want;
  array payments (1000) 8 _temporary_;
  do _N_=1 to nobs;
    set have nobs=nobs;
      payments(_N_)=payment;
      end;
  array idx 8 idx1-idx6;
  %macro check;
  %local comb;
  %do comb=2 %to 6;
  call missing(of idx1-idx&amp;amp;comb);
  ncomb=comb(nobs,&amp;amp;comb);
  do i=1 to ncomb;
    rc=lexcombi(nobs,&amp;amp;comb,of idx1-idx&amp;amp;comb);
    sum=0;
    do _N_=1 to &amp;amp;comb;
      sum+payments(idx(_N_));
      end;
    if round(sum,0.01)&amp;gt;&amp;amp;wanted then leave;
    if round(sum,0.01)=&amp;amp;wanted then output;
    end;
  %end;
  %mend;
  %check;&lt;BR /&gt;  keep idx1-idx6;
run;   

&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Sorting is necessary to break out of the loop as soon as values get too large, otherwise it will take forever. (I just changed the sort to be by payment, not ID, as&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/32733"&gt;@FreelanceReinh&lt;/a&gt;&amp;nbsp;reminded me, the other was a slip of fingers on the keyboard).&lt;/P&gt;
&lt;P&gt;The variables idx1-idx6 should contain the observation numbers (in the sorted data) of observations that add up to &amp;amp;wanted.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The ROUND function calls are not necessary in this example, as the test data has no decimals. But with decimals, you may have to round in order to catch all the possible fits.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The reason for the macro construct is only that if you have too many indices in the LEXCOMBI call, they will all be set to missing, so it cannot be done in a data step loop.&lt;/P&gt;</description>
      <pubDate>Wed, 27 Nov 2019 10:21:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Identifying-combinations-of-variable-that-add-up-to-a-set-amount/m-p/607330#M176515</guid>
      <dc:creator>s_lassen</dc:creator>
      <dc:date>2019-11-27T10:21:07Z</dc:date>
    </item>
    <item>
      <title>Re: Identifying combinations of variable that add up to a set amount</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Identifying-combinations-of-variable-that-add-up-to-a-set-amount/m-p/607331#M176516</link>
      <description>&lt;P&gt;Please post test-data in usable form. And have a look at &lt;A href="https://communities.sas.com/t5/SAS-Programming/How-do-I-Find-the-Combinations-that-Equal-a-Given-Sum/td-p/530839" target="_blank"&gt;https://communities.sas.com/t5/SAS-Programming/How-do-I-Find-the-Combinations-that-Equal-a-Given-Sum/td-p/530839&lt;/A&gt;.&lt;/P&gt;</description>
      <pubDate>Tue, 26 Nov 2019 13:27:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Identifying-combinations-of-variable-that-add-up-to-a-set-amount/m-p/607331#M176516</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2019-11-26T13:27:51Z</dc:date>
    </item>
    <item>
      <title>Re: Identifying combinations of variable that add up to a set amount</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Identifying-combinations-of-variable-that-add-up-to-a-set-amount/m-p/607481#M176620</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/76464"&gt;@s_lassen&lt;/a&gt;: Nice approach! Wise to use the ROUND function.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I assume the intention was to sort by &lt;EM&gt;&lt;FONT face="courier new,courier"&gt;payment&lt;/FONT&gt;&lt;/EM&gt;, not by &lt;FONT face="courier new,courier"&gt;id&lt;/FONT&gt;.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I'd suggest modifying the stopping rule a bit, e.g.&lt;/P&gt;
&lt;PRE&gt;if round(sum,0.01)&amp;gt;&amp;amp;wanted &lt;STRONG&gt;&amp;amp; i&amp;gt;&amp;amp;maxiter&lt;/STRONG&gt; then leave;&lt;/PRE&gt;
&lt;P&gt;where macro variable &lt;FONT face="courier new,courier"&gt;maxiter&lt;/FONT&gt; would be defined as a "large" integer, e.g. &lt;FONT face="courier new,courier"&gt;1e7&lt;/FONT&gt;. (There might be more efficient stopping rules than this.)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Maybe my test dataset, shown below, containing random numbers (or the fact that I chose the uniform distribution to generate them) was unfortunate, but with the original stopping rule no matching sum was found.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
call streaminit(27182818);
do _n_=1 to 253;
  payment=round(rand('uniform',1,115.4),.01);
  output;
end;
run; 

proc print data=have(obs=4);
sum payment;
run; /* --&amp;gt; wanted=252.22 */

proc sort data=have;
by payment;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;EM&gt;Without&lt;/EM&gt; a stopping rule 9511 combinations (of 2 [did not occur], 3 or 4 payments -- I omitted the checks of 5 and 6) summing to 252.22 were found, with the modified stopping rule (using &lt;FONT face="courier new,courier"&gt;maxiter=1e7&lt;/FONT&gt;) at least 372 of them (not including the &lt;EM&gt;a priori&lt;/EM&gt; "known" combination, though). Run times were &amp;lt;20 and &amp;lt;2 seconds, respectively.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/62592"&gt;@robulon&lt;/a&gt;: The above example indicates that you might get &lt;EM&gt;too many&lt;/EM&gt;&amp;nbsp;solutions: thousands or even millions (see below!) of different combinations summing to 182.87. This is because the number of combinations exceeds the number of possible sums by several orders of magnitude (assuming that all payment values are integer multiples of 0.01). A coarse lower bound for the &lt;EM&gt;average&lt;/EM&gt; number of combinations adding up to the same given sum can be obtained as follows:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
do k=2 to 6;
  s+comb(253,k)/(k*11540);
end;
put s=;
run; /* Result: s=5103162.1672 */&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;So, it would be surprising if none of the sums of 2, 3 or 4 payments were equal to 182.87. (And these cases are manageable with s_lassen's solution without the stopping rule.) Even if that happened, you could proceed by selecting &lt;EM&gt;one&lt;/EM&gt; combination (of 4 payments) per &lt;EM&gt;distinct&lt;/EM&gt; sum and combine these (&amp;lt;50,000) sums, restricted to sums &amp;lt;182.87, in each case with the remaining 249 payment values. Thus (most likely), you'll find a suitable combination of 5 payments and then many more when you go back to the pertinent sum of 4 payments and select &lt;EM&gt;all&lt;/EM&gt; combinations leading to that particular sum.&lt;/P&gt;</description>
      <pubDate>Wed, 27 Nov 2019 22:59:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Identifying-combinations-of-variable-that-add-up-to-a-set-amount/m-p/607481#M176620</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2019-11-27T22:59:56Z</dc:date>
    </item>
    <item>
      <title>Re: Identifying combinations of variable that add up to a set amount</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Identifying-combinations-of-variable-that-add-up-to-a-set-amount/m-p/607607#M176673</link>
      <description>&lt;P&gt;Yes, of course, the data should be sorted by payment. I shall edit my post to show the right variable.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 27 Nov 2019 10:19:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Identifying-combinations-of-variable-that-add-up-to-a-set-amount/m-p/607607#M176673</guid>
      <dc:creator>s_lassen</dc:creator>
      <dc:date>2019-11-27T10:19:04Z</dc:date>
    </item>
    <item>
      <title>Re: Identifying combinations of variable that add up to a set amount</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Identifying-combinations-of-variable-that-add-up-to-a-set-amount/m-p/607657#M176701</link>
      <description>&lt;P&gt;It looks like a OR problem. Try post it at OR forum and&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/1636"&gt;@RobPratt&lt;/a&gt;&amp;nbsp; is there .&lt;/P&gt;</description>
      <pubDate>Wed, 27 Nov 2019 12:26:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Identifying-combinations-of-variable-that-add-up-to-a-set-amount/m-p/607657#M176701</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2019-11-27T12:26:15Z</dc:date>
    </item>
    <item>
      <title>Re: Identifying combinations of variable that add up to a set amount</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Identifying-combinations-of-variable-that-add-up-to-a-set-amount/m-p/609722#M177516</link>
      <description>&lt;P&gt;Thank you &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/76464"&gt;@s_lassen&lt;/a&gt; and &lt;A href="https://communities.sas.com/t5/user/viewprofilepage/user-id/32733" target="_blank" rel="noopener"&gt;@FreelanceReinhard&lt;/A&gt;, and apologies for not expressing my thanks earlier, I got a bit caught up in other matters.&amp;nbsp;I've tried to resolve my issue using this solution but it appears to be falling down somewhere. Although I now know the figures that make up the total (obtained through other means but would still be great for future reference to get this sorted) are 75.9 and 106.97, the code does not identify this particular combination. In fact, it does not identify any combinations at all. I have tried amending the code using round(,0.1) and this does return some combinations but not the exact one.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I've tried some more testing putting the figures in as datalines to reduce the number of figures but it still isn't working. Would you be able to suggest where I'm going wrong? I'm wondering if it is something to do with the decimal places but have tried multiplying all the figures by 100 to turn them all into integers but am still not arriving at the solution.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This is what I'm using&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="1"&gt;&lt;STRONG&gt;data&lt;/STRONG&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="1"&gt; amounts;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="1"&gt;input&lt;/FONT&gt;&lt;FONT face="Courier New" size="1"&gt; amount;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="1"&gt;datalines&lt;/FONT&gt;&lt;FONT face="Courier New" size="1"&gt;;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;75.9&lt;/P&gt;&lt;P&gt;106.97&lt;/P&gt;&lt;P&gt;100&lt;/P&gt;&lt;P&gt;255&lt;/P&gt;&lt;P&gt;643.59&lt;/P&gt;&lt;P&gt;251.25&lt;/P&gt;&lt;P&gt;;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="1"&gt;&lt;STRONG&gt;run&lt;/STRONG&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="1"&gt;;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="1"&gt;%let&lt;/FONT&gt;&lt;FONT face="Courier New" size="1"&gt; outstanding = 187.87;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;　&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="1"&gt;&lt;STRONG&gt;data&lt;/STRONG&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="1"&gt; want;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="1"&gt;array&lt;/FONT&gt;&lt;FONT face="Courier New" size="1"&gt; payments (&lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT color="#008080" face="Courier New" size="1"&gt;1000&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New" size="1"&gt;) &lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT color="#008080" face="Courier New" size="1"&gt;8&lt;/FONT&gt;&lt;/STRONG&gt; &lt;FONT color="#0000ff" face="Courier New" size="1"&gt;_temporary_&lt;/FONT&gt;&lt;FONT face="Courier New" size="1"&gt;;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="1"&gt;do&lt;/FONT&gt;&lt;FONT face="Courier New" size="1"&gt; _N_= &lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT color="#008080" face="Courier New" size="1"&gt;1&lt;/FONT&gt;&lt;/STRONG&gt; &lt;FONT color="#0000ff" face="Courier New" size="1"&gt;to&lt;/FONT&gt;&lt;FONT face="Courier New" size="1"&gt; nobs;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="1"&gt;set&lt;/FONT&gt;&lt;FONT face="Courier New" size="1"&gt; amounts &lt;/FONT&gt;&lt;FONT color="#0000ff" face="Courier New" size="1"&gt;nobs&lt;/FONT&gt;&lt;FONT face="Courier New" size="1"&gt; = nobs;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;payments(_N_) = amount;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="1"&gt;end&lt;/FONT&gt;&lt;FONT face="Courier New" size="1"&gt;;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="1"&gt;array&lt;/FONT&gt;&lt;FONT face="Courier New" size="1"&gt; idx &lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT color="#008080" face="Courier New" size="1"&gt;8&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New" size="1"&gt; idx1 - idx6;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="1"&gt;&lt;STRONG&gt;%macro&lt;/STRONG&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="1"&gt; &lt;STRONG&gt;&lt;I&gt;check&lt;/I&gt;&lt;/STRONG&gt;;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="1"&gt;%local&lt;/FONT&gt;&lt;FONT face="Courier New" size="1"&gt; comb;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="1"&gt;%do&lt;/FONT&gt;&lt;FONT face="Courier New" size="1"&gt; comb = &lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT color="#008080" face="Courier New" size="1"&gt;2&lt;/FONT&gt;&lt;/STRONG&gt; &lt;FONT color="#0000ff" face="Courier New" size="1"&gt;%to&lt;/FONT&gt; &lt;STRONG&gt;&lt;FONT color="#008080" face="Courier New" size="1"&gt;6&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New" size="1"&gt;;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="1"&gt;call missing(of idx1 - idx&amp;amp;&lt;/FONT&gt;&lt;FONT color="#008080" face="Courier New" size="1"&gt;comb.&lt;/FONT&gt;&lt;FONT face="Courier New" size="1"&gt;);&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="1"&gt;ncomb = comb(nobs, &amp;amp;&lt;/FONT&gt;&lt;FONT color="#008080" face="Courier New" size="1"&gt;comb.&lt;/FONT&gt;&lt;FONT face="Courier New" size="1"&gt;);&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="1"&gt;do i = &lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT color="#008080" face="Courier New" size="1"&gt;1&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New" size="1"&gt; to ncomb;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="1"&gt;rc = lexcombi(nobs, &amp;amp;&lt;/FONT&gt;&lt;FONT color="#008080" face="Courier New" size="1"&gt;comb.&lt;/FONT&gt;&lt;FONT face="Courier New" size="1"&gt;, of idx1 - idx&amp;amp;&lt;/FONT&gt;&lt;FONT color="#008080" face="Courier New" size="1"&gt;comb.&lt;/FONT&gt;&lt;FONT face="Courier New" size="1"&gt;);&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="1"&gt;sum = &lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT color="#008080" face="Courier New" size="1"&gt;0&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New" size="1"&gt;;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="1"&gt;do _N_ = &lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT color="#008080" face="Courier New" size="1"&gt;1&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New" size="1"&gt; to &amp;amp;&lt;/FONT&gt;&lt;FONT color="#008080" face="Courier New" size="1"&gt;comb.&lt;/FONT&gt;&lt;FONT face="Courier New" size="1"&gt;;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;sum + payments(idx(_N_));&lt;/P&gt;&lt;P&gt;end;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="1"&gt;if round(sum,&lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT color="#008080" face="Courier New" size="1"&gt;0.01&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New" size="1"&gt;) &amp;gt; &amp;amp;&lt;/FONT&gt;&lt;FONT color="#008080" face="Courier New" size="1"&gt;outstanding.&lt;/FONT&gt;&lt;FONT face="Courier New" size="1"&gt; then leave;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="1"&gt;if round(sum,&lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT color="#008080" face="Courier New" size="1"&gt;0.01&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New" size="1"&gt;) = &amp;amp;&lt;/FONT&gt;&lt;FONT color="#008080" face="Courier New" size="1"&gt;outstanding.&lt;/FONT&gt;&lt;FONT face="Courier New" size="1"&gt; then output;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;end;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="1"&gt;%end&lt;/FONT&gt;&lt;FONT face="Courier New" size="1"&gt;;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="1"&gt;&lt;STRONG&gt;%mend&lt;/STRONG&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="1"&gt; check;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%&lt;STRONG&gt;&lt;I&gt;check&lt;/I&gt;&lt;/STRONG&gt;;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="1"&gt;keep&lt;/FONT&gt;&lt;FONT face="Courier New" size="1"&gt; idx1 - idx6;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="1"&gt;&lt;STRONG&gt;run&lt;/STRONG&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="1"&gt;; &lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;As always, any advice will be gratefully received.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Rob&lt;/P&gt;</description>
      <pubDate>Thu, 05 Dec 2019 16:07:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Identifying-combinations-of-variable-that-add-up-to-a-set-amount/m-p/609722#M177516</guid>
      <dc:creator>robulon</dc:creator>
      <dc:date>2019-12-05T16:07:48Z</dc:date>
    </item>
    <item>
      <title>Re: Identifying combinations of variable that add up to a set amount</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Identifying-combinations-of-variable-that-add-up-to-a-set-amount/m-p/609725#M177517</link>
      <description>&lt;P&gt;In your new example note that 75.9+106.97=18&lt;STRONG&gt;2&lt;/STRONG&gt;.87, not 18&lt;FONT color="#FF0000"&gt;7&lt;/FONT&gt;.87.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For the general case: Have you tried loosening (or even omitting) the stopping rule as I suggested in my previous post?&lt;/P&gt;</description>
      <pubDate>Thu, 05 Dec 2019 16:18:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Identifying-combinations-of-variable-that-add-up-to-a-set-amount/m-p/609725#M177517</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2019-12-05T16:18:11Z</dc:date>
    </item>
    <item>
      <title>Re: Identifying combinations of variable that add up to a set amount</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Identifying-combinations-of-variable-that-add-up-to-a-set-amount/m-p/609897#M177573</link>
      <description>&lt;P&gt;Oh dear, how embarrassing Can't even try the excuse that it was a slip of the fingers as 2 and 7 are nowhere near each other!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The loosening of the stopping rule seems to have worked. I've created a macro variable of the target amount earlier in the code&amp;nbsp;using sql to calculate the difference between the expected amount and the actual amount&amp;nbsp;and have use&amp;nbsp;the code&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="1"&gt;if sum &amp;gt; &amp;amp;&lt;/FONT&gt;&lt;FONT color="#008080" face="Courier New" size="1"&gt;outstanding.&lt;/FONT&gt;&lt;FONT face="Courier New" size="1"&gt; + &lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT color="#008080" face="Courier New" size="1"&gt;9&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New" size="1"&gt; then leave;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Interestingly enough (for me anyway), if I added a value that was any less than 9 to the macro variable, no results were returned whereas if I use 9 or higher, it correctly identifies the amounts.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Huge thanks for the help people, always impressed by the number of members who are happy to share their expertise and help us less knowledgeable types develop their skills. next step for me is to work my way through the code so that I understand&amp;nbsp;how it's doing what it's&amp;nbsp;doing.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks again,&lt;/P&gt;&lt;P&gt;Rob&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;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 06 Dec 2019 09:34:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Identifying-combinations-of-variable-that-add-up-to-a-set-amount/m-p/609897#M177573</guid>
      <dc:creator>robulon</dc:creator>
      <dc:date>2019-12-06T09:34:03Z</dc:date>
    </item>
  </channel>
</rss>

