<?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 up Vertically in Proc SQL in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/SUM-up-Vertically-in-Proc-SQL/m-p/318243#M69705</link>
    <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Firstly, please don't post Office files, they are a danger and I for one would not download them. &amp;nbsp;Post test in the form of a datastep in the post, help can be found here:&lt;BR /&gt;&lt;A href="https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-data-AKA-generate/ta-p/258712" target="_blank"&gt;https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-data-AKA-generate/ta-p/258712&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Secondly, your code is next to impossible to read, no good programming practices followed there. &amp;nbsp;Personally, I would think that doing this in Base SAS rather than SQL would make your code simpler and easier to read:&lt;/P&gt;
&lt;PRE&gt;data want;
  set have (where=sch_class in ('539','540','541','812','813') and upload_date &amp;gt;= '31DEC2015'd);
  by sch_class tran_type;
  retain shares_subscribed shares_redeemed;
  if sch_class in ("SWIN","SWINC","UNSAL","UNSALC") then do;
    shares_subscribed=sum(shares_subscribed,trade_units);
    ...
  end;
  if sch_class in ("SWOUT","SWOUT","UNPUR","UNPURC") then do; 
    shares_redeeemed=sum(shares_redeemed,trade_units);
    ...
  end;
  if last.tran_type then output;
run;&lt;/PRE&gt;
&lt;P&gt;Note that most of your case statements there are of no additional value to the logic, you already restrict the data to the '539','540' etc, so why add that to the case each time?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 12 Dec 2016 11:16:11 GMT</pubDate>
    <dc:creator>RW9</dc:creator>
    <dc:date>2016-12-12T11:16:11Z</dc:date>
    <item>
      <title>SUM up Vertically in Proc SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SUM-up-Vertically-in-Proc-SQL/m-p/318227#M69701</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp; I am trying sum up the columns using SUM function ang group by but it is not happening&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am attaching dummy data and code I am using cuurently please suugest me solution for the same.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;code:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;proc sql;&lt;BR /&gt;create table demo.unit_capital1 as&lt;BR /&gt;(select t1.*,&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;(case when (t1.sch_class in ('539','540','541','812','813') and t1.tran_type in ('SWIN','SWINC','UNSAL','UNSALC'))&lt;BR /&gt;then sum(t1.trades_units)&lt;BR /&gt;else 0 end) as SHARES_SUBSCRIBED,&lt;BR /&gt;(case when (t1.sch_class in ('539','540','541','812','813') and t1.tran_type in ('SWOUT','SWOUTC','UNPUR','UNPURC'))&lt;BR /&gt;then sum(t1.trades_units) else 0 end) as SHARES_REDEEMED,&lt;BR /&gt;(case when (t1.sch_class in ('539','540','541','812','813') and t1.tran_type in ('SWIN','SWINC','UNSAL','UNSALC'))&lt;BR /&gt;then sum(t1.Subscription)&lt;BR /&gt;else 0 end) as Subscription_Money,&lt;BR /&gt;(case when (t1.sch_class in ('539','540','541','812','813') and t1.tran_type in ('SWOUT','SWOUTC','UNPUR','UNPURC'))&lt;BR /&gt;then sum(t1.Subscription) else 0 end) as Redemption_Money,&lt;BR /&gt;(case when (t1.sch_class in ('539','540','541','812','813') and t1.tran_type in ('SWOUT','SWOUTC','UNPUR','UNPURC'))&lt;BR /&gt;then sum(t1.scheme_load) else 0 end) as Load_&lt;/P&gt;&lt;P&gt;from demo.unit_capital t1&lt;BR /&gt;WHERE T1.SCH_CLASS in ('539','540','541','812','813')&lt;BR /&gt;and t1.upload_date &amp;gt;= '31DEC2015'd&lt;BR /&gt;group by t1.UPLOAD_DATE,t1.sch_class,t1.tran_type)&lt;BR /&gt;order by t1.sch_class,t1.tran_type;&lt;BR /&gt;quit;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks,&lt;/P&gt;</description>
      <pubDate>Mon, 12 Dec 2016 09:12:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SUM-up-Vertically-in-Proc-SQL/m-p/318227#M69701</guid>
      <dc:creator>nishukhawale</dc:creator>
      <dc:date>2016-12-12T09:12:47Z</dc:date>
    </item>
    <item>
      <title>Re: SUM up Vertically in Proc SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SUM-up-Vertically-in-Proc-SQL/m-p/318233#M69702</link>
      <description>&lt;P&gt;The CASE should just return the value from the column. Then you use the SUM function around the CASE, this should give you the correct result.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;See also example below.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
  select
    origin
    , sum( case
      when type in ("SUV") then invoice
      else 0
      end) as suv_invocie_sum
  from
    sashelp.cars
  group by
    origin
  ;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Bruno&lt;/P&gt;</description>
      <pubDate>Mon, 12 Dec 2016 09:41:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SUM-up-Vertically-in-Proc-SQL/m-p/318233#M69702</guid>
      <dc:creator>BrunoMueller</dc:creator>
      <dc:date>2016-12-12T09:41:22Z</dc:date>
    </item>
    <item>
      <title>Re: SUM up Vertically in Proc SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SUM-up-Vertically-in-Proc-SQL/m-p/318243#M69705</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Firstly, please don't post Office files, they are a danger and I for one would not download them. &amp;nbsp;Post test in the form of a datastep in the post, help can be found here:&lt;BR /&gt;&lt;A href="https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-data-AKA-generate/ta-p/258712" target="_blank"&gt;https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-data-AKA-generate/ta-p/258712&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Secondly, your code is next to impossible to read, no good programming practices followed there. &amp;nbsp;Personally, I would think that doing this in Base SAS rather than SQL would make your code simpler and easier to read:&lt;/P&gt;
&lt;PRE&gt;data want;
  set have (where=sch_class in ('539','540','541','812','813') and upload_date &amp;gt;= '31DEC2015'd);
  by sch_class tran_type;
  retain shares_subscribed shares_redeemed;
  if sch_class in ("SWIN","SWINC","UNSAL","UNSALC") then do;
    shares_subscribed=sum(shares_subscribed,trade_units);
    ...
  end;
  if sch_class in ("SWOUT","SWOUT","UNPUR","UNPURC") then do; 
    shares_redeeemed=sum(shares_redeemed,trade_units);
    ...
  end;
  if last.tran_type then output;
run;&lt;/PRE&gt;
&lt;P&gt;Note that most of your case statements there are of no additional value to the logic, you already restrict the data to the '539','540' etc, so why add that to the case each time?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 12 Dec 2016 11:16:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SUM-up-Vertically-in-Proc-SQL/m-p/318243#M69705</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2016-12-12T11:16:11Z</dc:date>
    </item>
  </channel>
</rss>

