<?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 format in Proc SQL in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/CASE-format-in-Proc-SQL/m-p/450987#M113630</link>
    <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;As C is on the numerator, you shouldn't have to test its value.&lt;/P&gt;
&lt;P&gt;Also, D is a numeric variable so "INF" is not a valid value.&lt;/P&gt;
&lt;P&gt;You could set it to . and add an indicator variable as follows :&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
    SELECT a, b, abs(a-b) AS c, 
           CASE WHEN a=0 THEN . ELSE round(CALCULATED c*100/a,0.01) END AS d,
           CASE WHEN a=0 THEN 1 ELSE 0 END AS INF
    FROM test;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Wed, 04 Apr 2018 10:05:43 GMT</pubDate>
    <dc:creator>gamotte</dc:creator>
    <dc:date>2018-04-04T10:05:43Z</dc:date>
    <item>
      <title>CASE format in Proc SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/CASE-format-in-Proc-SQL/m-p/450982#M113628</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have three columns A and B, both numeric, and C = ABS(A-B)&amp;nbsp;&lt;/P&gt;&lt;P&gt;Sometimes A can be equal to 0. The same for B. They can also be both equal to 0 at the same time.So does C then.&lt;/P&gt;&lt;P&gt;I want to create a column D as the percentage of difference of B in comparison with A.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The mathematical formula is easy:&lt;/P&gt;&lt;PRE&gt;ROUND(C * 100 / A,0.01)&lt;/PRE&gt;&lt;P&gt;But you will have noticed that if A and/or C equal to 0 it won't work.&lt;/P&gt;&lt;P&gt;Then I do something like this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;if A = 0 
   if C = 0 then 0
   Else "INF"
Else ROUND(C* 100 / A,0.01)&lt;/PRE&gt;&lt;P&gt;"INF" stands for infinity here&lt;/P&gt;&lt;P&gt;In term of SAS that means:&lt;/P&gt;&lt;PRE&gt;CASE WHEN t1.A = 0 THEN
      CASE WHEN t1.C = 0 THEN 0
      ELSE "INF" END
ELSE ROUND(t1.C * 100 / t1.A,0.01)
END AS D&lt;/PRE&gt;&lt;P&gt;But when I do this I have the following error:&lt;/P&gt;&lt;PRE&gt;ERROR: Result of WHEN clause 2 is not the same data type as the preceding results.&lt;/PRE&gt;&lt;P&gt;Do you know how I should do it?&lt;/P&gt;&lt;P&gt;Thanks&lt;/P&gt;</description>
      <pubDate>Wed, 04 Apr 2018 09:25:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/CASE-format-in-Proc-SQL/m-p/450982#M113628</guid>
      <dc:creator>FP12</dc:creator>
      <dc:date>2018-04-04T09:25:23Z</dc:date>
    </item>
    <item>
      <title>Re: CASE format in Proc SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/CASE-format-in-Proc-SQL/m-p/450987#M113630</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;As C is on the numerator, you shouldn't have to test its value.&lt;/P&gt;
&lt;P&gt;Also, D is a numeric variable so "INF" is not a valid value.&lt;/P&gt;
&lt;P&gt;You could set it to . and add an indicator variable as follows :&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
    SELECT a, b, abs(a-b) AS c, 
           CASE WHEN a=0 THEN . ELSE round(CALCULATED c*100/a,0.01) END AS d,
           CASE WHEN a=0 THEN 1 ELSE 0 END AS INF
    FROM test;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 04 Apr 2018 10:05:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/CASE-format-in-Proc-SQL/m-p/450987#M113630</guid>
      <dc:creator>gamotte</dc:creator>
      <dc:date>2018-04-04T10:05:43Z</dc:date>
    </item>
    <item>
      <title>Re: CASE format in Proc SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/CASE-format-in-Proc-SQL/m-p/450994#M113633</link>
      <description>&lt;P&gt;Numeric column cannot contain "INF" as that is text.&amp;nbsp; You could create a character version as output:&lt;/P&gt;
&lt;PRE&gt;ELSE put(ROUND(t1.C * 100 / t1.A,0.01),best.)&lt;/PRE&gt;
&lt;P&gt;But then the rest of the column becomes a bit useless.&amp;nbsp; A better method is to create a format, and a specific number represents INF, e.g. if -999:&lt;/P&gt;
&lt;PRE&gt;proc format;
  value inf
    -999="INF";
run;

... end as d format=inf.
...&lt;/PRE&gt;
&lt;P&gt;So the number underneath would remain as -999, but would be displayed formatted as INF.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 04 Apr 2018 10:12:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/CASE-format-in-Proc-SQL/m-p/450994#M113633</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2018-04-04T10:12:18Z</dc:date>
    </item>
    <item>
      <title>Re: CASE format in Proc SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/CASE-format-in-Proc-SQL/m-p/451146#M113689</link>
      <description>&lt;P&gt;I might suggest using the DIVIDE function which will handle division by 0 and will assign special missing values for the division and not generate an error or warning message though in the Round function result you&amp;nbsp; will get missing values as a result.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;ROUND(C * divide(100,/ A),0.01)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Or use the special missing .I&amp;nbsp; (yes there is period that is part of the value) instead to TEXT "inf"&lt;/P&gt;</description>
      <pubDate>Wed, 04 Apr 2018 15:10:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/CASE-format-in-Proc-SQL/m-p/451146#M113689</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2018-04-04T15:10:46Z</dc:date>
    </item>
  </channel>
</rss>

