<?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 only if all rows value are same in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Sum-only-if-all-rows-value-are-same/m-p/752150#M236885</link>
    <description>&lt;P&gt;If you want to sum on groups indexed by col1-col3, and also on groups indexed by col1-col6, you need two separate sum variables.&lt;/P&gt;
&lt;P&gt;first.col3 implies first.col6, BTW.&lt;/P&gt;</description>
    <pubDate>Mon, 05 Jul 2021 19:16:48 GMT</pubDate>
    <dc:creator>Kurt_Bremser</dc:creator>
    <dc:date>2021-07-05T19:16:48Z</dc:date>
    <item>
      <title>Sum only if all rows value are same</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sum-only-if-all-rows-value-are-same/m-p/752028#M236828</link>
      <description>&lt;P&gt;I have a dataset as below:&lt;/P&gt;&lt;P&gt;col1 col2&amp;nbsp; &amp;nbsp;col3&amp;nbsp; col4&amp;nbsp; col5&amp;nbsp; date&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; col6&amp;nbsp; &amp;nbsp; &amp;nbsp;col7&lt;/P&gt;&lt;P&gt;&amp;nbsp; 10&amp;nbsp; &amp;nbsp; 11&amp;nbsp; &amp;nbsp; &amp;nbsp;1&amp;nbsp; &amp;nbsp; &amp;nbsp;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2&amp;nbsp; &amp;nbsp; &amp;nbsp; 04jul2021&amp;nbsp; &amp;nbsp; &amp;nbsp;11&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;100&lt;BR /&gt;&amp;nbsp; 10&amp;nbsp; &amp;nbsp; &amp;nbsp;11&amp;nbsp; &amp;nbsp; 2&amp;nbsp; &amp;nbsp; &amp;nbsp;1&amp;nbsp; &amp;nbsp; &amp;nbsp; 2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;05jul2021&amp;nbsp; &amp;nbsp; &amp;nbsp;12&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 50&lt;BR /&gt;&amp;nbsp; 10&amp;nbsp; &amp;nbsp; &amp;nbsp;11&amp;nbsp; &amp;nbsp; 2&amp;nbsp; &amp;nbsp; &amp;nbsp;1&amp;nbsp; &amp;nbsp; &amp;nbsp; 2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;05jul2021&amp;nbsp; &amp;nbsp; &amp;nbsp;13&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;100&lt;/P&gt;&lt;P&gt;&amp;nbsp; 11&amp;nbsp; &amp;nbsp; &amp;nbsp; 1&amp;nbsp; &amp;nbsp; &amp;nbsp;1&amp;nbsp; &amp;nbsp; &amp;nbsp; 1&amp;nbsp; &amp;nbsp; &amp;nbsp;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;01Jul2021&amp;nbsp; &amp;nbsp; &amp;nbsp;14&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;200&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp;11&amp;nbsp; &amp;nbsp; &amp;nbsp;1&amp;nbsp; &amp;nbsp; &amp;nbsp; 1&amp;nbsp; &amp;nbsp; &amp;nbsp;1&amp;nbsp; &amp;nbsp; &amp;nbsp;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;01jul12021&amp;nbsp; &amp;nbsp; 14&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 200&lt;/P&gt;&lt;P&gt;I want to sum the value in the col7 by col1, col2,col3,col4,col5,date,col6&amp;nbsp; and the resultant should be as below:&lt;/P&gt;&lt;P&gt;col1 col2&amp;nbsp; &amp;nbsp;col3&amp;nbsp; col4&amp;nbsp; col5&amp;nbsp; date&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; col6&amp;nbsp; &amp;nbsp; &amp;nbsp;col7&lt;/P&gt;&lt;P&gt;&amp;nbsp; 10&amp;nbsp; &amp;nbsp; 11&amp;nbsp; &amp;nbsp; &amp;nbsp;1&amp;nbsp; &amp;nbsp; &amp;nbsp;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2&amp;nbsp; &amp;nbsp; &amp;nbsp; 04jul2021&amp;nbsp; &amp;nbsp; &amp;nbsp;11&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;100&lt;BR /&gt;&amp;nbsp; 10&amp;nbsp; &amp;nbsp; &amp;nbsp;11&amp;nbsp; &amp;nbsp; 2&amp;nbsp; &amp;nbsp; &amp;nbsp;1&amp;nbsp; &amp;nbsp; &amp;nbsp; 2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;05jul2021&amp;nbsp; &amp;nbsp; &amp;nbsp;12&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 50&lt;BR /&gt;&amp;nbsp; 10&amp;nbsp; &amp;nbsp; &amp;nbsp;11&amp;nbsp; &amp;nbsp; 2&amp;nbsp; &amp;nbsp; &amp;nbsp;1&amp;nbsp; &amp;nbsp; &amp;nbsp; 2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;05jul2021&amp;nbsp; &amp;nbsp; &amp;nbsp;13&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;100&lt;/P&gt;&lt;P&gt;&amp;nbsp; 11&amp;nbsp; &amp;nbsp; &amp;nbsp; 1&amp;nbsp; &amp;nbsp; &amp;nbsp;1&amp;nbsp; &amp;nbsp; &amp;nbsp; 1&amp;nbsp; &amp;nbsp; &amp;nbsp;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;01jul2021&amp;nbsp; &amp;nbsp; &amp;nbsp; 14&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;400&lt;BR /&gt;if in above example as Col6 is different in the line 2 and 3 so col7 should not be summed(so two lines). Where as line 4 and 5 are same so it shoul be summed and in one line&amp;nbsp;&lt;BR /&gt;&amp;nbsp;How can I achieve in SAS Base?&lt;/P&gt;</description>
      <pubDate>Mon, 05 Jul 2021 07:54:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sum-only-if-all-rows-value-are-same/m-p/752028#M236828</guid>
      <dc:creator>Jayaditya</dc:creator>
      <dc:date>2021-07-05T07:54:45Z</dc:date>
    </item>
    <item>
      <title>Re: Sum only if all rows value are same</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sum-only-if-all-rows-value-are-same/m-p/752032#M236830</link>
      <description>&lt;P&gt;Like this (untested as no usable data was provided)?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
 &amp;nbsp;select COL1, COL2, COL3, COL4, COL5, DATE, COL6, sum(COL7) as COL7
 &amp;nbsp;from TABLE&amp;nbsp;
 &amp;nbsp;group by COL1,COL2,COL3, COL4, COL5, DATE, COL6;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 05 Jul 2021 08:19:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sum-only-if-all-rows-value-are-same/m-p/752032#M236830</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2021-07-05T08:19:57Z</dc:date>
    </item>
    <item>
      <title>Re: Sum only if all rows value are same</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sum-only-if-all-rows-value-are-same/m-p/752035#M236831</link>
      <description>&lt;P&gt;Hi Kris,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;It does not give me the result I am looking for. I attached the input dataset as CSV. I am new to SAS. I tried as below.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data new;&lt;/P&gt;&lt;P&gt;set input (rename = (col7=AMT_ORIG));&lt;/P&gt;&lt;P&gt;by col1,col2,col3,col4,col5,col6;&lt;/P&gt;&lt;P&gt;length amt 8;&lt;/P&gt;&lt;P&gt;if(first.col4) then amt = coalesce(amt_orig,0);&lt;/P&gt;&lt;P&gt;else amt = sum(amt_orig,amt);&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Please help me with this.&lt;/P&gt;</description>
      <pubDate>Mon, 05 Jul 2021 08:41:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sum-only-if-all-rows-value-are-same/m-p/752035#M236831</guid>
      <dc:creator>Jayaditya</dc:creator>
      <dc:date>2021-07-05T08:41:20Z</dc:date>
    </item>
    <item>
      <title>Re: Sum only if all rows value are same</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sum-only-if-all-rows-value-are-same/m-p/752037#M236832</link>
      <description>&lt;P&gt;&lt;EM&gt;&amp;gt; It does not give me the result I am looking for.&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;How? What's not as you want in the result? It matches your description as I understand it.&lt;/P&gt;
