<?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: Subset Sum Problem in SAS/IML Software and Matrix Computations</title>
    <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Subset-Sum-Problem/m-p/227367#M2349</link>
    <description>&lt;P&gt;Here's a way to do it by using the CLP solver in PROC OPTMODEL in SAS/OR:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc optmodel;
   num n = 7;
   num a {1..n} = [1, 4, 6, 6, 7, 19, 23];
   num target = 12;
   var X {1..n} binary;
   con sum {j in 1..n} a[j]*X[j] = target;
   solve with CLP / findallsolns;
   for {s in 1.._NSOL_} print {j in 1..n: X[j].sol[s] &amp;gt; 0} a[j];
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Fri, 25 Sep 2015 21:38:24 GMT</pubDate>
    <dc:creator>RobPratt</dc:creator>
    <dc:date>2015-09-25T21:38:24Z</dc:date>
    <item>
      <title>Subset Sum Problem</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Subset-Sum-Problem/m-p/227127#M2335</link>
      <description>&lt;P&gt;Has anyone written a SAS program to solve the Subset Sum Problem? &amp;nbsp;Given a reasonably small set of numbers, is there some combination of these numbers that sum up to a particular number?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here's a very simple example:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;We have the numbers 1, 4, 6, 6, 7, 19, and 23.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What combinations of these numbers will give us the number 12?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Solutions:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;1 + 4 + 7 = 12&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;6 + 6 = 12&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks much,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;David Oesper&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;P.S. I don't currently have access to SAS/IML at work, but I do at home. &amp;nbsp;I would prefer a Base SAS solution, if possible, but any and all approaches using any SAS product will be appreciated!&lt;/P&gt;</description>
      <pubDate>Thu, 24 Sep 2015 17:13:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Subset-Sum-Problem/m-p/227127#M2335</guid>
      <dc:creator>doesper</dc:creator>
      <dc:date>2015-09-24T17:13:20Z</dc:date>
    </item>
    <item>
      <title>Re: Subset Sum Problem</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Subset-Sum-Problem/m-p/227135#M2336</link>
      <description>&lt;P&gt;If you sort the data, you can do something smarter than brute force, but I'm guessing this exercise if more for puzzles than work, so here is a brute force solution:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc iml;
items = {1, 4, 6, 6, 7, 19, 23};
target = 12;

/* preliminary step: get rid of any 
   elements greater than the target */
items = items[ loc(items&amp;lt;=target) ];
N = nrow(items);

do k = 1 to N;
   a = allcomb(N, k);          /* all combs taken k at a time */
   m = shape(items[a], 0, k);  /* make matrix with k rows */
   idx = loc(m[,+] = target);  /* rows whose sum = target */
   if ncol(idx)&amp;gt;0 then 
      print (m[idx,]);
