<?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: New User - How to sum a column based on criteria from other columns? in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/New-User-How-to-sum-a-column-based-on-criteria-from-other/m-p/525882#M5023</link>
    <description>I edited Amazon...it should be 1000. Thanks for the heads up.&lt;BR /&gt;&lt;BR /&gt;Samsung has two entries because two of the trading ids are within the range of 12 so must be added together. The third Samsung entry has a trading id which is in not in range of any other trading id so is shown alone.</description>
    <pubDate>Wed, 09 Jan 2019 21:03:03 GMT</pubDate>
    <dc:creator>ohsofaizy</dc:creator>
    <dc:date>2019-01-09T21:03:03Z</dc:date>
    <item>
      <title>New User - How to sum a column based on criteria from other columns?</title>
      <link>https://communities.sas.com/t5/New-SAS-User/New-User-How-to-sum-a-column-based-on-criteria-from-other/m-p/525867#M5021</link>
      <description>&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;Security&lt;/TD&gt;&lt;TD&gt;Trading ID&lt;/TD&gt;&lt;TD&gt;Quantity&lt;/TD&gt;&lt;TD&gt;Cash&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;Apple&lt;/TD&gt;&lt;TD&gt;560&lt;/TD&gt;&lt;TD&gt;400&lt;/TD&gt;&lt;TD&gt;10&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;Apple&lt;/TD&gt;&lt;TD&gt;561&lt;/TD&gt;&lt;TD&gt;400&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;Apple&lt;/TD&gt;&lt;TD&gt;562&lt;/TD&gt;&lt;TD&gt;200&lt;/TD&gt;&lt;TD&g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t;P&gt;Based on the table above I am trying to figure out a way in SAS 9.3 (EG 5.1) using proc SQL&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Whenever cash &amp;gt; 0 then I would like to group the security and sum 'quantity' if trading IDs are within a range of 12. Ultimately I would like this particular table to come out as:&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;Security&lt;/TD&gt;&lt;TD&gt;Quantity&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;Apple&lt;/TD&gt;&lt;TD&gt;1000&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;IBM&lt;/TD&gt;&lt;TD&gt;60&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;IBM&lt;/TD&gt;&lt;TD&gt;50&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;Amazon&lt;/TD&gt;&lt;TD&gt;1000&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;Nike&lt;/TD&gt;&lt;TD&gt;500&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;Samsung&lt;/TD&gt;&lt;TD&gt;300&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;Samsung&lt;/TD&gt;&lt;TD&gt;400&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;</description>
      <pubDate>Wed, 09 Jan 2019 21:01:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/New-User-How-to-sum-a-column-based-on-criteria-from-other/m-p/525867#M5021</guid>
      <dc:creator>ohsofaizy</dc:creator>
      <dc:date>2019-01-09T21:01:13Z</dc:date>
    </item>
    <item>
      <title>Re: New User - How to sum a column based on criteria from other columns?</title>
      <link>https://communities.sas.com/t5/New-SAS-User/New-User-How-to-sum-a-column-based-on-criteria-from-other/m-p/525878#M5022</link>
      <description>&lt;P&gt;Why is Amazon 1500? What have you tried so far, what does your SQL look like?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;EDIT: And why is Samsung in with two enteries, when one is 0?&lt;/P&gt;</description>
      <pubDate>Wed, 09 Jan 2019 21:00:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/New-User-How-to-sum-a-column-based-on-criteria-from-other/m-p/525878#M5022</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2019-01-09T21:00:36Z</dc:date>
    </item>
    <item>
      <title>Re: New User - How to sum a column based on criteria from other columns?</title>
      <link>https://communities.sas.com/t5/New-SAS-User/New-User-How-to-sum-a-column-based-on-criteria-from-other/m-p/525882#M5023</link>
      <description>I edited Amazon...it should be 1000. Thanks for the heads up.&lt;BR /&gt;&lt;BR /&gt;Samsung has two entries because two of the trading ids are within the range of 12 so must be added together. The third Samsung entry has a trading id which is in not in range of any other trading id so is shown alone.</description>
      <pubDate>Wed, 09 Jan 2019 21:03:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/New-User-How-to-sum-a-column-based-on-criteria-from-other/m-p/525882#M5023</guid>
      <dc:creator>ohsofaizy</dc:creator>
      <dc:date>2019-01-09T21:03:03Z</dc:date>
    </item>
    <item>
      <title>Re: New User - How to sum a column based on criteria from other columns?</title>
      <link>https://communities.sas.com/t5/New-SAS-User/New-User-How-to-sum-a-column-based-on-criteria-from-other/m-p/525885#M5024</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/254310"&gt;@ohsofaizy&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;I edited Amazon...it should be 1000. Thanks for the heads up.&lt;BR /&gt;&lt;BR /&gt;Samsung has two entries because two of the trading ids are within the range of 12 so must be added together. The third Samsung entry has a trading id which is in not in range of any other trading id so is shown alone.&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;What about that cash&amp;gt;0 criteria mentioned in your original post?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;&lt;SPAN&gt;Whenever cash &amp;gt; 0&lt;/SPAN&gt;&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;</description>
      <pubDate>Wed, 09 Jan 2019 21:07:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/New-User-How-to-sum-a-column-based-on-criteria-from-other/m-p/525885#M5024</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2019-01-09T21:07:21Z</dc:date>
    </item>
    <item>
      <title>Re: New User - How to sum a column based on criteria from other columns?</title>
      <link>https://communities.sas.com/t5/New-SAS-User/New-User-How-to-sum-a-column-based-on-criteria-from-other/m-p/525889#M5025</link>
      <description>&lt;P&gt;Since the cash is greater than 0 (10 in this case), and there are no trading ids within a range of 12 the quantity of 400 will remain.&lt;BR /&gt;&lt;BR /&gt;Ultimately the sum of the "quantity" column should still be the same in both tables.&lt;/P&gt;</description>
      <pubDate>Wed, 09 Jan 2019 21:13:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/New-User-How-to-sum-a-column-based-on-criteria-from-other/m-p/525889#M5025</guid>
      <dc:creator>ohsofaizy</dc:creator>
      <dc:date>2019-01-09T21:13:54Z</dc:date>
    </item>
    <item>
      <title>Re: New User - How to sum a column based on criteria from other columns?</title>
      <link>https://communities.sas.com/t5/New-SAS-User/New-User-How-to-sum-a-column-based-on-criteria-from-other/m-p/525891#M5026</link>
      <description>&lt;P&gt;This should get you started, I'll leave the final steps up to you.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You should double check the join criteria, particularly the 12 boundary&amp;nbsp;value, depending on exactly how that's specified it may need to be 11 since the BETWEEN operator includes the boundary values.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;*create sample data to work with;
