<?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: summing according to different criteria in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/summing-according-to-different-criteria/m-p/666944#M199649</link>
    <description>&lt;P&gt;What's wrong with your SQL query? Can you give an example of the data (datastep code) that does not sum correclty?&lt;/P&gt;</description>
    <pubDate>Sat, 04 Jul 2020 14:24:45 GMT</pubDate>
    <dc:creator>s_lassen</dc:creator>
    <dc:date>2020-07-04T14:24:45Z</dc:date>
    <item>
      <title>summing according to different criteria</title>
      <link>https://communities.sas.com/t5/SAS-Programming/summing-according-to-different-criteria/m-p/666853#M199592</link>
      <description>&lt;P&gt;Hello everyone,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am struggling with a very simple problem but somehow I am not getting what I want to - to addition values in one column by creating another column, given several criteria.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I add a little extract of the dataset, in a very simplified version, we have three columns - jour (date), investor(a number of ID), position(a number).&lt;/P&gt;&lt;P&gt;I want to create another column named sumpos which would provide me with a sum of Position variable IF the investor is the same AND IF the date is the same - basically, daily positions.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The pieces of code I have tried are the following:&lt;/P&gt;&lt;P&gt;proc sql;&lt;BR /&gt;create table ptf_funds as&lt;BR /&gt;select&lt;BR /&gt;investor,&lt;BR /&gt;jour,&lt;BR /&gt;SUM(Position) as sumPosition&lt;BR /&gt;from funds_merged1&lt;BR /&gt;group by investor, jour;&lt;BR /&gt;quit;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;but this code above does not add all the values - skips some of them and I did not manage to understand which and why.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Another way I have tried is the following:&lt;/P&gt;&lt;P&gt;data atrial_funds;&lt;BR /&gt;set funds_merged1;&lt;BR /&gt;if ((investor=investor) AND (jour=jour))then sum(Position)=sumPosition;&lt;BR /&gt;else sumPosition=sumPosition;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;But I am not sure if this would work and I get an error saying that I did not define the sum as an array.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If anyone could help me, this would be great!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 03 Jul 2020 15:49:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/summing-according-to-different-criteria/m-p/666853#M199592</guid>
      <dc:creator>aiste</dc:creator>
      <dc:date>2020-07-03T15:49:37Z</dc:date>
    </item>
    <item>
      <title>Re: summing according to different criteria</title>
      <link>https://communities.sas.com/t5/SAS-Programming/summing-according-to-different-criteria/m-p/666859#M199596</link>
      <description>&lt;P&gt;I'm fairly certain you just need a proc means, unless you want a running total in your data set?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=funds_merged1;
by investor jour;
run;

proc means data=funds_merged N SUM;
by investor jour;
var position;
output out=want sum(position) = Position;
run;

&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you do want a running total you want this then:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=funds_merged1;
by investor jour;
run;

data want_running_total;
set funds_merged1;
by investor jour;
if first.jour then runningTotal = position;
else runningTotal + position;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;BY group processing allows you to group your analysis by different variables and you don't need to check for investor=investor. FYI - since SAS only processes a line at a time, investor=investor will always be true since it's referring to the same values.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 03 Jul 2020 16:17:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/summing-according-to-different-criteria/m-p/666859#M199596</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2020-07-03T16:17:35Z</dc:date>
    </item>
    <item>
      <title>Re: summing according to different criteria</title>
      <link>https://communities.sas.com/t5/SAS-Programming/summing-according-to-different-criteria/m-p/666944#M199649</link>
      <description>&lt;P&gt;What's wrong with your SQL query? Can you give an example of the data (datastep code) that does not sum correclty?&lt;/P&gt;</description>
      <pubDate>Sat, 04 Jul 2020 14:24:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/summing-according-to-different-criteria/m-p/666944#M199649</guid>
      <dc:creator>s_lassen</dc:creator>
      <dc:date>2020-07-04T14:24:45Z</dc:date>
    </item>
  </channel>
</rss>

