<?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: Net Groups of Lines in a Table to $0.00 in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Net-Groups-of-Lines-in-a-Table-to-0-00/m-p/469658#M285537</link>
    <description>&lt;P&gt;Can you please share&amp;nbsp;a representative data set, or maybe the most difficult one?&lt;/P&gt;</description>
    <pubDate>Tue, 12 Jun 2018 15:49:17 GMT</pubDate>
    <dc:creator>RobPratt</dc:creator>
    <dc:date>2018-06-12T15:49:17Z</dc:date>
    <item>
      <title>Net Groups of Lines in a Table to $0.00</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Net-Groups-of-Lines-in-a-Table-to-0-00/m-p/469510#M285528</link>
      <description>&lt;P&gt;Hello - thanks in advance for any help.&amp;nbsp; I have a data table with 300+ records.&amp;nbsp; I'm trying to identify combinations of lines that net to $0.00 automatically.&amp;nbsp; Is there a way to do this within SAS?&amp;nbsp; Thank you again for any input you may have.&amp;nbsp; A simplified example is below...&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;1/ -5&lt;/P&gt;&lt;P&gt;2/ 2&lt;/P&gt;&lt;P&gt;3/ 3&lt;/P&gt;&lt;P&gt;4/ -10&lt;/P&gt;&lt;P&gt;5/ 4&lt;/P&gt;&lt;P&gt;6/ 6&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In the above example, rows 1-3 and 4-6 would get grouped together since they net to $0.00 each.&amp;nbsp; (-5 + 2 + 3) and (-10 + 4 + 6).&amp;nbsp; Is there a way to identify these combinations automatically within SAS?&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 12 Jun 2018 12:21:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Net-Groups-of-Lines-in-a-Table-to-0-00/m-p/469510#M285528</guid>
      <dc:creator>adornodj</dc:creator>
      <dc:date>2018-06-12T12:21:09Z</dc:date>
    </item>
    <item>
      <title>Re: Unique Iterations  Combination of numbers that sum or match a target value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Net-Groups-of-Lines-in-a-Table-to-0-00/m-p/469514#M285529</link>
      <description>&lt;P&gt;1. Please change the title of your question to something meaningful such as "Combination of numbers that sum&amp;nbsp;to a target value" to increase the chances of getting an answer.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;2. I am not aware of any such feature in SAS, though SAS/IML can probably find such combinations.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;3. I found these solutions using Excel and C++ in case it helps.&lt;/P&gt;
&lt;P&gt;&lt;A href="http://www.k2e.com/tech-update/tips/147-using-excel-to-identify-entries-that-add-to-a-specific-value" target="_blank"&gt;http://www.k2e.com/tech-update/tips/147-using-excel-to-identify-entries-that-add-to-a-specific-value&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&lt;A href="https://leetcode.com/problems/combination-sum/description/" target="_blank"&gt;https://leetcode.com/problems/combination-sum/description/&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;DIV id="main"&gt;&amp;nbsp;&lt;/DIV&gt;</description>
      <pubDate>Tue, 12 Jun 2018 05:28:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Net-Groups-of-Lines-in-a-Table-to-0-00/m-p/469514#M285529</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2018-06-12T05:28:51Z</dc:date>
    </item>
    <item>
      <title>Re: Unique Iterations</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Net-Groups-of-Lines-in-a-Table-to-0-00/m-p/469534#M285530</link>
      <description>&lt;P&gt;Why doesn't all 6 get grouped together, all in they all sum up to 0?&amp;nbsp; Need to define your logic a bit better.&amp;nbsp; Anyways, a retained value should be enough - not not tested, post test data in the form of a datastep and what the output should look like!&lt;/P&gt;
&lt;PRE&gt;data want;
  set have;
  retain grp sm;
  if _n_=1 then do;
    grp=1;
    sm=val;
  end;
  else do;
    sm=sum(sm,val);
    if sm=0 then grp=grp+1;
  end;
run;
  &lt;/PRE&gt;</description>
      <pubDate>Tue, 12 Jun 2018 08:03:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Net-Groups-of-Lines-in-a-Table-to-0-00/m-p/469534#M285530</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2018-06-12T08:03:37Z</dc:date>
    </item>
    <item>
      <title>Re: Unique Iterations</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Net-Groups-of-Lines-in-a-Table-to-0-00/m-p/469585#M285531</link>
      <description>&lt;P&gt;Interesting...&lt;/P&gt;&lt;P&gt;I assume that you only want a series of subsequent lines that add to 0?&lt;/P&gt;&lt;P&gt;But how to group? there can still be many possibilities. I did an example program like this:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  do i=1 to 1000;
    x=ceil(rand('UNIFORM')*21)-11;
    output;
    end;