end;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Probably &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/18408"&gt;@Ksharp﻿&lt;/a&gt;&amp;nbsp;or &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/19924"&gt;@FriedEgg﻿&lt;/a&gt;&amp;nbsp;can whip up something fast in Base SAS.&lt;/P&gt;</description>
      <pubDate>Thu, 24 Sep 2015 17:44:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Subset-Sum-Problem/m-p/227135#M2336</guid>
      <dc:creator>Rick_SAS</dc:creator>
      <dc:date>2015-09-24T17:44:46Z</dc:date>
    </item>
    <item>
      <title>Re: Subset Sum Problem</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Subset-Sum-Problem/m-p/227157#M2337</link>
      <description>&lt;P&gt;Thanks, Rick! &amp;nbsp;I'll give this a try at home and perhaps someone else will post a Base SAS solution as you suggest. &amp;nbsp;By the way, I have really been enjoying your blog postings that feature various mathematical topics. &amp;nbsp;Kudos!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks again,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Dave&lt;/P&gt;</description>
      <pubDate>Thu, 24 Sep 2015 18:51:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Subset-Sum-Problem/m-p/227157#M2337</guid>
      <dc:creator>doesper</dc:creator>
      <dc:date>2015-09-24T18:51:37Z</dc:date>
    </item>
    <item>
      <title>Re: Subset Sum Problem</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Subset-Sum-Problem/m-p/227213#M2338</link>
      <description>&lt;P&gt;Here's the DATA step version. &amp;nbsp;First, note that there is a somewhat natural inclination to find all combinations of 2, then all combinations of 3, etc. &amp;nbsp;That can be done, but it's harder and not necessary. &amp;nbsp;Assuming that your data set contains var1-var6, and you want all combinations that add to 12:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data want;&lt;/P&gt;&lt;P&gt;set have;&lt;/P&gt;&lt;P&gt;do include_v1=0, 1;&lt;/P&gt;&lt;P&gt;do include_v2=0, 1;&lt;/P&gt;&lt;P&gt;do include_v3=0, 1;&lt;/P&gt;&lt;P&gt;do include_v4=0, 1;&lt;/P&gt;&lt;P&gt;do include_v5=0, 1;&lt;/P&gt;&lt;P&gt;do include_v6=0, 1;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp;total = include_v1 * var1 + include_v2 * var2 + include_v3 * var3 + include_v4 * var4 + include_v5 * var5 + include_v6 * var6;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp;if total=12 then output;&lt;/P&gt;&lt;P&gt;end; end; end; end; end; end;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;You can always add arrays. &amp;nbsp;You can always change the spacing and indentation. &amp;nbsp;You can always add macro language. &amp;nbsp;But whether you do or not, the trick is to treat this as a set of binary conditions rather than a variety of combinations.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Good luck.&lt;/P&gt;</description>
      <pubDate>Fri, 25 Sep 2015 03:07:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Subset-Sum-Problem/m-p/227213#M2338</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2015-09-25T03:07:26Z</dc:date>
    </item>
    <item>
      <title>Re: Subset Sum Problem</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Subset-Sum-Problem/m-p/227242#M2343</link>
      <description>&lt;P&gt;From a mathematical perspective, &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/4954"&gt;@Astounding﻿&lt;/a&gt;'s solution has an interesting interpretation in terms of 0/1 matrices. when the set of numbers contains k elements, the matrix formulation of his technique is to create a 0/1 matrix with k columns in which the rows cover all possible combinations. Each of the first k rows have exactly one 1. Each of the next "k choose 2" rows have exactly two 1s, and so forth until the last row contains all 1s.&amp;nbsp; You can implement Astounding's idea in a matrix language by using the ideas in the article &lt;A href="http://blogs.sas.com/content/iml/2011/01/05/creating-a-matrix-with-all-combinations-of-zeros-and-ones.html" target="_self"&gt;"Creating a matrix with all combinations of zeros and ones".&lt;/A&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The same article has a formulation that is appropriate for a DATA step implementation that does not require k nested loops.&amp;nbsp; This formulation (without the nested loops) can support a set with an arbitrary number of elements (within reason, say k &amp;lt;= 18). This is probably similar to what Astounding was thinking with the statement "you can always add arrays."&lt;/P&gt;</description>
      <pubDate>Fri, 25 Sep 2015 11:58:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Subset-Sum-Problem/m-p/227242#M2343</guid>
      <dc:creator>Rick_SAS</dc:creator>
      <dc:date>2015-09-25T11:58:38Z</dc:date>
    </item>
    <item>
      <title>Re: Subset Sum Problem</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Subset-Sum-Problem/m-p/227291#M2345</link>
      <description>&lt;P&gt;RIck,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Just to illustrate some of the techniques I had in mind ... what if you had 25 variables instead of 6? &amp;nbsp;Macro language might generate the loops:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%macro loop_begins (n_loops=);&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp;%local i;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp;%do i=1 %to &amp;amp;n_loops;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; do include_v&amp;amp;i=0, 1;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp;%end;&lt;/P&gt;&lt;P&gt;%mend loop_begins;&lt;/P&gt;&lt;P&gt;%loop_begins (n_loops=25)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Arrays might come in handy for calculating TOTAL.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;array flags {25} include_v1 - include_v25;&lt;/P&gt;&lt;P&gt;array vars {25} var1-var25;&lt;/P&gt;&lt;P&gt;total=0;&lt;/P&gt;&lt;P&gt;do i=1 to 25;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp;total = total + flags{i} * vars{i};&lt;/P&gt;&lt;P&gt;end;&lt;/P&gt;&lt;P&gt;if total=12 then output;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The logic doesn't really change, just the way you generate the needed statements changes.&lt;/P&gt;</description>
      <pubDate>Fri, 25 Sep 2015 15:41:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Subset-Sum-Problem/m-p/227291#M2345</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2015-09-25T15:41:49Z</dc:date>
    </item>
    <item>
      <title>Re: Subset Sum Problem</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Subset-Sum-Problem/m-p/227367#M2349</link>
      <description>&lt;P&gt;Here's a way to do it by using the CLP solver in PROC OPTMODEL in SAS/OR:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc optmodel;
   num n = 7;
   num a {1..n} = [1, 4, 6, 6, 7, 19, 23];
   num target = 12;
   var X {1..n} binary;
   con sum {j in 1..n} a[j]*X[j] = target;
   solve with CLP / findallsolns;
   for {s in 1.._NSOL_} print {j in 1..n: X[j].sol[s] &amp;gt; 0} a[j];
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 25 Sep 2015 21:38:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Subset-Sum-Problem/m-p/227367#M2349</guid>
      <dc:creator>RobPratt</dc:creator>
      <dc:date>2015-09-25T21:38:24Z</dc:date>
    </item>
  </channel>
</rss>

