<?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: calculate sum of fields with calculate field in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/calculate-sum-of-fields-with-calculate-field/m-p/922675#M363321</link>
    <description>&lt;P&gt;Thank you but the solution provided is in two steps of calculation-&lt;/P&gt;
&lt;P&gt;First you calculate the field X_calc&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Then you calculate TOTAL&lt;/P&gt;
&lt;P&gt;My question is if can do it in one step&amp;nbsp; so the calculation of field X_Calc be inside the SUM function??&lt;/P&gt;</description>
    <pubDate>Wed, 03 Apr 2024 07:19:38 GMT</pubDate>
    <dc:creator>Ronein</dc:creator>
    <dc:date>2024-04-03T07:19:38Z</dc:date>
    <item>
      <title>calculate sum of fields with calculate field</title>
      <link>https://communities.sas.com/t5/SAS-Programming/calculate-sum-of-fields-with-calculate-field/m-p/922670#M363316</link>
      <description>&lt;P&gt;Hello&lt;/P&gt;
&lt;P&gt;I want to calculate sum of fields .&lt;/P&gt;
&lt;P&gt;One of the fields in the sum is calculated variable.&lt;/P&gt;
&lt;P&gt;Is there a way to calculate it in one step with SUM function?&lt;/P&gt;
&lt;P&gt;This way is not working&amp;nbsp;&lt;/P&gt;
&lt;P&gt;SUM(case when X&amp;lt;0 then abs(X) else 0 end as calc_X,Y,Z) as Total_Other_Way&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data have;
Input Cust_ID X Y Z;
cards;
1 -100 50 60
2 200 60 40
3 -300 30 20
4 400 10 90
5 250 80 20
;
Run;

proc sql;
create table want as
select *, 
       case when X&amp;lt;0 then abs(X) else 0 end as calc_X,
	   SUM(calculated calc_X,Y,Z) as Total,
	   SUM(case when X&amp;lt;0 then abs(X) else 0 end as calc_X,Y,Z) as Total_Other_Way
from have
;
quit;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 03 Apr 2024 06:39:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/calculate-sum-of-fields-with-calculate-field/m-p/922670#M363316</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2024-04-03T06:39:05Z</dc:date>
    </item>
    <item>
      <title>Re: calculate sum of fields with calculate field</title>
      <link>https://communities.sas.com/t5/SAS-Programming/calculate-sum-of-fields-with-calculate-field/m-p/922673#M363319</link>
      <description>&lt;P&gt;I got no problem.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data have;
Input Cust_ID X Y Z;
cards;
1 -100 50 60
2 200 60 40
3 -300 30 20
4 400 10 90
5 250 80 20
;
Run;

proc sql;
create table want as
select *, 
       case when X&amp;lt;0 then abs(X) else 0 end as calc_X,
	   SUM(calculated calc_X,Y,Z) as Total
from have
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Or you could try this one:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data have;
Input Cust_ID X Y Z;
cards;
1 -100 50 60
2 200 60 40
3 -300 30 20
4 400 10 90
5 250 80 20
;
Run;

proc sql;
create table want as
select *, 
       -X*(X&amp;lt;0) as calc_X,
	   SUM(calculated calc_X,Y,Z) as Total
from have
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 03 Apr 2024 07:16:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/calculate-sum-of-fields-with-calculate-field/m-p/922673#M363319</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2024-04-03T07:16:57Z</dc:date>
    </item>
    <item>
      <title>Re: calculate sum of fields with calculate field</title>
      <link>https://communities.sas.com/t5/SAS-Programming/calculate-sum-of-fields-with-calculate-field/m-p/922674#M363320</link>
      <description>&lt;P&gt;If you want the intermediate result in a new variable, you have to do the separate calculation. If not, use the second way, but remove the "as calc_x".&lt;/P&gt;</description>
      <pubDate>Wed, 03 Apr 2024 07:19:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/calculate-sum-of-fields-with-calculate-field/m-p/922674#M363320</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2024-04-03T07:19:35Z</dc:date>
    </item>
    <item>
      <title>Re: calculate sum of fields with calculate field</title>
      <link>https://communities.sas.com/t5/SAS-Programming/calculate-sum-of-fields-with-calculate-field/m-p/922675#M363321</link>
      <description>&lt;P&gt;Thank you but the solution provided is in two steps of calculation-&lt;/P&gt;
&lt;P&gt;First you calculate the field X_calc&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Then you calculate TOTAL&lt;/P&gt;
&lt;P&gt;My question is if can do it in one step&amp;nbsp; so the calculation of field X_Calc be inside the SUM function??&lt;/P&gt;</description>
      <pubDate>Wed, 03 Apr 2024 07:19:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/calculate-sum-of-fields-with-calculate-field/m-p/922675#M363321</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2024-04-03T07:19:38Z</dc:date>
    </item>
    <item>
      <title>Re: calculate sum of fields with calculate field</title>
      <link>https://communities.sas.com/t5/SAS-Programming/calculate-sum-of-fields-with-calculate-field/m-p/922676#M363322</link>
      <description>&lt;P&gt;Great,&lt;/P&gt;
&lt;P&gt;Here is the solution as you offered&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
Data have;
Input Cust_ID X Y Z;
cards;
1 -100 50 60
2 200 60 40
3 -300 30 20
4 400 10 90
5 250 80 20
;
Run;

proc sql;
create table want as
select *, 
	   SUM(case when X&amp;lt;0 then abs(X) else 0 end,Y,Z) as Total_Other_Way
from have
;
quit;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 03 Apr 2024 07:20:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/calculate-sum-of-fields-with-calculate-field/m-p/922676#M363322</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2024-04-03T07:20:58Z</dc:date>
    </item>
  </channel>
</rss>