run;

data groups;
  set have nobs=nobs;
  sum=0;
  do _P_=_N_ to nobs;
    set have(keep=x rename=(x=x2)) point=_P_;
    sum+x2;
    if sum=0 then do;
      shortest_group=_P_-_N_+1;
      leave;
      end;
    end;
  keep i x shortest_group;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;So that for each line, you get the length of the shortest group that can start here. It is easy to see that a lot of them overlap; which ones would you choose for creating your group variable? Do you want the largest number of (non-overlapping) groups possible, or do you want to have as many lines as possible inside groups, or something else?&lt;/P&gt;</description>
      <pubDate>Tue, 12 Jun 2018 11:41:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Net-Groups-of-Lines-in-a-Table-to-0-00/m-p/469585#M285531</guid>
      <dc:creator>s_lassen</dc:creator>
      <dc:date>2018-06-12T11:41:41Z</dc:date>
    </item>
    <item>
      <title>Re: Unique Iterations</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Net-Groups-of-Lines-in-a-Table-to-0-00/m-p/469587#M285532</link>
      <description>&lt;P&gt;I fully agree with &lt;STRONG&gt;RW9&lt;/STRONG&gt; and &lt;STRONG&gt;s_lassen&lt;/STRONG&gt; that the specifications should be more precise. Restricting the groups to &lt;EM&gt;consecutive&lt;/EM&gt; records would indeed avoid substantial complexity which otherwise might require SAS/OR capabilities.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The possibility of overlapping groups has already been mentioned (like if in your own example 4 was replaced by 7).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If this is real-world data, aren't there other variables indicating likely or impossible groupings?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What if a group of values happen to sum to zero by chance?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In any case, make sure you avoid numeric representation issues if you are dealing with non-integer values.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Example:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data caveat;
input amt;
s+amt;
if s=0 then put 'This might NOT occur!';
if round(s,.001)=0 then put 'Rounding helps!';
cards;
4.90
1.90
-6.80
;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 12 Jun 2018 11:57:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Net-Groups-of-Lines-in-a-Table-to-0-00/m-p/469587#M285532</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2018-06-12T11:57:33Z</dc:date>
    </item>
    <item>
      <title>Net Groups of Lines in a Table to $0.00</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Net-Groups-of-Lines-in-a-Table-to-0-00/m-p/469595#M285533</link>
      <description>&lt;P&gt;Hello All - First off, &lt;U&gt;&lt;STRONG&gt;thank you&lt;/STRONG&gt;&lt;/U&gt; for the responses. I've updated to subject line to better reflect my question. I was working on two items late last night and apparently grouped both thoughts into this thread by mistake. A little more information on my question...&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;-Yes, the file nets down to $0.00 naturally as a whole. That said, we are trying to get it down into smaller subsets.&lt;/P&gt;&lt;P&gt;-Unfortunately, there are no additional character fields to utilize in order to group down the data smaller. We are left only with the amount field.&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;-Yes, it is plausible that some lines may group together to form a bigger cluster (like my example, 6 lines, versus splitting into 2 groups of 3).&amp;nbsp; This is somewhat unlikely based on how the amounts are created though.&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 12 Jun 2018 12:30:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Net-Groups-of-Lines-in-a-Table-to-0-00/m-p/469595#M285533</guid>
      <dc:creator>adornodj</dc:creator>
      <dc:date>2018-06-12T12:30:44Z</dc:date>
    </item>
    <item>
      <title>Re: Net Groups of Lines in a Table to $0.00</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Net-Groups-of-Lines-in-a-Table-to-0-00/m-p/469618#M285534</link>
      <description>&lt;P&gt;I am afraid you have to resort to SAS/OR .&lt;/P&gt;
