<?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: Case when multiple conditions to create aggregated value in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Case-when-multiple-conditions-to-create-aggregated-value/m-p/678409#M204741</link>
    <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/292396"&gt;@David_Billa&lt;/a&gt;&amp;nbsp;If the issue is resolved please close the issue.&amp;nbsp;&lt;BR /&gt;If not please provide us some test data.&lt;/P&gt;</description>
    <pubDate>Fri, 21 Aug 2020 10:52:09 GMT</pubDate>
    <dc:creator>Satish_Parida</dc:creator>
    <dc:date>2020-08-21T10:52:09Z</dc:date>
    <item>
      <title>Case when multiple conditions to create aggregated value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Case-when-multiple-conditions-to-create-aggregated-value/m-p/678381#M204724</link>
      <description>&lt;P&gt;I would like to understand whether code below logically is right. Because it is not producing the desired results. I want to create a variable 'CR_Ratio' which is equal to (CR/sum(Yearly_Premium) when it statisfies the condition&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" size="3"&gt;Contract=&lt;/FONT&gt;&lt;FONT face="Courier New" size="3" color="#800080"&gt;'yes'&lt;/FONT&gt; &lt;FONT face="Courier New" size="3" color="#0000ff"&gt;and&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; Start_CR=&lt;/FONT&gt;&lt;FONT face="Courier New" size="3" color="#800080"&gt;'no'&lt;/FONT&gt; &lt;FONT face="Courier New" size="3" color="#0000ff"&gt;and&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; year_start=&lt;/FONT&gt;&lt;FONT face="Courier New" size="3" color="#800080"&gt;'yes'&lt;/FONT&gt; otherwise I want '0'&lt;/P&gt;
&lt;P&gt;&lt;LI-WRAPPER&gt;&lt;/LI-WRAPPER&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;proc sql;
   create table want  as
   select * ,(case when (Contract='yes' and Start_CR='no' and Full_year_start='yes')
            then (CR/sum(Yearly_Premium
            else 0)) 
            end) as CR_Ratio length = 8
            format = 21.4
from have
group by
      Segment,
      Company
;
quit;&lt;/PRE&gt;
&lt;P&gt;what are other ways to tackle it apart from case when?&lt;/P&gt;</description>
      <pubDate>Fri, 21 Aug 2020 09:08:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Case-when-multiple-conditions-to-create-aggregated-value/m-p/678381#M204724</guid>
      <dc:creator>David_Billa</dc:creator>
      <dc:date>2020-08-21T09:08:32Z</dc:date>
    </item>
    <item>
      <title>Re: Case when multiple conditions to create aggregated value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Case-when-multiple-conditions-to-create-aggregated-value/m-p/678384#M204727</link>
      <description>&lt;P&gt;Please supply an example for have, point out where the result does not meet your expectations, and show what you expected.&lt;/P&gt;</description>
      <pubDate>Fri, 21 Aug 2020 09:14:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Case-when-multiple-conditions-to-create-aggregated-value/m-p/678384#M204727</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-08-21T09:14:22Z</dc:date>
    </item>
    <item>
      <title>Re: Case when multiple conditions to create aggregated value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Case-when-multiple-conditions-to-create-aggregated-value/m-p/678386#M204729</link>
      <description>Before I provide the sample data, can I understand that the provided&lt;BR /&gt;program matches with the conditions in ' case when ' to create new&lt;BR /&gt;variable?&lt;BR /&gt;</description>
      <pubDate>Fri, 21 Aug 2020 09:24:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Case-when-multiple-conditions-to-create-aggregated-value/m-p/678386#M204729</guid>
      <dc:creator>David_Billa</dc:creator>
      <dc:date>2020-08-21T09:24:21Z</dc:date>
    </item>
    <item>
      <title>Re: Case when multiple conditions to create aggregated value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Case-when-multiple-conditions-to-create-aggregated-value/m-p/678403#M204737</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/292396"&gt;@David_Billa&lt;/a&gt;&amp;nbsp; You misplaced few parenthesis. Here is the code you need.&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
   create table want  as
   select * ,(case when (Contract='yes' and Start_CR='no' and Full_year_start='yes') 
					then CR/sum(Yearly_Premium)   
					else 0)      
			  end) as CR_Ratio length = 8 format = 21.4
