<?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: How to create a column that sums by one less variable each time? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-a-column-that-sums-by-one-less-variable-each-time/m-p/792322#M253842</link>
    <description>&lt;P&gt;What code have you tried?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It looks like if you reverse the sort order, WANT is a running sum of NUMBER.&lt;/P&gt;</description>
    <pubDate>Tue, 25 Jan 2022 20:59:23 GMT</pubDate>
    <dc:creator>Quentin</dc:creator>
    <dc:date>2022-01-25T20:59:23Z</dc:date>
    <item>
      <title>How to create a column that sums by one less variable each time?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-a-column-that-sums-by-one-less-variable-each-time/m-p/792318#M253841</link>
      <description>&lt;P&gt;Hello! I am trying to create the "want" column.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;TABLE border="0" cellspacing="0" cellpadding="0"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;venue&lt;/TD&gt;&lt;TD&gt;fruit&lt;/TD&gt;&lt;TD&gt;number&lt;/TD&gt;&lt;TD&gt;&lt;STRONG&gt;want&lt;/STRONG&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;market&lt;/TD&gt;&lt;TD&gt;apple&lt;/TD&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;&lt;STRONG&gt;12&lt;/STRONG&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;market&lt;/TD&gt;&lt;TD&gt;orange&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;&lt;STRONG&gt;9&lt;/STRONG&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;market&lt;/TD&gt;&lt;TD&gt;banana&lt;/TD&gt;&lt;TD&gt;7&lt;/TD&gt;&lt;TD&gt;&lt;STRONG&gt;7&lt;/STRONG&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;store&lt;/TD&gt;&lt;TD&gt;apple&lt;/TD&gt;&lt;TD&gt;4&lt;/TD&gt;&lt;TD&gt;&lt;STRONG&gt;11&lt;/STRONG&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;store&lt;/TD&gt;&lt;TD&gt;orange&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;&lt;STRONG&gt;7&lt;/STRONG&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;store&lt;/TD&gt;&lt;TD&gt;banana&lt;/TD&gt;&lt;TD&gt;6&lt;/TD&gt;&lt;TD&gt;&lt;STRONG&gt;6&lt;/STRONG&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For the first entry, it first sums the number of apples, oranges, and bananas (for the market venue).&lt;/P&gt;&lt;P&gt;For the second entry it sums the oranges and bananas (for the market venue).&lt;/P&gt;&lt;P&gt;And the third entry sums the bananas (for the market venue).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This process then repeats for each type of venue.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks in advance for the help!&lt;/P&gt;</description>
      <pubDate>Tue, 25 Jan 2022 20:49:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-a-column-that-sums-by-one-less-variable-each-time/m-p/792318#M253841</guid>
      <dc:creator>Cruiser13</dc:creator>
      <dc:date>2022-01-25T20:49:06Z</dc:date>
    </item>
    <item>
      <title>Re: How to create a column that sums by one less variable each time?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-a-column-that-sums-by-one-less-variable-each-time/m-p/792322#M253842</link>
      <description>&lt;P&gt;What code have you tried?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It looks like if you reverse the sort order, WANT is a running sum of NUMBER.&lt;/P&gt;</description>
      <pubDate>Tue, 25 Jan 2022 20:59:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-a-column-that-sums-by-one-less-variable-each-time/m-p/792322#M253842</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2022-01-25T20:59:23Z</dc:date>
    </item>
    <item>
      <title>Re: How to create a column that sums by one less variable each time?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-a-column-that-sums-by-one-less-variable-each-time/m-p/792323#M253843</link>
      <description>&lt;P&gt;Try double DoW-loop, of course I assume data are properly sorted:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data Have;
  input venue	$ fruit	$ number want;
cards;
market	apple	3	12
market	orange	2	9
market	banana	7	7
store	apple	4	11
store	orange	1	7
store	banana	6	6
run;