&lt;P&gt;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;&lt;/P&gt;</description>
      <pubDate>Tue, 12 Jun 2018 13:18:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Net-Groups-of-Lines-in-a-Table-to-0-00/m-p/469618#M285534</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2018-06-12T13:18:58Z</dc:date>
    </item>
    <item>
      <title>Re: Net Groups of Lines in a Table to $0.00</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Net-Groups-of-Lines-in-a-Table-to-0-00/m-p/469636#M285535</link>
      <description>&lt;P&gt;Do the values have to be grouped in sequence?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;How do you expect the result to look for your given example?&lt;/P&gt;</description>
      <pubDate>Tue, 12 Jun 2018 14:35:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Net-Groups-of-Lines-in-a-Table-to-0-00/m-p/469636#M285535</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2018-06-12T14:35:03Z</dc:date>
    </item>
    <item>
      <title>Re: Net Groups of Lines in a Table to $0.00</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Net-Groups-of-Lines-in-a-Table-to-0-00/m-p/469648#M285536</link>
      <description>&lt;P&gt;No, the data does not (and often will not be) grouped in sequence.&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Honestly, the results will vary.&amp;nbsp; I've manually gone through a few iterations (I have several files to do this on with 300+ records).&amp;nbsp; I had one&amp;nbsp; table that nearly all rows paired off and an equal net value (ex: +100 / -100) and another where I was able to find 5/300 rows that netted and the remainder had to be made into one big group.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am working on Enterprise Guide, should have mentioned that earlier.&lt;/P&gt;</description>
      <pubDate>Tue, 12 Jun 2018 15:14:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Net-Groups-of-Lines-in-a-Table-to-0-00/m-p/469648#M285536</guid>
      <dc:creator>adornodj</dc:creator>
      <dc:date>2018-06-12T15:14:06Z</dc:date>
    </item>
    <item>
      <title>Re: Net Groups of Lines in a Table to $0.00</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Net-Groups-of-Lines-in-a-Table-to-0-00/m-p/469658#M285537</link>
      <description>&lt;P&gt;Can you please share&amp;nbsp;a representative data set, or maybe the most difficult one?&lt;/P&gt;</description>
      <pubDate>Tue, 12 Jun 2018 15:49:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Net-Groups-of-Lines-in-a-Table-to-0-00/m-p/469658#M285537</guid>
      <dc:creator>RobPratt</dc:creator>
      <dc:date>2018-06-12T15:49:17Z</dc:date>
    </item>
    <item>
      <title>Re: Net Groups of Lines in a Table to $0.00</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Net-Groups-of-Lines-in-a-Table-to-0-00/m-p/469662#M285538</link>
      <description>&lt;P&gt;All -as requested, here's a dataset. It's just a single column of amounts.&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 12 Jun 2018 16:05:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Net-Groups-of-Lines-in-a-Table-to-0-00/m-p/469662#M285538</guid>
      <dc:creator>adornodj</dc:creator>
      <dc:date>2018-06-12T16:05:08Z</dc:date>
    </item>
    <item>
      <title>Re: Net Groups of Lines in a Table to $0.00</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Net-Groups-of-Lines-in-a-Table-to-0-00/m-p/469715#M285539</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/215002"&gt;@adornodj&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;All -as requested, here's a dataset. It's just a single column of amounts.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Your data just threw another curve: What do you want with 0 values? Since 0 doesn't actually affect sums very much do they require special treatment? Such as all zero values in one "group" or a maximum of one zero per "group" or any other rules?&lt;/P&gt;</description>
      <pubDate>Tue, 12 Jun 2018 19:08:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Net-Groups-of-Lines-in-a-Table-to-0-00/m-p/469715#M285539</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2018-06-12T19:08:17Z</dc:date>
    </item>
    <item>
      <title>Re: Net Groups of Lines in a Table to $0.00</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Net-Groups-of-Lines-in-a-Table-to-0-00/m-p/469717#M285540</link>
      <description>&lt;P&gt;I'm not sure yet whether it is optimal, but the following greedy heuristic (which tries to partition the observations into a maximum number of groups) yields 86 groups:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc optmodel;
   set OBS_ALL;
   num x {OBS_ALL};
   read data have into OBS_ALL=[_N_] x=BASE_AMT;
   set OBS init OBS_ALL;

   /* Select[i] = 1 if observation i is selected to be in the current group; 0 otherwise */
   var Select {OBS} binary;

   /* selected observations sum to 0 */
   con SumToZero:
      sum {i in OBS} x[i] * Select[i] = 0;

   /* select at least one observation */
   con Nonempty:
      sum {i in OBS} Select[i] &amp;gt;= 1;

   /* minimize number selected */
   min NumSelected = sum {i in OBS} Select[i];

   num group {OBS_ALL};
   set SELECTED = {i in OBS: Select[i].sol &amp;gt; 0.5};
   num g init 0;

   /* greedy heuristic */
   do until (_solution_status_ = 'INFEASIBLE' or card(OBS) = 0);
      /* call MILP solver */
      solve;
      put SELECTED=;
      g = g + 1;
      for {i in SELECTED} group[i] = g;
      OBS = OBS diff SELECTED;
   end;

   create data want(drop=i) from [i] x group;
