<?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: OptModel to find a variance in a table in Mathematical Optimization, Discrete-Event Simulation, and OR</title>
    <link>https://communities.sas.com/t5/Mathematical-Optimization/OptModel-to-find-a-variance-in-a-table/m-p/760505#M3471</link>
    <description>&lt;P&gt;Here's one way to find a single solution (using 21 to match your example):&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc optmodel;
   /* declare index sets */
   set &amp;lt;str&amp;gt; ListSet;

   /* declare parameters */
   num a {ListSet} init 0;

   /* read the set of Variables */
   read data OPTMODELSETUP into ListSet=[ID] a=Var1;
   print a;

   /* declare the model */
   var X {ListSet} binary;

   con sum {ID in ListSet} a[ID]*X[ID] = 21;

   /* call the solver and save the results */
   solve noobj;
   print X;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;To get all solutions and save them to a data set, you can do this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;   solve with clp / findallsolns;
   create data out from [s]=(1.._NSOL_) {ID in ListSet} &amp;lt;col(ID)=(a[ID]*X[ID].sol[s])&amp;gt;;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;That way outputs one observation per solution, but CREATE DATA can save the results however you want.&amp;nbsp; Here's a sparse alternative:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;   create data out2 from [s ID]={s in 1.._NSOL_, ID in ListSet: X[ID].sol[s] &amp;gt; 0.5} a=a[ID];&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Tue, 10 Aug 2021 00:16:18 GMT</pubDate>
    <dc:creator>RobPratt</dc:creator>
    <dc:date>2021-08-10T00:16:18Z</dc:date>
    <item>
      <title>OptModel to find a variance in a table</title>
      <link>https://communities.sas.com/t5/Mathematical-Optimization/OptModel-to-find-a-variance-in-a-table/m-p/760499#M3470</link>
      <description>&lt;P&gt;I am trying to solve the classic find the number problem.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a report which has a variance on it of some value Z. I want to be able to pinpoint what rows may be the cause of the variance Z in my data. i.e there are certain rows that may add up to the Z. In a practical sense the situation plays out as follows:&lt;/P&gt;&lt;P&gt;Bob: "hey Chad, your total is over by $21"&lt;/P&gt;&lt;P&gt;Chad: "I received a listing of the balancing entries from Bill this afternoon, I'm not too sure what is making up the $21 variance"&lt;/P&gt;&lt;P&gt;Bob: "Is it possible to find out what items are throwing out the variance? perhaps there are some entries that are unbalanced?"&amp;nbsp;&lt;/P&gt;&lt;P&gt;Chad: (sigh) "I'll call my wife, I'm not going home tonight".&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In most cases, Bob spends hours trying to find out which rows are causing the error, and enviably misses his dinner at home.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have set up a sample problem with primes:&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;Items in "a" are primes indexed by the ID AAA,BBB,...etc.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="SASAlex101_0-1628549571219.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/62425i6DB139005ACA21EA/image-size/medium?v=v2&amp;amp;px=400" role="button" title="SASAlex101_0-1628549571219.png" alt="SASAlex101_0-1628549571219.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;let's say I want to find out which numbers add up to 21. (answer is AAA + DDD + FFF)&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have constructed a vector X initialized as 0's and can only be binary values. My desired result is below:&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="SASAlex101_1-1628549809081.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/62426i0F3B59D7E979DD37/image-size/medium?v=v2&amp;amp;px=400" role="button" title="SASAlex101_1-1628549809081.png" alt="SASAlex101_1-1628549809081.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;INPUT DATA = &lt;CODE class=" language-sas"&gt;OPTMODELSETUP &lt;/CODE&gt;:&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;ID	Var1
AAA	1
BBB	3
CCC	5
DDD	7
EEE	11
FFF	13
GGG	17&lt;/PRE&gt;&lt;P&gt;I'm not seeming to be able to get there:&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc optmodel;

/* declare index sets */
set &amp;lt;str&amp;gt; ListSet;
set VarChoices = 1..1;

/* declare parameters */
num a {ListSet,VarChoices} init 0;

/* read the set of Variables */
read data OPTMODELSETUP into ListSet=[ID] {j in VarChoices} &amp;lt;a[ID,j]=col('Var'||(j))&amp;gt;;

print a;

/* declare the model */
var X {ListSet, VarChoices} binary init 0;

