<?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: Conditional sum in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Conditional-sum/m-p/233663#M42700</link>
    <description>&lt;P&gt;The data table driven approach would involve a table listing the summing conditions and a translation of the table into a data step. Something like this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input sales qty;
datalines;
12 2
24 5
23 3
16 6
15 1
22 6
11 3
;

data conditions;
length condition $64;
input condNo condition &amp;amp;;
datalines;
1 qty&amp;gt;=1 and sales&amp;gt;=10
2 qty&amp;gt;=2 and sales&amp;gt;=15
3 qty&amp;gt;10
;

data _null_;
length var $32 line $200;
set conditions end=done;
if _n_=1 then 
    call execute("data wantTable; set have end=done;");
var = cats("sumSales_", condNo);
line = catx(" ", "if", condition, "then", var, "+ sales;");
call execute(line);
if done then 
    call execute("if done then output; keep sumSales_: ; run;");
run;

proc transpose data=wantTable out=wantList; run;

proc sql;
create table want as
select a.condNo, a.condition, b.col1 as sumSales
from conditions as a inner join wantList as b
    on a.condNo = input(scan(b._name_, 2, "_"), best.);
select * from want;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Sun, 08 Nov 2015 03:41:48 GMT</pubDate>
    <dc:creator>PGStats</dc:creator>
    <dc:date>2015-11-08T03:41:48Z</dc:date>
    <item>
      <title>Conditional sum</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditional-sum/m-p/233603#M42676</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am looking for some iterative process that allows me to get conditional sum based on certain cut off points. For example:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;if qty&amp;gt;=1 and sales&amp;gt;=10 then get sum sales&amp;nbsp;&lt;/P&gt;
&lt;P&gt;if qty&amp;gt;=2 and sales&amp;gt;=15 then get sum sales&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Any suggestions regarding this please?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Please see below a sample data set.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data have;&lt;/P&gt;
&lt;P&gt;input sales qty;&lt;BR /&gt;datalines;&lt;BR /&gt;12 2&lt;BR /&gt;24 5&lt;BR /&gt;23 3&lt;BR /&gt;16 6&lt;BR /&gt;15 1&lt;BR /&gt;22 6&lt;BR /&gt;11 3&lt;BR /&gt;;&lt;/P&gt;</description>
      <pubDate>Sat, 07 Nov 2015 04:09:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditional-sum/m-p/233603#M42676</guid>
      <dc:creator>stat_sas</dc:creator>
      <dc:date>2015-11-07T04:09:23Z</dc:date>
    </item>
    <item>
      <title>Re: Conditional sum</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditional-sum/m-p/233605#M42677</link>
      <description>&lt;P&gt;Had to guess what you want exactly. Something like this?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input sales qty;
datalines;
12 2
24 5
23 3
16 6
15 1
22 6
11 3
;

data want;
set have end=done;
if  qty&amp;gt;=1 and sales&amp;gt;=10 or
    qty&amp;gt;=2 and sales&amp;gt;=15 
        then sumSales + sales;
if done then output;
keep sumSales;
run;

proc print data=want noobs; run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 07 Nov 2015 04:38:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditional-sum/m-p/233605#M42677</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2015-11-07T04:38:09Z</dc:date>
    </item>
    <item>
      <title>Re: Conditional sum</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditional-sum/m-p/233607#M42678</link>
      <description>&lt;P&gt;Thanks PG for the input- I am trying to get sum of sales variable at various cut off points. Something like this&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;qty &amp;nbsp; &amp;nbsp;sales &amp;nbsp; sum&lt;/P&gt;