quit;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The resulting group size frequencies are:&lt;/P&gt;
&lt;DIV class="branch"&gt;
&lt;DIV&gt;
&lt;DIV align="center"&gt;
&lt;TABLE class="table" rules="all" frame="box" cellspacing="0" cellpadding="5" summary="Procedure SQL: Query Results"&gt;
&lt;THEAD&gt;
&lt;TR&gt;
&lt;TH class="r b header" scope="col"&gt;group_size&lt;/TH&gt;
&lt;TH class="r b header" scope="col"&gt;group_size_freq&lt;/TH&gt;
&lt;/TR&gt;
&lt;/THEAD&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD class="r data"&gt;1&lt;/TD&gt;
&lt;TD class="r data"&gt;6&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class="r data"&gt;2&lt;/TD&gt;
&lt;TD class="r data"&gt;50&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class="r data"&gt;3&lt;/TD&gt;
&lt;TD class="r data"&gt;11&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class="r data"&gt;4&lt;/TD&gt;
&lt;TD class="r data"&gt;12&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class="r data"&gt;5&lt;/TD&gt;
&lt;TD class="r data"&gt;5&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class="r data"&gt;8&lt;/TD&gt;
&lt;TD class="r data"&gt;1&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class="r data"&gt;115&lt;/TD&gt;
&lt;TD class="r data"&gt;1&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;That is, 6 singletons (the ones with BASE_AMT = 0), 50 pairs, 11 triples, ..., and one big group of 115.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 12 Jun 2018 20:14:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Net-Groups-of-Lines-in-a-Table-to-0-00/m-p/469717#M285540</guid>
      <dc:creator>RobPratt</dc:creator>
      <dc:date>2018-06-12T20:14:03Z</dc:date>
    </item>
    <item>
      <title>Re: Net Groups of Lines in a Table to $0.00</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Net-Groups-of-Lines-in-a-Table-to-0-00/m-p/469727#M285541</link>
      <description>&lt;P&gt;Here is an example of a brute force not terribly elegant using only data step code approach and has the potential of not actually working depending on your actual data. This is an example of find the pairs that total 0, then find the groups of 3 that total 0. The pattern could be continued. This captures both the index (original row number) as well as the values on those rows with one record per group. I did two levels of this.&lt;/P&gt;
&lt;PRE&gt;data have;
   input BASE_AMT;