&lt;P&gt;Please provide some data as code, either a data step or SQL.&lt;/P&gt;</description>
      <pubDate>Mon, 05 Jul 2021 08:50:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sum-only-if-all-rows-value-are-same/m-p/752037#M236832</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2021-07-05T08:50:13Z</dc:date>
    </item>
    <item>
      <title>Re: Sum only if all rows value are same</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sum-only-if-all-rows-value-are-same/m-p/752056#M236838</link>
      <description>&lt;P&gt;Maxim 2: Read the Log.&lt;/P&gt;
&lt;PRE&gt; 73         data new;
 74         set input (rename = (col7=AMT_ORIG));
 75         by col1,col2,col3,col4,col5,col6;
                   _
                   22
                   76
 ERROR 22-322: Syntaxfehler, erwartet wird eines der folgenden: ein Name, -, :, ;, DESCENDING, GROUPFORMAT, NOTSORTED, _ALL_, 
               _CHARACTER_, _CHAR_, _NUMERIC_.  
 
 ERROR 76-322: Syntax error, statement will be ignored.
 
 76         length amt 8;
 77         if(first.col4) then amt = coalesce(amt_orig,0);
 78         else amt = sum(amt_orig,amt);
 79         run;
 
 NOTE: The SAS System stopped processing this step because of errors.
 WARNING: The data set WORK.NEW may be incomplete.  When this step was stopped there were 0 observations and 9 variables.
 WARNING: Datei WORK.NEW wurde nicht ersetzt, da da dieser Schritt angehalten wurde.&lt;/PRE&gt;
