<?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: Proc SQL Case When Syntax Error in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Proc-SQL-Case-When-Syntax-Error/m-p/567060#M159421</link>
    <description>&lt;P&gt;IMO trying to reproduce convoluted conditional logic in SQL at all is what you are doing wrong. Choose the right tool for the job - your DATA step version is way better.&lt;/P&gt;</description>
    <pubDate>Tue, 18 Jun 2019 20:21:56 GMT</pubDate>
    <dc:creator>SASKiwi</dc:creator>
    <dc:date>2019-06-18T20:21:56Z</dc:date>
    <item>
      <title>Proc SQL Case When Syntax Error</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proc-SQL-Case-When-Syntax-Error/m-p/567053#M159417</link>
      <description>&lt;P&gt;I am trying to use a nested case statement in Proc Sql to define a new variable. I am able to do this using a data step using Nested If's in the code below, but my Proc Sql code throws a syntax error. The data step and proc sql outputs should match. What am I doing wrong?&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;BR /&gt;&lt;CODE class=" language-sas"&gt;*Test Data Set;
data test;                     
   input Category_1 $ Category_2 $ Category_3 $ Category_4 $;       
   datalines;           
Y N  A 1
Y N  A 5
Y N  B 6
Y Y  B 3
Y Y  A 2
Y Y  A 5
Y Y  B 1
Y Y  B 4
N Y  A 2
N Y  A 5
N Y  B 3
N Y  B 6
N N  A 1
N N  A 4
N N  B 3
N N  B 5
N Y  A 1
N Y  A 6
N Y  B 3
N Y  B 4
;  

*Data step;
data test_data;
set test;
length Field $4;
if Category_1= "N" then do;
	if Category_2= "N" then do;
		if Category_3= "A" then Field= "AB";
		else if Category_4 in (1,2,3) then Field= "ABC";
		else if Category_4 in (4,5,6) then Field= "ABCD";
	end;
	else if Category_2 = "Y" then do;
		if Category_3= "A" then Field= "AC";
		else if Category_3= "B" then Field= "ACB";
	end;
end;
else if Category_1= "Y" then do;
	if Category_2= "N" then do;
		if Category_3= "A" then Field= "BA";
		else if Category_4 in (1,2,3) then Field= "BAC";
		else if Category_4 in (4,5,6) then Field= "BACD";
	end;
	else if Category_2= "Y" then do;
		if Category_3= "A" then Field= "BAD";
		else if Category_3= "B" then Field= "CAD";
	end;
end;
run;

*Proc Sql;
proc sql; create table test_sql as select *,
case 
	when Category_1="N" 
		then
			case 
				when Category_2="N" 
					then
						case 
							when Category_3="A" 
								then "AB"
 							else when Category_4 in(1,2,3) 
								then "ABC"
							
						end
							else when Category_4 in (4,5,6) 
								then "ABCD"
							
						end 
				else when Category_2="Y"
					then 
						case 
							when Category_3="A" 
								then "AC"
							else when  Category_3="B"
								then "ACB"
						end
				end
			end
	else when Category_1="Y"
		then 
			case 
				when Category_2="N" 
					then
						case 
							when Category_3="A" 
								then "BA"
 							else when Category_4 in(1,2,3) 
								then "BAC" 
							else when Category_4 in (4,5,6) 
								then "BACD"
						end 
				else when Category_2="Y"
					then 
						case 
							when Category_3="A" 
								then "BAD"
							else when  Category_3="B"
								then "CAD"
						end
			end
end as Field