datalines;
-758086.38
-715706.92
-308866.51
-65557.44
-39680
-22150.98
-20967.45
-18659
-13091.9
-8400
-7800
-5902.99
-4900
-3800
-3567.64
-3472
-3201.42
-3108.97
-2462
-2400
-1975
-1975
-1964
-1425.95
-1256.03
-1200
-7492.68
7492.68
-7301.35
7301.35
-1200
-1200
-3346.65
3346.65
-1026.9
-912.5
-912.5
-2862.83
2862.83
-849.57
-2499
2499
-822.87
-781.76
-2357.55
2357.55
-781.76
-2272.4
2272.4
-2060.21
2060.21
-750
-750
-750
-750
-1893.63
1893.63
-1739.47
1739.47
-690.63
-1509.29
1509.29
-1492.8
1492.8
-673.44
-673.44
-1399
1399
-673.44
-673.44
-1226.26
1226.26
-663.34
-663.33
-663.33
-658.53
-971.51
971.51
-945.45
945.45
-600.4
-600
-884.25
884.25
-593.74
-576.89
-566.91
-799.59
799.59
-554.47
-544.28
-526.75
-519.78
-515.62
-510
-510
-500
-692.2
692.2
-500
-469.08
-468.45
-394
-372.26
-369.08
-361.42
-361.42
-347.89
-347.89
-325.4
-318.6
-300
-581.25
581.25
-300
-296.86
-560
560
-288.43
-279.55
-540.45
540.45
-261.84
-524.6
524.6
-255.09
-251.25
-250
-247.08
-226.67
-217
-189.1
-497.2
497.2
-181.82
-485.35
485.35
-181.53
-180.08
-455.1
455.1
-154
-446.77
446.77
-418.2
418.2
-147.16
-384.7
384.7
-146
-137.84
-132.43
-130.62
-129.96
-129
-116.1
-313.01
313.01
-302.95
302.95
-113.75
-104.34
-101.32
-99
-94.73
-280.75
280.75
-90
-87.84
-83.28
-81
-79.77
-79.24
-78.84
-77.71
-73.26
-219.96
219.96
-68.11
-68.09
-216.86
216.86
-64.28
-64.12
-185.6
185.6
-62.91
-62.64
-62.37
174.14
-174.14
-173.85
173.85
-62.35
-59.76
-149.85
149.85
-59.04
-59.01
-56.43
-55.68
-54.5
-54.04
-52.8
-126
126
-120.55
120.55
-52.57
-52.52
-50
-49.36
-47.25
-47.22
-44.56
-43.21
-42
-40.46
-36.3
-88.3
88.3
-35.4
-34.61
-33.42
-31.66
-30.96
-30.67
-29.12
-73.85
73.85
-28.9
-68.72
68.72
-28.24
-28.09
-27.56
-26.23
-25.65
-25.1
-24.44
-24.22
-59.55
59.55
-23.42
-20.74
-57.9
57.9
-19.41
-56.24
56.24
-18.65
-18.41
-17.12
-16.65
-15.72
-14.75
-13.54
-13.38
-12.16
-11.8
-11.7
-11.66
-11.53
-42.28
42.28
-11.45
-11.22
-10.62
-10
-8.9
-34.35
34.35
-8.77
-8.64
-8.44
-7.89
-6.24
-6
-5.64
-5.35
-4.76
-4.52
-4.48
-3.51
-3.41
-2.99
-2.85
-2.65
-2.53
-18.9
18.9
-2.46
-2.37
-1.96
-1.76
-1.63
46.23
61.35
88.78
89.77
92
107.49
167.52
212.34
218.12
229.44
267.52
296.38
451.3
485.73
510
629.13
790.82
799.37
835.03
1329.06
1443.56
1581.69
1928.62
2274.62
2726.63
3000.4
4093.26
5426.75
9014.1
25414.75
43152
44807.36
1906992.59
0
0
0
0
0
0
;
run;

proc transpose data=have out=work.trans
  prefix=ba
;
var base_amt;
run;

data work.pairs ( keep=iout jout ival jval)
     work.leftover (keep=ba:)
;
   set work.trans end=last;
   array b ba:;
   do i= 1 to (dim(b)-1);
      do j= 2 to dim(b);
         if sum(b[i],b[j])= 0 then do;
            if not missing(b[i]) then iout=i;
            if not missing(b[j]) then jout=j;
            ival = b[i];
            jval = b[j];
            output work.pairs;
            call missing(b[i],b[j],iout,jout);
            leave;
         end;
      end;
   end;
  
   if last then output work.leftover;
run;

data work.trios ( keep=iout jout kout ival jval kval)
     work.leftover2 (keep=ba:)
;
   set work.leftover end=last;
   array b ba:;
   do i= 1 to (dim(b)-2);
      do j= 2 to (dim(b)-1);
         do k= 3 to dim(b);
            if sum(b[i],b[j],b[k])= 0 then do;
               if not missing(b[i]) then iout=i;
               if not missing(b[j]) then jout=j;
               if not missing(b[k]) then kout=k;
               ival = b[i];
               jval = b[j];
               kval = b[k];
               output work.trios;
               call missing(b[i],b[j],b[k],iout,jout,kout);
               leave;
            end;
         end;
      end;
   end;
   if last then output work.leftover2;