min Objective = (sum {ID in ListSet, j in VarChoices} a[ID,j]*X[ID,j]);

con sum {j in VarChoices} a[ID,j]*X[ID,j] = 31;

/* call the solver and save the results */
   solve;

/*ods output PrintTable#8=Solution;*/
print X;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Is this problem expandable such that all solutions can be displayed as different columns in X? we know in real life, there may be multiple solutions in which the rows total a certain value.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks in advance.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 09 Aug 2021 23:02:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Mathematical-Optimization/OptModel-to-find-a-variance-in-a-table/m-p/760499#M3470</guid>
      <dc:creator>SASAlex101</dc:creator>
      <dc:date>2021-08-09T23:02:53Z</dc:date>
    </item>
    <item>
      <title>Re: OptModel to find a variance in a table</title>
      <link>https://communities.sas.com/t5/Mathematical-Optimization/OptModel-to-find-a-variance-in-a-table/m-p/760505#M3471</link>
      <description>&lt;P&gt;Here's one way to find a single solution (using 21 to match your example):&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc optmodel;
   /* declare index sets */
   set &amp;lt;str&amp;gt; ListSet;

   /* declare parameters */
   num a {ListSet} init 0;

   /* read the set of Variables */
   read data OPTMODELSETUP into ListSet=[ID] a=Var1;
   print a;

   /* declare the model */
   var X {ListSet} binary;

   con sum {ID in ListSet} a[ID]*X[ID] = 21;

   /* call the solver and save the results */
   solve noobj;
   print X;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;To get all solutions and save them to a data set, you can do this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;   solve with clp / findallsolns;
   create data out from [s]=(1.._NSOL_) {ID in ListSet} &amp;lt;col(ID)=(a[ID]*X[ID].sol[s])&amp;gt;;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;That way outputs one observation per solution, but CREATE DATA can save the results however you want.&amp;nbsp; Here's a sparse alternative:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;   create data out2 from [s ID]={s in 1.._NSOL_, ID in ListSet: X[ID].sol[s] &amp;gt; 0.5} a=a[ID];&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 10 Aug 2021 00:16:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Mathematical-Optimization/OptModel-to-find-a-variance-in-a-table/m-p/760505#M3471</guid>
      <dc:creator>RobPratt</dc:creator>
      <dc:date>2021-08-10T00:16:18Z</dc:date>
    </item>
    <item>
      <title>Re: OptModel to find a variance in a table</title>
      <link>https://communities.sas.com/t5/Mathematical-Optimization/OptModel-to-find-a-variance-in-a-table/m-p/760508#M3472</link>
      <description>Nice one Rob!;&lt;BR /&gt;So if I'm interpreting the solution correctly,&lt;BR /&gt;s AAA BBB CCC DDD EEE FFF GGG&lt;BR /&gt;1 0 3 0 7 11 0 0&lt;BR /&gt;2 0 3 5 0 0 13 0&lt;BR /&gt;3 1 0 0 7 0 13 0&lt;BR /&gt;4 1 3 0 0 0 0 17&lt;BR /&gt;&lt;BR /&gt;if we have any value in the column &amp;gt; 0 then we interpret that as a trigger?&lt;BR /&gt;i.e solution row 3 AAA + DDD + FFF&lt;BR /&gt;solution row 4 AAA + BBB + GGG = 21&lt;BR /&gt;&lt;BR /&gt;Thanks Rob;&lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Tue, 10 Aug 2021 00:57:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Mathematical-Optimization/OptModel-to-find-a-variance-in-a-table/m-p/760508#M3472</guid>
      <dc:creator>SASAlex101</dc:creator>
      <dc:date>2021-08-10T00:57:10Z</dc:date>
    </item>
    <item>
      <title>Re: OptModel to find a variance in a table</title>
      <link>https://communities.sas.com/t5/Mathematical-Optimization/OptModel-to-find-a-variance-in-a-table/m-p/760509#M3473</link>
      <description>&lt;P&gt;Yes, that is the correct interpretation.&lt;/P&gt;</description>
      <pubDate>Tue, 10 Aug 2021 01:12:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Mathematical-Optimization/OptModel-to-find-a-variance-in-a-table/m-p/760509#M3473</guid>
      <dc:creator>RobPratt</dc:creator>
      <dc:date>2021-08-10T01:12:42Z</dc:date>
    </item>
  </channel>
</rss>