&lt;P&gt;The position of the ERROR marker alerts you to the syntactically wrong element, which is the comma. Commas are not allowed in BY statements. You can also find this out by reading the documentation (Maxim 1) of the&amp;nbsp;&lt;A href="https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.4/lestmtsref/p0yeyftk8ftuckn1o5qzy53284gz.htm" target="_blank" rel="noopener"&gt;BY Statement&lt;/A&gt;.&lt;/P&gt;
&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/16961"&gt;@ChrisNZ&lt;/a&gt;&amp;nbsp;'s code works and produces the intended result:&lt;/P&gt;
&lt;PRE&gt;col1	col2	col3	col4	col5	date	col6	COL7
10	11	1	1	2	2021-07-04	11	100
10	11	2	1	2	2021-07-05	12	50
10	11	2	1	2	2021-07-05	13	100
11	1	1	1	1	2021-07-01	14	400
&lt;/PRE&gt;
&lt;P&gt;Your code needs to be fixed and expanded with a RETAIN statement, or the value of amt will not be kept across observations; you also need a subsetting IF to only keep the last observation of a group, and use the correct FIRST. and LAST. variables:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data new;
set input (rename = (col7=AMT_ORIG));
by col1 col2 col3 col4 col5 date col6;
retain amt;
length amt 8;
if first.col6 then amt = coalesce(amt_orig,0);
else amt = sum(amt_orig,amt);
if last.col6;
run;

proc print data=new noobs;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result:&lt;/P&gt;
&lt;PRE&gt;col1	col2	col3	col4	col5	date	col6	AMT_ORIG	amt
10	11	1	1	2	2021-07-04	11	100	100
10	11	2	1	2	2021-07-05	12	50	50
10	11	2	1	2	2021-07-05	13	100	100
11	1	1	1	1	2021-07-01	14	200	400&lt;/PRE&gt;
&lt;P&gt;But you can refine your code by using a &lt;A href="https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.4/lestmtsref/n1dfiqj146yi2cn1maeju9wo7ijs.htm" target="_blank" rel="noopener"&gt;SUM statement&lt;/A&gt;, which implies the retain. And you do not need to rename col7, as you won't need it after summing:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data new;
set input;
by col1 col2 col3 col4 col5 date col6;
length amt 8;
if first.col6
then amt = col7;
else amt + col7;
if last.col6;
drop col7;
run;

proc print data=new noobs;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result:&lt;/P&gt;
&lt;PRE&gt;col1	col2	col3	col4	col5	date	col6	amt
10	11	1	1	2	2021-07-04	11	100
10	11	2	1	2	2021-07-05	12	50
10	11	2	1	2	2021-07-05	13	100
11	1	1	1	1	2021-07-01	14	400
&lt;/PRE&gt;
&lt;P&gt;But why don't you use PROC SUMMARY, which is built for this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc summary data=input nway;
class col1 col2 col3 col4 col5 date col6;
output
  out=want (drop=_:)
  sum(col7)=amt
;
run;

proc print data=want noobs;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result:&lt;/P&gt;
&lt;PRE&gt;col1	col2	col3	col4	col5	date	col6	amt
10	11	1	1	2	2021-07-04	11	100
10	11	2	1	2	2021-07-05	12	50
10	11	2	1	2	2021-07-05	13	100
11	1	1	1	1	2021-07-01	14	400
&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 05 Jul 2021 11:24:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sum-only-if-all-rows-value-are-same/m-p/752056#M236838</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2021-07-05T11:24:42Z</dc:date>
    </item>
    <item>
      <title>Re: Sum only if all rows value are same</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sum-only-if-all-rows-value-are-same/m-p/752118#M236867</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt;: Thanks for the reply.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If I also want to check on col3 as well. So can I use the condition as below:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Because first, I have to check on col3 and also on col6 and do the cumulative sum.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Would you please help me with this? or is it impossible, and I have to perform this action in a separate step?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="sas"&gt;if first.col6 or first.col3 then amt = coalesce(amt_orig,0);
else amt = sum(amt_orig,amt);
if last.col6;
run;&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 05 Jul 2021 16:28:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sum-only-if-all-rows-value-are-same/m-p/752118#M236867</guid>
      <dc:creator>Jayaditya</dc:creator>
      <dc:date>2021-07-05T16:28:53Z</dc:date>
    </item>
    <item>
      <title>Re: Sum only if all rows value are same</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sum-only-if-all-rows-value-are-same/m-p/752150#M236885</link>
      <description>&lt;P&gt;If you want to sum on groups indexed by col1-col3, and also on groups indexed by col1-col6, you need two separate sum variables.&lt;/P&gt;
&lt;P&gt;first.col3 implies first.col6, BTW.&lt;/P&gt;</description>
      <pubDate>Mon, 05 Jul 2021 19:16:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sum-only-if-all-rows-value-are-same/m-p/752150#M236885</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2021-07-05T19:16:48Z</dc:date>
    </item>
    <item>
      <title>Re: Sum only if all rows value are same</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sum-only-if-all-rows-value-are-same/m-p/752205#M236919</link>
      <description>&lt;P&gt;Thanks for clarifying. I was able to solve and proceed further.&lt;/P&gt;</description>
      <pubDate>Tue, 06 Jul 2021 05:19:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sum-only-if-all-rows-value-are-same/m-p/752205#M236919</guid>
      <dc:creator>Jayaditya</dc:creator>
      <dc:date>2021-07-06T05:19:42Z</dc:date>
    </item>
  </channel>
</rss>