data Want;
  sum = 0;
  do _N_ = 1 by 1 until(last.venue);
    set have(drop = want);
    by venue;
    sum + number;
  end;


  do _N_ = 0 to _N_ - 1;
    set have(drop = want);

    ln = lag(number);

    if _N_ then want = want - ln;
           else want = sum;
    output;
  end;

  keep venue fruit number want;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Bart&lt;/P&gt;</description>
      <pubDate>Tue, 25 Jan 2022 21:02:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-a-column-that-sums-by-one-less-variable-each-time/m-p/792323#M253843</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2022-01-25T21:02:53Z</dc:date>
    </item>
    <item>
      <title>Re: How to create a column that sums by one less variable each time?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-a-column-that-sums-by-one-less-variable-each-time/m-p/792332#M253844</link>
      <description>&lt;P&gt;Reverse running sum. Weird way to do it.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;1. Either reverse the data, calculate a running total and then reverse again.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;2. Calculate an overall total by venue, merge it in and subtract as you go down the rows.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;FYI - since order seems to matter here, I would have expected an order variable of some sort? Is it just alphabetical for fruit? Different orders will obviously change the resuts so something to think about.&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table want as
select *, sum(number) as total, calculated total - number as want
from have
group by venue;
quit;

&lt;/CODE&gt;&lt;/PRE&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/398984"&gt;@Cruiser13&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hello! I am trying to create the "want" column.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;TABLE border="0" cellspacing="0" cellpadding="0"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD&gt;venue&lt;/TD&gt;
&lt;TD&gt;fruit&lt;/TD&gt;
&lt;TD&gt;number&lt;/TD&gt;
&lt;TD&gt;&lt;STRONG&gt;want&lt;/STRONG&gt;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;market&lt;/TD&gt;
&lt;TD&gt;apple&lt;/TD&gt;
&lt;TD&gt;3&lt;/TD&gt;
&lt;TD&gt;&lt;STRONG&gt;12&lt;/STRONG&gt;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;market&lt;/TD&gt;
&lt;TD&gt;orange&lt;/TD&gt;
&lt;TD&gt;2&lt;/TD&gt;
&lt;TD&gt;&lt;STRONG&gt;9&lt;/STRONG&gt;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;market&lt;/TD&gt;
&lt;TD&gt;banana&lt;/TD&gt;
&lt;TD&gt;7&lt;/TD&gt;
&lt;TD&gt;&lt;STRONG&gt;7&lt;/STRONG&gt;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;store&lt;/TD&gt;
&lt;TD&gt;apple&lt;/TD&gt;
&lt;TD&gt;4&lt;/TD&gt;
&lt;TD&gt;&lt;STRONG&gt;11&lt;/STRONG&gt;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;store&lt;/TD&gt;
&lt;TD&gt;orange&lt;/TD&gt;
&lt;TD&gt;1&lt;/TD&gt;
&lt;TD&gt;&lt;STRONG&gt;7&lt;/STRONG&gt;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;store&lt;/TD&gt;
&lt;TD&gt;banana&lt;/TD&gt;
&lt;TD&gt;6&lt;/TD&gt;
&lt;TD&gt;&lt;STRONG&gt;6&lt;/STRONG&gt;&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For the first entry, it first sums the number of apples, oranges, and bananas (for the market venue).&lt;/P&gt;
&lt;P&gt;For the second entry it sums the oranges and bananas (for the market venue).&lt;/P&gt;
&lt;P&gt;And the third entry sums the bananas (for the market venue).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This process then repeats for each type of venue.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks in advance for the help!&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 25 Jan 2022 21:33:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-a-column-that-sums-by-one-less-variable-each-time/m-p/792332#M253844</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2022-01-25T21:33:22Z</dc:date>
    </item>
    <item>
      <title>Re: How to create a column that sums by one less variable each time?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-a-column-that-sums-by-one-less-variable-each-time/m-p/792344#M253846</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/35763"&gt;@yabwon&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Good approach.&amp;nbsp; Consider simplifying the bottom loop:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;want = sum;&lt;/P&gt;