&lt;P&gt;&amp;gt;=1 &amp;nbsp; &amp;gt;=10 &amp;nbsp; 123&lt;/P&gt;
&lt;P&gt;&amp;gt;=2 &amp;nbsp; &amp;gt;=15 &amp;nbsp; 100&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I've tried in the following way:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want;&lt;BR /&gt;set have;&lt;BR /&gt;do i=1 to 7 by 1;&lt;BR /&gt;do j=10 to 25 by 5;&lt;BR /&gt;if qty&amp;gt;=i and sales&amp;gt;=j then do;&lt;BR /&gt;tot+sales;&lt;BR /&gt;leave;&lt;BR /&gt;end;&lt;BR /&gt;end;&lt;BR /&gt;end;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 07 Nov 2015 05:23:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditional-sum/m-p/233607#M42678</guid>
      <dc:creator>stat_sas</dc:creator>
      <dc:date>2015-11-07T05:23:23Z</dc:date>
    </item>
    <item>
      <title>Re: Conditional sum</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditional-sum/m-p/233656#M42695</link>
      <description>Will the &amp;gt;= signs change or will it always be greater than or equal to? If so, I might recommend a macro in this case, with a data table to drive the macro. &lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Sun, 08 Nov 2015 01:11:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditional-sum/m-p/233656#M42695</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2015-11-08T01:11:55Z</dc:date>
    </item>
    <item>
      <title>Re: Conditional sum</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditional-sum/m-p/233661#M42698</link>
      <description>&lt;P&gt;Is your data set so large that you have to process it only once? &amp;nbsp;For a small data set this should be easy:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc means data=have sum;&lt;/P&gt;
&lt;P&gt;var sales;&lt;/P&gt;
&lt;P&gt;where qty &amp;gt;= 1 and sales &amp;gt;= 10;&lt;/P&gt;
&lt;P&gt;title 'Results for qty &amp;gt;= 1 and sales &amp;gt;= 10';&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc means data=have sum;&lt;/P&gt;
&lt;P&gt;var sales;&lt;/P&gt;
&lt;P&gt;where qty &amp;gt;= 2 and sales &amp;gt;= 15;&lt;/P&gt;
&lt;P&gt;title 'Results for qty &amp;gt;= 2 and sales &amp;gt;= 15';&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;</description>
      <pubDate>Sun, 08 Nov 2015 02:31:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditional-sum/m-p/233661#M42698</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2015-11-08T02:31:23Z</dc:date>
    </item>
    <item>
      <title>Re: Conditional sum</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditional-sum/m-p/233663#M42700</link>
      <description>&lt;P&gt;The data table driven approach would involve a table listing the summing conditions and a translation of the table into a data step. Something like this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input sales qty;
datalines;
12 2
24 5
23 3
16 6
15 1
22 6
11 3
;

data conditions;
length condition $64;
input condNo condition &amp;amp;;
datalines;
1 qty&amp;gt;=1 and sales&amp;gt;=10
2 qty&amp;gt;=2 and sales&amp;gt;=15
3 qty&amp;gt;10
;

data _null_;
length var $32 line $200;
set conditions end=done;
if _n_=1 then 
    call execute("data wantTable; set have end=done;");
var = cats("sumSales_", condNo);
line = catx(" ", "if", condition, "then", var, "+ sales;");
call execute(line);
if done then 
    call execute("if done then output; keep sumSales_: ; run;");
run;

proc transpose data=wantTable out=wantList; run;

proc sql;
create table want as
select a.condNo, a.condition, b.col1 as sumSales
from conditions as a inner join wantList as b
    on a.condNo = input(scan(b._name_, 2, "_"), best.);
select * from want;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 08 Nov 2015 03:41:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditional-sum/m-p/233663#M42700</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2015-11-08T03:41:48Z</dc:date>
    </item>
    <item>
      <title>Re: Conditional sum</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditional-sum/m-p/233666#M42702</link>
      <description>&lt;P&gt;Thanks everyone for the suggestions. This is very helpful and lead me to get a solution of my problem.&lt;/P&gt;</description>
      <pubDate>Sun, 08 Nov 2015 05:39:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditional-sum/m-p/233666#M42702</guid>
      <dc:creator>stat_sas</dc:creator>
      <dc:date>2015-11-08T05:39:26Z</dc:date>
    </item>
  </channel>
</rss>

