<?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: syntax error proc sql case when statement in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/syntax-error-proc-sql-case-when-statement/m-p/610753#M76782</link>
    <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/302184"&gt;@sasprogramming&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Does that answer you question? -&amp;gt; just remove the multiple "else" to keep one at the very end.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;

create table data_new as select *,

case when a is not null then 500 

	 when b&amp;gt;=60 and c=0 then 400

	 when d &amp;lt;6 and Days_Arrears&amp;gt;e then 300

	 when d &amp;lt;6 and Days_Arrears&amp;gt;e then 200

         when d &amp;lt;6 and Days_Arrears=e then 100

	 when f = 'available' then 50

	 when f = 'vacant' then 25

else 0 end as Points

from data_old;

quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;A href="https://www.lexjansen.com/wuss/2011/coders/Papers_Lafler_K_72492.pdf" target="_self"&gt;https://www.lexjansen.com/wuss/2011/coders/Papers_Lafler_K_72492.pdf&lt;/A&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 10 Dec 2019 15:41:00 GMT</pubDate>
    <dc:creator>ed_sas_member</dc:creator>
    <dc:date>2019-12-10T15:41:00Z</dc:date>
    <item>
      <title>syntax error proc sql case when statement</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/syntax-error-proc-sql-case-when-statement/m-p/610580#M76781</link>
      <description>&lt;P&gt;I am trying to run the following code and I am running into this error:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;

create table data_new as select *,

case when a is not null then 500 

else when b&amp;gt;=60 and c=0 then 400

else when d &amp;lt;6 and Days_Arrears&amp;gt;e then 300

else when d &amp;lt;6 and Days_Arrears&amp;gt;e then 200

else when d &amp;lt;6 and Days_Arrears=e then 100

else when f = 'available' then 50

else f = 'vacant' then 25

else 0 end as Points

from data_old;