&lt;P&gt;output;&lt;/P&gt;
&lt;P&gt;sum = sum - number;&lt;/P&gt;</description>
      <pubDate>Tue, 25 Jan 2022 22:23:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-a-column-that-sums-by-one-less-variable-each-time/m-p/792344#M253846</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2022-01-25T22:23:02Z</dc:date>
    </item>
    <item>
      <title>Re: How to create a column that sums by one less variable each time?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-a-column-that-sums-by-one-less-variable-each-time/m-p/792365#M253855</link>
      <description>&lt;P&gt;If&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;The data are grouped (but not necessarily in ascending or descending order) by venue.&lt;/LI&gt;
&lt;LI&gt;Each venue has exactly three fruits, with apple followed by orange followed by banana, then:&lt;/LI&gt;
&lt;/OL&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input venue $ fruit $ number     x;
datalines;
market	apple	3	12
market	orange	2	9
market	banana	7	7
store	apple	4	11
store	orange	1	7
store	banana	6	6
run;


data want;
  set have;
  by venue notsorted;
  want+number;   /*Make the total for the venue */

  /* At end of venue, reread and output records*/
  if last.venue then do until (last.venue);
    set have;          
    by venue notsorted;
    output;
    want=want-number;  
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I read in your desired resulting values as variable X, and replicated by calculating variable WANT.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Each venue is read twice.&amp;nbsp; &amp;nbsp;Note that the first pass increments WANT, and at the end of the first pass, the second pass is programmed in a DO group, initiated by the statement.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;  if last.venue then do until (last.venue);
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;In this statement the first "last.venue" is tracking the first pass of the venue, but the second "last.venue" is associated with the second pass - i.e. the second SET and BY statements.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Eddited addtion.&amp;nbsp; This program also solves the more general problem as stated in your subject line, i.e. the first obs sums the entire venue, the 2nd obs sums entire venue minus the first obs.&amp;nbsp; &amp;nbsp;And the i'th obs sum the entire venue except for obs 1 through i-1.&amp;nbsp; You then presumablhy wouldn't have to worry about the order or number of fruits.&lt;/P&gt;</description>
      <pubDate>Wed, 26 Jan 2022 06:06:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-a-column-that-sums-by-one-less-variable-each-time/m-p/792365#M253855</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2022-01-26T06:06:28Z</dc:date>
    </item>
    <item>
      <title>Re: How to create a column that sums by one less variable each time?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-a-column-that-sums-by-one-less-variable-each-time/m-p/792385#M253866</link>
      <description>&lt;P&gt;Simple double DO loop:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
do until (last.venue);
  set have;
  by venue;
  want = sum(want,number);
end;
do until (last.venue);
  set have;
  by venue;
  output;
  want = sum(want,-number);
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 26 Jan 2022 08:37:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-a-column-that-sums-by-one-less-variable-each-time/m-p/792385#M253866</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2022-01-26T08:37:05Z</dc:date>
    </item>
    <item>
      <title>Re: How to create a column that sums by one less variable each time?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-a-column-that-sums-by-one-less-variable-each-time/m-p/792410#M253878</link>
      <description>&lt;P&gt;You will have to read the data twice, e.g.:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  want=0;
  do until(last.venue);
    set have;
    by venue;
    want+number;
    end;
  do until(last.venue);
    set have;
    by venue;
    output;
    want+-number; /* subtract after outputting */
    end;
run;
  &lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 26 Jan 2022 10:20:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-a-column-that-sums-by-one-less-variable-each-time/m-p/792410#M253878</guid>
      <dc:creator>s_lassen</dc:creator>
      <dc:date>2022-01-26T10:20:26Z</dc:date>
    </item>
  </channel>
</rss>