data have;
informat security $30.;
input Security	$ TradingID	Quantity	Cash;
cards;
Apple	560	400	10
Apple	561	400	0
Apple	562	200	0
IBM	987	60	10
IBM	435	50	5
Amazon	755	500	10
Amazon	756	500	0
Nike	757	500	0
Samsung	982	100	10
Samsung	983	200	0
Samsung	122	400	10
;;;;
run;


*Code to join on itself;
proc sql;
create table summary as 
select h1.security, sum(h2.quantity) as total
from have as h1
left join have as h2
on h1.security = h2.security
/*Trading ID within 12*/
and h1.tradingID between h2.tradingID - 12 and h2.tradingID + 12
where h1.cash&amp;gt;0
group by h1.security, h1.TradingID;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 09 Jan 2019 21:13:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/New-User-How-to-sum-a-column-based-on-criteria-from-other/m-p/525891#M5026</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2019-01-09T21:13:53Z</dc:date>
    </item>
    <item>
      <title>Re: New User - How to sum a column based on criteria from other columns?</title>
      <link>https://communities.sas.com/t5/New-SAS-User/New-User-How-to-sum-a-column-based-on-criteria-from-other/m-p/525892#M5027</link>
      <description>Thank you! Let me take a look at this and see if I can figure it out.&lt;BR /&gt;&lt;BR /&gt;Thanks!</description>
      <pubDate>Wed, 09 Jan 2019 21:16:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/New-User-How-to-sum-a-column-based-on-criteria-from-other/m-p/525892#M5027</guid>
      <dc:creator>ohsofaizy</dc:creator>
      <dc:date>2019-01-09T21:16:07Z</dc:date>
    </item>
  </channel>
</rss>