from have
group by
      Segment,
      Company
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 21 Aug 2020 10:16:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Case-when-multiple-conditions-to-create-aggregated-value/m-p/678403#M204737</guid>
      <dc:creator>Satish_Parida</dc:creator>
      <dc:date>2020-08-21T10:16:36Z</dc:date>
    </item>
    <item>
      <title>Re: Case when multiple conditions to create aggregated value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Case-when-multiple-conditions-to-create-aggregated-value/m-p/678409#M204741</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/292396"&gt;@David_Billa&lt;/a&gt;&amp;nbsp;If the issue is resolved please close the issue.&amp;nbsp;&lt;BR /&gt;If not please provide us some test data.&lt;/P&gt;</description>
      <pubDate>Fri, 21 Aug 2020 10:52:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Case-when-multiple-conditions-to-create-aggregated-value/m-p/678409#M204741</guid>
      <dc:creator>Satish_Parida</dc:creator>
      <dc:date>2020-08-21T10:52:09Z</dc:date>
    </item>
    <item>
      <title>Re: Case when multiple conditions to create aggregated value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Case-when-multiple-conditions-to-create-aggregated-value/m-p/678411#M204742</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/138619"&gt;@Satish_Parida&lt;/a&gt;&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt;&amp;nbsp;I don't think I've misplaced any paranthesis in my post. Code works fine. I tried with your proposed code also but still the data is not matching to my desired results.&lt;/P&gt;
&lt;P&gt;Sample data:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data have;
    input Branch $ Sub_Segment $ Currency $ Yearly_Premium Contract $ Start_CR $  End_CR $ Full_year_Start RC;
    datalines;
1831234 7183_AS1    EUR 2533629   yes no  no  yes 131169336
1831234 7183_AS1    EUR 11643516  yes no  no  yes 131169336
;
run;&lt;/PRE&gt;
&lt;P&gt;Desired result is, I want all the rows and observations from the dataset 'have' plus one more variable 'CR_ratio'&amp;nbsp; and it should have value as 9.2521 in the matching rows. I want to display 0 if the condition is not statisfied.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Condition to claculate new variable is,&lt;/P&gt;
&lt;PRE&gt;(case
    when (Contract='yes' and Start_CR='no' and Full_year_start='yes') then CR/sum(Yearly_Premium) 
    else 0)&lt;/PRE&gt;
&lt;P&gt;I'm looking for the solution in proc sql&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 21 Aug 2020 10:57:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Case-when-multiple-conditions-to-create-aggregated-value/m-p/678411#M204742</guid>
      <dc:creator>David_Billa</dc:creator>
      <dc:date>2020-08-21T10:57:29Z</dc:date>
    </item>
    <item>
      <title>Re: Case when multiple conditions to create aggregated value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Case-when-multiple-conditions-to-create-aggregated-value/m-p/678477#M204784</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/292396"&gt;@David_Billa&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/138619"&gt;@Satish_Parida&lt;/a&gt;&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt;&amp;nbsp;I don't think I've misplaced any paranthesis in my post.&lt;/P&gt;
&lt;P&gt;I'm looking for the solution in proc sql&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Considering that the original post "code"&lt;/P&gt;
&lt;PRE&gt;proc sql;
   create table want  as
   select * ,(case when (Contract='yes' and Start_CR='no' and Full_year_start='yes')
            then (CR/sum(Yearly_Premium
            else 0&lt;FONT color="#FF0000"&gt;&lt;STRONG&gt;))&lt;/STRONG&gt; &lt;/FONT&gt;
            end) as CR_Ratio length = 8
            format = 21.4
from have
group by
      Segment,
      Company
;
quit;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;Has the closing ) for both the "&lt;FONT color="#FF0000"&gt;&lt;STRONG&gt;(&lt;/STRONG&gt;&lt;/FONT&gt;CR/ " and&amp;nbsp; "sum&lt;FONT color="#FF0000"&gt;&lt;STRONG&gt;(&lt;/STRONG&gt;&lt;/FONT&gt;Yearly_premium"&amp;nbsp;&lt;STRONG&gt;after&amp;nbsp;&lt;/STRONG&gt;"else 0" very clearly, you did post code with bad parentheses.&amp;nbsp; Which is one reason you will see a lot of requests to post code from the LOG in this forum. Then there is no question as to what was actually submitted. &lt;/P&gt;</description>
      <pubDate>Fri, 21 Aug 2020 15:13:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Case-when-multiple-conditions-to-create-aggregated-value/m-p/678477#M204784</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2020-08-21T15:13:55Z</dc:date>
    </item>
  </channel>
</rss>