run;&lt;/PRE&gt;
&lt;P&gt;Actually one data step with "large" number of nested do loops should work but the actual number of nested levels will depend on the number of rows of data and the actual values.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The potential of not getting everything assigned to a group comes about when a value that is needed to complete a "complex" group of maybe 6 or 7 values gets used in a smaller set.&lt;/P&gt;</description>
      <pubDate>Tue, 12 Jun 2018 19:44:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Net-Groups-of-Lines-in-a-Table-to-0-00/m-p/469727#M285541</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2018-06-12T19:44:20Z</dc:date>
    </item>
    <item>
      <title>Re: Net Groups of Lines in a Table to $0.00</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Net-Groups-of-Lines-in-a-Table-to-0-00/m-p/469754#M285542</link>
      <description>&lt;P&gt;I had started to program a somewhat similar approach (starting with singletons, then pairs, triples, then I gave it up because it seemed to me that the number of combinations to check might grow too&amp;nbsp;large -- depending on the data, though). I used the CALL ALLCOMBI routine rather than nested DO loops to create the combinations.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Not sure where the "&lt;SPAN&gt;potential of not getting everything assigned to a group" should come from: Since the original dataset has a total of zero, the leftover after taking away any proper subset with sum zero must have a total of zero as well. So, the worst case I could imagine was that at some point the remaining (not further decomposable) subset would be unnecessarily large.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 12 Jun 2018 20:30:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Net-Groups-of-Lines-in-a-Table-to-0-00/m-p/469754#M285542</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2018-06-12T20:30:20Z</dc:date>
    </item>
    <item>
      <title>Re: Net Groups of Lines in a Table to $0.00</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Net-Groups-of-Lines-in-a-Table-to-0-00/m-p/469762#M285543</link>
      <description>&lt;P&gt;Everyone -THANK YOU.&amp;nbsp; I'm implementing and trying both the "brute force" and the "greedy" applications. I sincerely cannot thank the community enough for the help on this.&amp;nbsp; I was pulling my hair out trying to figure this out, so from the bottom of my grateful heart thank you.&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 12 Jun 2018 21:04:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Net-Groups-of-Lines-in-a-Table-to-0-00/m-p/469762#M285543</guid>
      <dc:creator>adornodj</dc:creator>
      <dc:date>2018-06-12T21:04:04Z</dc:date>
    </item>
    <item>
      <title>Re: Net Groups of Lines in a Table to $0.00</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Net-Groups-of-Lines-in-a-Table-to-0-00/m-p/469773#M285544</link>
      <description>&lt;P&gt;You can replace the pair search with something simpler.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data work.pairs ( keep=iout jout ival jval)
     work.leftover (keep=ba:)
;
   set work.trans end=last;
   array b ba:;
   do i= 1 to (dim(b)-1);
     if b[i]=. then continue;
     j=whichn(-b[i],of b[*]);
     if j=0 then continue;
     iout=i;
     jout=j;
     ival = b[i];
     jval = b[j];
     output work.pairs2
     call missing(b[i],b[j],iout,jout);
   end;
  
   if last then output work.leftover;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Not that it makes difference overall....&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 12 Jun 2018 21:54:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Net-Groups-of-Lines-in-a-Table-to-0-00/m-p/469773#M285544</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2018-06-12T21:54:49Z</dc:date>
    </item>
    <item>
      <title>Re: Net Groups of Lines in a Table to $0.00</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Net-Groups-of-Lines-in-a-Table-to-0-00/m-p/470174#M285545</link>
      <description>&lt;P&gt;Your problem is to partition the observations into zero-sum groups.&amp;nbsp; If&amp;nbsp;the objective is to maximize the number of groups, it is&amp;nbsp;clear that you want to treat each singleton (BASE_AMT = 0) as its own group, because any group that contains more than one observation and includes a 0 can be split into two groups, each with zero sum.&amp;nbsp; Less obvious is the fact that you can treat each pair x and -x as their own group, without loss of optimality.&amp;nbsp; There are two cases to consider, given any partition of the observations into zero-sum groups:&lt;/P&gt;
&lt;P&gt;1. If a group contains more than two observations and includes a pair x and -x, you can split off the pair into its own group {x,-x}, preserving zero sums, and thereby increase the number of groups by one.&lt;/P&gt;
&lt;P&gt;2. If x and -x appear in two different groups, say G1 and G2, you can reorganize as two groups {x,-x} and (G1 diff {x}) union (G2 diff {-x}), each with zero sum, and thereby preserve the number of groups.&amp;nbsp; Explicitly, G1 diff {x} sums to -x (because G1 sums to 0), and G2 diff {-x}&amp;nbsp;sums to x, so their union&amp;nbsp;sums to -x + x = 0.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This means that the first two rounds of the greedy algorithm are optimal in the sense that removing singletons and pairs does not ruin your chances of maximizing the number of groups.&lt;/P&gt;</description>
      <pubDate>Thu, 14 Jun 2018 19:22:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Net-Groups-of-Lines-in-a-Table-to-0-00/m-p/470174#M285545</guid>
      <dc:creator>RobPratt</dc:creator>
      <dc:date>2018-06-14T19:22:31Z</dc:date>
    </item>
  </channel>
</rss>

