<?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: sum group by in if then do in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/sum-group-by-in-if-then-do/m-p/690012#M209833</link>
    <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;COL1+COL2;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;is a short way to write :&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;retain COL1;

COL1=sum(COL1, COL2);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;So the leftmost operand should be the name of the column that will contain the summation result.&lt;/P&gt;</description>
    <pubDate>Thu, 08 Oct 2020 14:31:05 GMT</pubDate>
    <dc:creator>gamotte</dc:creator>
    <dc:date>2020-10-08T14:31:05Z</dc:date>
    <item>
      <title>sum group by in if then do</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sum-group-by-in-if-then-do/m-p/689980#M209818</link>
      <description>&lt;P&gt;Hi I hope you're all well,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a simple problem yet quite disturbing as I don't know what happens exactly and why don't I get the results wanted, to put you in the context let's suppose you have a table with variables like this ;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;project&amp;nbsp;&amp;nbsp;&amp;nbsp; company&amp;nbsp;&amp;nbsp;&amp;nbsp; category&amp;nbsp;&amp;nbsp;&amp;nbsp; ROI&amp;nbsp;&amp;nbsp;&amp;nbsp; contribution&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;My aim is quite simple, i wanna sum my contibs according to certain companies by project and category ( i'm sorry i can't give you the right names nor values of my table for confidentiality purposes ).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Now since i want only certain clients sums I did this : ( assume the company is Shelbey LTD )&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=table2; by  project category ;run;

options mprint;
data table3;
set table2;
Company = scan(project,1,'_'); /* from here i get my company name */
by project category;
if Company = "SHELBEY LTD" then do;
	if First.contrib then contrib2=0;
		contrib + contrib2;
	if Last.contrib;
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Well, in the results all my companies are there with no value in contrib2 ( which is normal considering the fact that i "selected" only the shelbey company ) but all the shelbey observations disappeard !&lt;/P&gt;&lt;P&gt;It's quite weird, this the first time i encounter this kind of difficulty.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;here is the message i got from the log&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;NOTE: Variable 'First.contrib'n is uninitialized.
NOTE: Variable 'Last.contrib'n is uninitialized.
NOTE: There were 1052 observations read from the data set WORK.TABLE2.
NOTE: The data set WORK.TABLE3 has 860 observations and 13 variables.
NOTE: Compressing data set WORK.TABLE3 decreased size by 83.33 percent. 
      Compressed is 2 pages; un-compressed would require 12 pages.
NOTE: DATA statement used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Please Help if you can.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks in advance and stay safe &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 08 Oct 2020 13:57:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sum-group-by-in-if-then-do/m-p/689980#M209818</guid>
      <dc:creator>skavli</dc:creator>
      <dc:date>2020-10-08T13:57:59Z</dc:date>
    </item>
    <item>
      <title>Re: sum group by in if then do</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sum-group-by-in-if-then-do/m-p/689989#M209823</link>
      <description>Hello, &lt;BR /&gt;You use first.contrib and last.contrib but contrib is not in your by statement.</description>
      <pubDate>Thu, 08 Oct 2020 14:02:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sum-group-by-in-if-then-do/m-p/689989#M209823</guid>
      <dc:creator>gamotte</dc:creator>
      <dc:date>2020-10-08T14:02:03Z</dc:date>
    </item>
    <item>
      <title>Re: sum group by in if then do</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sum-group-by-in-if-then-do/m-p/689999#M209826</link>
      <description>&lt;P&gt;I think you should use first.category and last.category instead of first.contrib and last.contrib.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I guess you probably also want to change :&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;contrib+contrib2;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;into&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;contrib2+contrib;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;That is contrib2 is the sum of contrib column for the current project/category.&lt;/P&gt;</description>
      <pubDate>Thu, 08 Oct 2020 14:13:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sum-group-by-in-if-then-do/m-p/689999#M209826</guid>
      <dc:creator>gamotte</dc:creator>
      <dc:date>2020-10-08T14:13:57Z</dc:date>
    </item>
    <item>
      <title>Re: sum group by in if then do</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sum-group-by-in-if-then-do/m-p/690003#M209827</link>
      <description>why is the second order important ?</description>
      <pubDate>Thu, 08 Oct 2020 14:22:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sum-group-by-in-if-then-do/m-p/690003#M209827</guid>
      <dc:creator>skavli</dc:creator>
      <dc:date>2020-10-08T14:22:35Z</dc:date>
    </item>
    <item>
      <title>Re: sum group by in if then do</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sum-group-by-in-if-then-do/m-p/690008#M209831</link>
      <description>Oh yes, I realized that i have loop over the occurences in my by group not the values that corresponds to it thanks !</description>
      <pubDate>Thu, 08 Oct 2020 14:26:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sum-group-by-in-if-then-do/m-p/690008#M209831</guid>
      <dc:creator>skavli</dc:creator>
      <dc:date>2020-10-08T14:26:25Z</dc:date>
    </item>
    <item>
      <title>Re: sum group by in if then do</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sum-group-by-in-if-then-do/m-p/690012#M209833</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;COL1+COL2;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;is a short way to write :&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;retain COL1;

COL1=sum(COL1, COL2);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;So the leftmost operand should be the name of the column that will contain the summation result.&lt;/P&gt;</description>
      <pubDate>Thu, 08 Oct 2020 14:31:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sum-group-by-in-if-then-do/m-p/690012#M209833</guid>
      <dc:creator>gamotte</dc:creator>
      <dc:date>2020-10-08T14:31:05Z</dc:date>
    </item>
  </channel>
</rss>