quit;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Error:&lt;/P&gt;&lt;PRE&gt;1          OPTIONS NONOTES NOSTIMER NOSOURCE NOSYNTAXCHECK;
 72         
 73         proc sql;
 74         
 75         create table data_new as select *,
 76         
 77         case when a is not null then 500
 78         
 79         else when b&amp;gt;=60 and c=0 then 400
                      ____________
                      22
                      76
 ERROR 22-322: Syntax error, expecting one of the following: !, !!, &amp;amp;, (, *, **, +, -, '.', /, &amp;lt;, &amp;lt;=, &amp;lt;&amp;gt;, =, &amp;gt;, &amp;gt;=, AND, EQ, EQT, 
               GE, GET, GT, GTT, LE, LET, LT, LTT, NE, NET, OR, ^=, |, ||, ~=.  
 
 ERROR 76-322: Syntax error, statement will be ignored.
 &lt;/PRE&gt;</description>
      <pubDate>Mon, 09 Dec 2019 22:45:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/syntax-error-proc-sql-case-when-statement/m-p/610580#M76781</guid>
      <dc:creator>sasprogramming</dc:creator>
      <dc:date>2019-12-09T22:45:05Z</dc:date>
    </item>
    <item>
      <title>Re: syntax error proc sql case when statement</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/syntax-error-proc-sql-case-when-statement/m-p/610753#M76782</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/302184"&gt;@sasprogramming&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Does that answer you question? -&amp;gt; just remove the multiple "else" to keep one at the very end.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;

create table data_new as select *,

case when a is not null then 500 

	 when b&amp;gt;=60 and c=0 then 400

	 when d &amp;lt;6 and Days_Arrears&amp;gt;e then 300

	 when d &amp;lt;6 and Days_Arrears&amp;gt;e then 200

         when d &amp;lt;6 and Days_Arrears=e then 100

	 when f = 'available' then 50

	 when f = 'vacant' then 25

else 0 end as Points

from data_old;

quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;A href="https://www.lexjansen.com/wuss/2011/coders/Papers_Lafler_K_72492.pdf" target="_self"&gt;https://www.lexjansen.com/wuss/2011/coders/Papers_Lafler_K_72492.pdf&lt;/A&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 10 Dec 2019 15:41:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/syntax-error-proc-sql-case-when-statement/m-p/610753#M76782</guid>
      <dc:creator>ed_sas_member</dc:creator>
      <dc:date>2019-12-10T15:41:00Z</dc:date>
    </item>
    <item>
      <title>Re: syntax error proc sql case when statement</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/syntax-error-proc-sql-case-when-statement/m-p/610758#M76783</link>
      <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/302184"&gt;@sasprogramming&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/292097"&gt;@ed_sas_member&lt;/a&gt;&amp;nbsp;has replied to your post while I was still typing my answer ...&lt;/P&gt;
&lt;P&gt;I was wondering why nobody had replied to your post yet, because syntax errors are among the easiest errors to resolve. Here's why: The &lt;A href="https://support.sas.com/en/documentation.html" target="_blank" rel="noopener"&gt;SAS documentation&lt;/A&gt; is really good, so that a comparison of a piece of SAS code and the syntax shown there quickly reveals the difference which constitutes the syntax error.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In your example, use, e.g., "case when" (without quotes) as the search terms and the first link returned will be:&lt;/P&gt;
&lt;P&gt;&lt;A class="result-title_txt_all" href="https://documentation.sas.com/?docsetId=sqlproc&amp;amp;docsetVersion=9.4&amp;amp;docsetTarget=n0a85s0ijz65irn1h3jtariooea5.htm&amp;amp;locale=en" target="_blank" rel="noopener"&gt;CASE Expression :: SAS® 9.4 SQL Procedure User’s Guide, Fourth Edition&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;(which is the correct one [assuming SAS version 9.4] because the error occurred in a PROC SQL step, not in FedSQL etc.).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It says:&lt;/P&gt;
&lt;DIV id="n1r5k3lzyyz8zen1gpa3d1ubzt75" class="xisDoc-syntaxSimple"&gt;
&lt;DIV class="xisDoc-syntaxLevel"&gt;
&lt;DIV class="xisDoc-syntaxLevel"&gt;
&lt;H2 id="n1qfnqp5xlw10nn10cacqzl0f8dq" class="xisDoc-title"&gt;Syntax&lt;/H2&gt;
&lt;DIV id="n1r5k3lzyyz8zen1gpa3d1ubzt75" class="xisDoc-syntaxSimple"&gt;
&lt;DIV class="xisDoc-syntaxLevel"&gt;&lt;SPAN class="xisDoc-keyword"&gt;CASE&lt;/SPAN&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN class="xisDoc-optional"&gt;&amp;lt;&lt;EM class="xisDoc-userSuppliedValue"&gt;&lt;A class="ng-scope" tabindex="0" title="“case-operand”" href="https://documentation.sas.com/?docsetId=sqlproc&amp;amp;docsetTarget=n0a85s0ijz65irn1h3jtariooea5.htm&amp;amp;docsetVersion=9.4&amp;amp;locale=en#p0o8jaylyd0qrbn1dml9kfbmhuvp" data-docset-id="sqlproc" data-docset-version="9.4" data-original-href="n0a85s0ijz65irn1h3jtariooea5.htm#p0o8jaylyd0qrbn1dml9kfbmhuvp"&gt;case-operand&lt;/A&gt;&lt;/EM&gt;&amp;gt;&lt;/SPAN&gt;
&lt;DIV class="xisDoc-syntaxLevel"&gt;&lt;SPAN class="xisDoc-keyword"&gt;WHEN&lt;/SPAN&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;EM class="xisDoc-userSuppliedValue"&gt;&lt;A class="ng-scope" tabindex="0" title="“when-condition”" href="https://documentation.sas.com/?docsetId=sqlproc&amp;amp;docsetTarget=n0a85s0ijz65irn1h3jtariooea5.htm&amp;amp;docsetVersion=9.4&amp;amp;locale=en#p1hair56i6sjw4n1f8hpxhx5cskf" data-docset-id="sqlproc" data-docset-version="9.4" data-original-href="n0a85s0ijz65irn1h3jtariooea5.htm#p1hair56i6sjw4n1f8hpxhx5cskf"&gt;when-condition&lt;/A&gt;&lt;/EM&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN class="xisDoc-keyword"&gt;THEN&lt;/SPAN&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;EM class="xisDoc-userSuppliedValue"&gt;&lt;A class="ng-scope" tabindex="0" title="“result-expression”" href="https://documentation.sas.com/?docsetId=sqlproc&amp;amp;docsetTarget=n0a85s0ijz65irn1h3jtariooea5.htm&amp;amp;docsetVersion=9.4&amp;amp;locale=en#n0i7h21kk5im2en18xaqacg41hmc" data-docset-id="sqlproc" data-docset-version="9.4" data-original-href="n0a85s0ijz65irn1h3jtariooea5.htm#n0i7h21kk5im2en18xaqacg41hmc"&gt;result-expression&lt;/A&gt;&lt;/EM&gt;&lt;/DIV&gt;
&lt;DIV class="xisDoc-syntaxLevel"&gt;&lt;SPAN class="xisDoc-optional"&gt;&amp;lt;&lt;SPAN class="xisDoc-keyword"&gt;WHEN&lt;/SPAN&gt;&lt;EM class="xisDoc-userSuppliedValue"&gt;&lt;A class="ng-scope" tabindex="0" title="“when-condition”" href="https://documentation.sas.com/?docsetId=sqlproc&amp;amp;docsetTarget=n0a85s0ijz65irn1h3jtariooea5.htm&amp;amp;docsetVersion=9.4&amp;amp;locale=en#p1hair56i6sjw4n1f8hpxhx5cskf" data-docset-id="sqlproc" data-docset-version="9.4" data-original-href="n0a85s0ijz65irn1h3jtariooea5.htm#p1hair56i6sjw4n1f8hpxhx5cskf"&gt;when-condition&lt;/A&gt;&lt;/EM&gt;&lt;SPAN class="xisDoc-keyword"&gt;THEN&lt;/SPAN&gt;&lt;EM class="xisDoc-userSuppliedValue"&gt;&lt;A class="ng-scope" tabindex="0" title="“result-expression”" href="https://documentation.sas.com/?docsetId=sqlproc&amp;amp;docsetTarget=n0a85s0ijz65irn1h3jtariooea5.htm&amp;amp;docsetVersion=9.4&amp;amp;locale=en#n0i7h21kk5im2en18xaqacg41hmc" data-docset-id="sqlproc" data-docset-version="9.4" data-original-href="n0a85s0ijz65irn1h3jtariooea5.htm#n0i7h21kk5im2en18xaqacg41hmc"&gt;result-expression&lt;/A&gt;&lt;/EM&gt;&lt;SPAN class="xis-symbolHEllipsis"&gt;...&lt;/SPAN&gt;&amp;gt;&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV class="xisDoc-syntaxLevel"&gt;&lt;SPAN class="xisDoc-optional"&gt;&amp;lt;&lt;SPAN class="xisDoc-keyword"&gt;ELSE&lt;/SPAN&gt;&lt;EM class="xisDoc-userSuppliedValue"&gt;&lt;A class="ng-scope" tabindex="0" title="“result-expression”" href="https://documentation.sas.com/?docsetId=sqlproc&amp;amp;docsetTarget=n0a85s0ijz65irn1h3jtariooea5.htm&amp;amp;docsetVersion=9.4&amp;amp;locale=en#n0i7h21kk5im2en18xaqacg41hmc" data-docset-id="sqlproc" data-docset-version="9.4" data-original-href="n0a85s0ijz65irn1h3jtariooea5.htm#n0i7h21kk5im2en18xaqacg41hmc"&gt;result-expression&lt;/A&gt;&lt;/EM&gt;&amp;gt;&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV class="xisDoc-syntaxLevel"&gt;&lt;SPAN class="xisDoc-keyword"&gt;END&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;DIV class="xisDoc-syntaxLevel"&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV class="xisDoc-syntaxLevel"&gt;So you see that both your "else when" and "else ... then" constructs are invalid. In fact, the second and all subsequent WHEN clauses in a CASE expression have sort of an "implied ELSE" in front of them, i.e., they apply only if all previous WHEN conditions were not met, as does the result expression in the ELSE clause at the end (if any).&lt;/DIV&gt;
&lt;DIV class="xisDoc-syntaxLevel"&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV class="xisDoc-syntaxLevel"&gt;Now the syntax correction is easy: Delete the first five "&lt;FONT face="courier new,courier"&gt;else&lt;/FONT&gt;" keywords and replace the sixth (followed by "&lt;FONT face="courier new,courier"&gt;f = 'vacant'&lt;/FONT&gt;") with "&lt;FONT face="courier new,courier"&gt;when&lt;/FONT&gt;" -- as suggested by&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/292097"&gt;@ed_sas_member&lt;/a&gt;.&lt;/DIV&gt;
&lt;DIV class="xisDoc-syntaxLevel"&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV class="xisDoc-syntaxLevel"&gt;&lt;STRONG&gt;You're not done yet, though.&lt;/STRONG&gt; Note that most likely there's (at least) one &lt;EM&gt;logical&lt;/EM&gt; error left: The two WHEN conditions for &lt;FONT face="courier new,courier"&gt;Points=300&lt;/FONT&gt; and &lt;FONT face="courier new,courier"&gt;Points=200&lt;/FONT&gt; are accidentally (?) identical. As a consequence, the second of the two will never apply (because of the "implied ELSE") and none of the observations in &lt;FONT face="courier new,courier"&gt;data_new&lt;/FONT&gt; will receive 200 points. Reconsider the criteria for 200 and 300 and correct the WHEN condition(s) accordingly.&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;</description>
      <pubDate>Tue, 10 Dec 2019 16:00:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/syntax-error-proc-sql-case-when-statement/m-p/610758#M76783</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2019-12-10T16:00:13Z</dc:date>
    </item>
  </channel>
</rss>

