<?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: column wise sum positive numbers in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/column-wise-sum-positive-numbers/m-p/904801#M357450</link>
    <description>&lt;P&gt;if you want just to sum positive (negative) values from a column try this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data dsn;
input var1 var2 var3;
datalines;
1  -2   3
4   5  -6
-7  8   9
6  -1  -7
5   2   -4
-6  9   1
;
run;

data want;
  set dsn end=e;
  array A var1-var3;
  array B pos1-pos3; /* positive */
  array C neg1-neg3; /* negative, as a bonus*/
  do over A;
    B + (A&amp;lt;&amp;gt;0);
    C + (A&amp;gt;&amp;lt;0);
  end;
  if e;
  drop var:;
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, 28 Nov 2023 09:11:50 GMT</pubDate>
    <dc:creator>yabwon</dc:creator>
    <dc:date>2023-11-28T09:11:50Z</dc:date>
    <item>
      <title>column wise sum positive numbers</title>
      <link>https://communities.sas.com/t5/SAS-Programming/column-wise-sum-positive-numbers/m-p/904776#M357437</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data dsn;
input var1 var2 var3;
datalines;
1  -2   3
4   5  -6
-7  8   9
6  -1  -7
5   2   -4
-6  9   1
;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;How to sum column wise only positive rows ?&lt;/P&gt;</description>
      <pubDate>Tue, 28 Nov 2023 04:03:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/column-wise-sum-positive-numbers/m-p/904776#M357437</guid>
      <dc:creator>pavank</dc:creator>
      <dc:date>2023-11-28T04:03:15Z</dc:date>
    </item>
    <item>
      <title>Re: column wise sum positive numbers</title>
      <link>https://communities.sas.com/t5/SAS-Programming/column-wise-sum-positive-numbers/m-p/904777#M357438</link>
      <description>&lt;P&gt;What is a "positive row"?&lt;/P&gt;
&lt;P&gt;Do you mean only the positive values?&lt;/P&gt;
&lt;P&gt;Or do you want to sum the three values for an observations and then decide whether to include that observation in the overall sum?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The later is easier.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc means data=dsn sum;
 where sum(var1,var2,var3) &amp;gt; 0;
  var var1-var3;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;To do the former you will have to do a lot of the work yourself.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
 set dsn end=eof;
  array actual var1-var3;
  array sums (3) _temporary_;
  do index=1 to dim(actual);
    if actual[index] &amp;gt; 0 then sums[index] + actual[index];
  end;
  if eof then do;
    do index=1 to dim(actual);
      actual[index]=sums[index];
    end;
    output;
  end;
  drop index;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result&lt;/P&gt;
&lt;PRE&gt;Obs    var1    var2    var3

 1      16      24      13
&lt;/PRE&gt;
&lt;P&gt;It might be easier to first transpose the data.&amp;nbsp; But you would need an unique identifier for each observation.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data dsn2;
  row+1;
  set dsn;
run;

proc transpose data=dsn2 out=dsn3;
  by row;
  var var1-var3;
run;

proc means data=dsn3 sum;
  where col1 &amp;gt; 0;
  class _name_;
  var col1;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result&lt;/P&gt;
&lt;PRE&gt;The MEANS Procedure

   Analysis Variable : COL1

NAME OF
FORMER        N
VARIABLE    Obs             Sum
-------------------------------
var1          4      16.0000000

var2          4      24.0000000

var3          3      13.0000000
-------------------------------

&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 28 Nov 2023 04:14:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/column-wise-sum-positive-numbers/m-p/904777#M357438</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-11-28T04:14:37Z</dc:date>
    </item>
    <item>
      <title>Re: column wise sum positive numbers</title>
      <link>https://communities.sas.com/t5/SAS-Programming/column-wise-sum-positive-numbers/m-p/904779#M357439</link>
      <description>&lt;P&gt;Thank you for providing sample data. What also often helps to clarify a question is to show the expected result.&lt;/P&gt;
&lt;P&gt;Below code assuming you want to get the sum of positive values within a row.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input var1 var2 var3;
datalines;
1  -2   3
4   5  -6
-7  8   9
6  -1  -7
5   2   -4
-6  9   1
;