from test;
quit;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Log:&lt;/P&gt;&lt;BLOCKQUOTE&gt;&lt;P&gt;proc sql;&lt;BR /&gt;24 ! create table test_sql as select *,&lt;BR /&gt;25 case&lt;BR /&gt;26 when Category_1="N"&lt;BR /&gt;27 then&lt;BR /&gt;28 case&lt;BR /&gt;29 when Category_2="N"&lt;BR /&gt;30 then&lt;BR /&gt;31 case&lt;BR /&gt;32 when Category_3="A"&lt;BR /&gt;33 then "AB"&lt;BR /&gt;34 else when Category_4 in(1,2,3)&lt;BR /&gt;__________&lt;BR /&gt;22&lt;BR /&gt;76&lt;BR /&gt;ERROR 22-322: Syntax error, expecting one of the following: !, !!, &amp;amp;, (, *, **, +, -, '.', /, AND, OR, |, ||.&lt;/P&gt;&lt;P&gt;ERROR 76-322: Syntax error, statement will be ignored.&lt;/P&gt;&lt;P&gt;35 then "ABC"&lt;BR /&gt;36&lt;BR /&gt;37 end&lt;BR /&gt;38 else when Category_4 in (4,5,6)&lt;BR /&gt;39 then "ABCD"&lt;BR /&gt;40&lt;BR /&gt;41 end&lt;BR /&gt;42 else when Category_2="Y"&lt;BR /&gt;43 then&lt;BR /&gt;44 case&lt;BR /&gt;45 when Category_3="A"&lt;BR /&gt;2 The SAS System 12:31 Tuesday, June 18, 2019&lt;/P&gt;&lt;P&gt;46 then "AC"&lt;BR /&gt;47 else when Category_3="B"&lt;BR /&gt;48 then "ACB"&lt;BR /&gt;49 end&lt;BR /&gt;50 end&lt;BR /&gt;51 end&lt;BR /&gt;52 else when Category_1="Y"&lt;BR /&gt;53 then&lt;BR /&gt;54 case&lt;BR /&gt;55 when Category_2="N"&lt;BR /&gt;56 then&lt;BR /&gt;57 case&lt;BR /&gt;58 when Category_3="A"&lt;BR /&gt;59 then "BA"&lt;BR /&gt;60 else when Category_4 in(1,2,3)&lt;BR /&gt;61 then "BAC"&lt;BR /&gt;62 else when Category_4 in (4,5,6)&lt;BR /&gt;63 then "BACD"&lt;BR /&gt;64 end&lt;BR /&gt;65 else when Category_2="Y"&lt;BR /&gt;66 then&lt;BR /&gt;67 case&lt;BR /&gt;68 when Category_3="A"&lt;BR /&gt;69 then "BAD"&lt;BR /&gt;70 else when Category_3="B"&lt;BR /&gt;71 then "CAD"&lt;BR /&gt;72 end&lt;BR /&gt;73 end&lt;BR /&gt;74 end as Field&lt;BR /&gt;75&lt;BR /&gt;76 from test;&lt;BR /&gt;NOTE: PROC SQL set option NOEXEC and will continue to check the syntax of statements.&lt;BR /&gt;77 quit;&lt;BR /&gt;NOTE: The SAS System stopped processing this step because of errors.&lt;BR /&gt;NOTE: PROCEDURE SQL used (Total process time):&lt;BR /&gt;real time 0.00 seconds&lt;BR /&gt;user cpu time 0.00 seconds&lt;BR /&gt;system cpu time 0.00 seconds&lt;BR /&gt;memory 59.62k&lt;BR /&gt;OS Memory 27556.00k&lt;BR /&gt;Timestamp 06/18/2019 02:59:21 PM&lt;BR /&gt;Step Count 50 Switch Count 0&lt;BR /&gt;Page Faults 0&lt;BR /&gt;Page Reclaims 13&lt;BR /&gt;Page Swaps 0&lt;BR /&gt;Voluntary Context Switches 3&lt;BR /&gt;Involuntary Context Switches 1&lt;BR /&gt;Block Input Operations 0&lt;BR /&gt;Block Output Operations 0&lt;/P&gt;&lt;/BLOCKQUOTE&gt;</description>
      <pubDate>Tue, 18 Jun 2019 19:59:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proc-SQL-Case-When-Syntax-Error/m-p/567053#M159417</guid>
      <dc:creator>theponcer</dc:creator>
      <dc:date>2019-06-18T19:59:53Z</dc:date>
    </item>
    <item>
      <title>Re: Proc SQL Case When Syntax Error</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proc-SQL-Case-When-Syntax-Error/m-p/567055#M159418</link>
      <description>I would highly recommend a look up table or a format instead of this type of logic.</description>
      <pubDate>Tue, 18 Jun 2019 20:04:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proc-SQL-Case-When-Syntax-Error/m-p/567055#M159418</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2019-06-18T20:04:04Z</dc:date>
    </item>
    <item>
      <title>Re: Proc SQL Case When Syntax Error</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proc-SQL-Case-When-Syntax-Error/m-p/567060#M159421</link>
      <description>&lt;P&gt;IMO trying to reproduce convoluted conditional logic in SQL at all is what you are doing wrong. Choose the right tool for the job - your DATA step version is way better.&lt;/P&gt;</description>
      <pubDate>Tue, 18 Jun 2019 20:21:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proc-SQL-Case-When-Syntax-Error/m-p/567060#M159421</guid>
      <dc:creator>SASKiwi</dc:creator>
      <dc:date>2019-06-18T20:21:56Z</dc:date>
    </item>
    <item>
      <title>Re: Proc SQL Case When Syntax Error</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proc-SQL-Case-When-Syntax-Error/m-p/567073#M159424</link>
      <description>&lt;P&gt;The case expression in SQL is&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;case&lt;/P&gt;
&lt;P&gt;when &amp;lt;expression&amp;gt; value&lt;/P&gt;
&lt;P&gt;when &amp;lt;expression&amp;gt; value&lt;/P&gt;
&lt;P&gt;...&lt;/P&gt;
&lt;P&gt;else value&lt;/P&gt;
&lt;P&gt;end as name&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;there is no &lt;EM&gt;else when&lt;/EM&gt;.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Remove the &lt;EM&gt;else.&lt;/EM&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 18 Jun 2019 21:10:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proc-SQL-Case-When-Syntax-Error/m-p/567073#M159424</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2019-06-18T21:10:44Z</dc:date>
    </item>
  </channel>
</rss>