data  want(keep=var: cum_vars: sum_var)
      want_grandtotal(keep=cum_vars: grandtotal)
      ;
  set have end=last;

  array vars {*} var:;
  array cum_vars {3} 8;
  retain cum_vars;

  do i=1 to dim(vars);
    if vars[i]&amp;gt;=0 then 
      do;
        sum_var=sum(sum_var,vars[i]);
        cum_vars[i]=sum(cum_vars[i],vars[i]);
      end;
  end;
  output want;

  grandtotal+sum_var;
  if last then output want_grandtotal;
  drop i;
run;

proc print data=want;
run;
proc print data=want_grandtotal;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Patrick_0-1701148076738.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/90284iAEF0310C23C83E39/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Patrick_0-1701148076738.png" alt="Patrick_0-1701148076738.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 28 Nov 2023 05:08:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/column-wise-sum-positive-numbers/m-p/904779#M357439</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2023-11-28T05:08:05Z</dc:date>
    </item>
    <item>
      <title>Re: column wise sum positive numbers</title>
      <link>https://communities.sas.com/t5/SAS-Programming/column-wise-sum-positive-numbers/m-p/904788#M357442</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data dsn;
input var1 var2 var3;
datalines;
1  -2   3
4   5  -6
-7  8   9
6  -1  -7
5   2   -4
-6  9   1
;
run;
proc sql;
create table want as
select sum(ifn(var1&amp;gt;0,var1,0)) as var1,
       sum(ifn(var2&amp;gt;0,var2,0)) as var2,
	   sum(ifn(var3&amp;gt;0,var3,0)) as var3
from dsn;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 28 Nov 2023 05:47:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/column-wise-sum-positive-numbers/m-p/904788#M357442</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2023-11-28T05:47:52Z</dc:date>
    </item>
    <item>
      <title>Re: column wise sum positive numbers</title>
      <link>https://communities.sas.com/t5/SAS-Programming/column-wise-sum-positive-numbers/m-p/904789#M357443</link>
      <description>&lt;P&gt;If you have SAS/IML. That would be very convenient.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data dsn;
input var1 var2 var3;
datalines;
1  -2   3
4   5  -6
-7  8   9
6  -1  -7
5   2   -4
-6  9   1
;
run;
proc iml;
use dsn;
read all var _all_ into x[c=vname];
close;
want=(x#(x&amp;gt;0))[+,];
create want from want[c=vname];
append from want;
close;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 28 Nov 2023 05:53:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/column-wise-sum-positive-numbers/m-p/904789#M357443</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2023-11-28T05:53:34Z</dc:date>
    </item>
    <item>
      <title>Re: column wise sum positive numbers</title>
      <link>https://communities.sas.com/t5/SAS-Programming/column-wise-sum-positive-numbers/m-p/904801#M357450</link>
      <description>&lt;P&gt;if you want just to sum positive (negative) values from a column try this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data dsn;
input var1 var2 var3;
datalines;
1  -2   3
4   5  -6
-7  8   9
6  -1  -7
5   2   -4
-6  9   1
;
run;

data want;
  set dsn end=e;
  array A var1-var3;
  array B pos1-pos3; /* positive */
  array C neg1-neg3; /* negative, as a bonus*/
  do over A;
    B + (A&amp;lt;&amp;gt;0);
    C + (A&amp;gt;&amp;lt;0);
  end;
  if e;
  drop var:;
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, 28 Nov 2023 09:11:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/column-wise-sum-positive-numbers/m-p/904801#M357450</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2023-11-28T09:11:50Z</dc:date>
    </item>
    <item>
      <title>Re: column wise sum positive numbers</title>
      <link>https://communities.sas.com/t5/SAS-Programming/column-wise-sum-positive-numbers/m-p/905073#M357498</link>
      <description>&lt;P&gt;Hi Patrick&lt;/P&gt;
&lt;P&gt;Thank you for your solution&lt;/P&gt;</description>
      <pubDate>Wed, 29 Nov 2023 04:40:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/column-wise-sum-positive-numbers/m-p/905073#M357498</guid>
      <dc:creator>pavank</dc:creator>
      <dc:date>2023-11-29T04:40:58Z</dc:date>
    </item>
  </channel>
</rss>

