<?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 help in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/CASE-WHEN-help/m-p/821152#M324187</link>
    <description>"Or" doesnt work in case when; i had it like that before and it gave me error</description>
    <pubDate>Thu, 30 Jun 2022 14:47:23 GMT</pubDate>
    <dc:creator>bhca60</dc:creator>
    <dc:date>2022-06-30T14:47:23Z</dc:date>
    <item>
      <title>CASE WHEN help</title>
      <link>https://communities.sas.com/t5/SAS-Programming/CASE-WHEN-help/m-p/821029#M324120</link>
      <description>&lt;P&gt;I am having issues with CASE WHEN. I'm trying to create a column called Condition and flag each as asthma, COPD, etc.&amp;nbsp; I have two sets of codes for each condtion labeled as _suff or _rel (sufficient or related, respectively).&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
23         proc sql;
24         create table condition as
25         select
26         id,
27         pid,
28         dxCode as icd,
29         dxCodeDescription,
30         dxSeqNo
31         case when (compress(icd,".") in (&amp;amp;asthma_suff, &amp;amp;asthma_rel) then 'ASTHMA'
           ____
           22
           76
ERROR 22-322: Syntax error, expecting one of the following: a quoted string, !, !!, &amp;amp;, (, *, **, +, ',', -, '.', /, &amp;lt;, &amp;lt;=, &amp;lt;&amp;gt;, =, 
              &amp;gt;, &amp;gt;=, ?, AND, AS, BETWEEN, CONTAINS, EQ, EQT, FORMAT, FROM, GE, GET, GT, GTT, IN, INFORMAT, INTO, IS, LABEL, LE, 
              LEN, LENGTH, LET, LIKE, LT, LTT, NE, NET, NOT, NOTIN, OR, TRANSCODE, ^, ^=, |, ||, ~, ~=.  

ERROR 76-322: Syntax error, statement will be ignored.

32         
33          	 when (compress(icd,".") in &amp;amp;cad_suff, &amp;amp;cad_rel) then 'CAD'
34         
35              when (compress(icd,".") in &amp;amp;chf_suff, &amp;amp;chf_rel)  then 'CHF'
36         	
37          	 when (compress(icd,".") in &amp;amp;copd_suff, &amp;amp;copd_rel) then 'COPD'
38         
39          	 when (compress(icd,".") in &amp;amp;diab_suff, &amp;amp;diab_rel) then 'DIABETES'
40         
41         else 'HTN'
42         end as Condition
43         from dxtable
44         order by eventid;
NOTE: PROC SQL set option NOEXEC and will continue to check the syntax of statements.
45         quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 29 Jun 2022 21:17:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/CASE-WHEN-help/m-p/821029#M324120</guid>
      <dc:creator>bhca60</dc:creator>
      <dc:date>2022-06-29T21:17:00Z</dc:date>
    </item>
    <item>
      <title>Re: CASE WHEN help</title>
      <link>https://communities.sas.com/t5/SAS-Programming/CASE-WHEN-help/m-p/821031#M324122</link>
      <description>&lt;P&gt;You forgot a comma before the CASE (after dxSeqNo).&lt;/P&gt;
&lt;P&gt;And possibly a parenthesis before the list of values:&amp;nbsp;&lt;/P&gt;
&lt;PRE class="language-sas"&gt;&lt;CODE&gt;&amp;amp;cad_suff, &amp;amp;cad_rel)&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 29 Jun 2022 21:20:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/CASE-WHEN-help/m-p/821031#M324122</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2022-06-29T21:20:41Z</dc:date>
    </item>
    <item>
      <title>Re: CASE WHEN help</title>
      <link>https://communities.sas.com/t5/SAS-Programming/CASE-WHEN-help/m-p/821042#M324129</link>
      <description>still not working. I tried doing OR operators but it doesnt work for CASE WHEN.  &lt;BR /&gt;&lt;BR /&gt;the macros are a list of codes looking like this: ('J9088','90877')</description>
      <pubDate>Wed, 29 Jun 2022 22:41:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/CASE-WHEN-help/m-p/821042#M324129</guid>
      <dc:creator>bhca60</dc:creator>
      <dc:date>2022-06-29T22:41:33Z</dc:date>
    </item>
    <item>
      <title>Re: CASE WHEN help</title>
      <link>https://communities.sas.com/t5/SAS-Programming/CASE-WHEN-help/m-p/821050#M324135</link>
      <description>&lt;P&gt;Get the syntax right before adding the code generation (macro code).&lt;/P&gt;
&lt;P&gt;You are missing commas and parentheses.&amp;nbsp; Why are you ordering by a variable that is not being selected?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table condition as
  select
     id
   , pid
   , dxCode as icd
   , dxCodeDescription
   , dxSeqNo
   , case when ( compress(icd,".") in ('A') ) then 'ASTHMA'
          when ( compress(icd,".") in ('B') ) then 'CAD'
          when ( compress(icd,".") in ('C') ) then 'CHF'
          when ( compress(icd,".") in ('D') ) then 'COPD'
          when ( compress(icd,".") in ('E') ) then 'DIABETES'
          else 'HTN'
     end as Condition
  from dxtable
  order by id
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Then you can try adding in your macro variable references.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 30 Jun 2022 00:25:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/CASE-WHEN-help/m-p/821050#M324135</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-06-30T00:25:00Z</dc:date>
    </item>
    <item>
      <title>Re: CASE WHEN help</title>
      <link>https://communities.sas.com/t5/SAS-Programming/CASE-WHEN-help/m-p/821059#M324140</link>
      <description>&lt;P&gt;I'm getting an error (the parentheses seem fine now):&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;23         proc sql;
24         create table condition as
25         select
26         id,
27         pid,
28         dxCode as icd,
29         dxCodeDescription,
30         dxSeqNo,
31         case when (compress(icd,".") in (&amp;amp;asthma_suff)) then 'ASTHMA'
NOTE: Line generated by the macro variable "ASTHMA_SUFF".
31         ('J4520','J4521','J4522','J4530','J4531','J4532','J4540','J4541','J4542','J4550','J4551','J4552','J45901','J45902','J4590
            _______
_                                                                                                                                   
            79
22

76
31       ! 9','J45990','J45991','J45998')
ERROR 79-322: Expecting a SELECT.

ERROR 22-322: Syntax error, expecting one of the following: a quoted string, !, !!, &amp;amp;, *, **, +, ',', -, /, &amp;lt;, &amp;lt;=, &amp;lt;&amp;gt;, =, &amp;gt;, &amp;gt;=, ?, 
              AND, AS, BETWEEN, CONTAINS, EQ, EQT, FORMAT, FROM, GE, GET, GT, GTT, IN, INFORMAT, INTO, IS, LABEL, LE, LEN, LENGTH, 
              LET, LIKE, LT, LTT, NE, NET, NOT, NOTIN, OR, TRANSCODE, ^, ^=, |, ||, ~, ~=.  

ERROR 76-322: Syntax error, statement will be ignored.

32         	 when (compress(icd,".") in (&amp;amp;asthma_rel)) then 'ASTHMA'
33          	 when (compress(icd,".") in (&amp;amp;cad_suff)) then 'CAD'
34         	 when (compress(icd,".") in (&amp;amp;cad_rel)) then 'CAD'
35              when (compress(icd,".") in (&amp;amp;chf_suff)  then 'CHF'
36         	 when (compress(icd,".") in (&amp;amp;chf_rel)) then 'CHF'
37          	 when (compress(icd,".") in (&amp;amp;copd_suff) then 'COPD'
38           	 when (compress(icd,".") in (&amp;amp;copd_rel)) then 'COPD'
39          	 when (compress(icd,".") in (&amp;amp;diab_suff)) then 'DIABETES'
40          	 when (compress(icd,".") in (&amp;amp;diab_rel)) then 'DIABETES'
2                                                          The SAS System                             20:52 Wednesday, June 29, 2022

41         else 'HTN'
42         end as Condition
43         from dxtable
44         order by id;
NOTE: PROC SQL set option NOEXEC and will continue to check the syntax of statements.
45         quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 30 Jun 2022 04:27:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/CASE-WHEN-help/m-p/821059#M324140</guid>
      <dc:creator>bhca60</dc:creator>
      <dc:date>2022-06-30T04:27:37Z</dc:date>
    </item>
    <item>
      <title>Re: CASE WHEN help</title>
      <link>https://communities.sas.com/t5/SAS-Programming/CASE-WHEN-help/m-p/821060#M324141</link>
      <description>&lt;P&gt;Looks like your IN list is enclosed in two lots of brackets. Removing the brackets from your macro variable value is probably the best option.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt; get rid of this bracket ==&amp;gt;('J4520','J4521','J4522','J4530','J4531','J4532','J4540','J4541','J4542','J4550','J4551','J4552','J45901','J45902','J4590
            _______
_                                                                                                                                   
            79
22

76
31       ! 9','J45990','J45991','J45998') &amp;lt;== get rid of this bracket&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 30 Jun 2022 04:46:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/CASE-WHEN-help/m-p/821060#M324141</guid>
      <dc:creator>SASKiwi</dc:creator>
      <dc:date>2022-06-30T04:46:27Z</dc:date>
    </item>
    <item>
      <title>Re: CASE WHEN help</title>
      <link>https://communities.sas.com/t5/SAS-Programming/CASE-WHEN-help/m-p/821062#M324143</link>
      <description>&lt;P&gt;You have accidentally added macro quoting to the value of your macro variable.&amp;nbsp; Remove it.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;case when (compress(icd,".") in (%unquote(&amp;amp;asthma_suff) )) &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Why did you add the macro quoting?&lt;/P&gt;
&lt;P&gt;To be able to use the value of those macro variables to generate that code they cannot contain any characters that would need macro quoting.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Example:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;11   %let x='A','B';
12   proc sql;
13   select name
14        , case when (name in (&amp;amp;x)) then 'xxx' end as junk
15   from sashelp.class
16   ;
NOTE: A CASE expression has no ELSE clause. Cases not accounted for by the WHEN clauses will result in a missing value for the CASE
      expression.
NOTE: Writing HTML Body file: sashtml.htm
17   quit;
NOTE: PROCEDURE SQL used (Total process time):
      real time           0.14 seconds
      cpu time            0.12 seconds


18   %let x=%bquote('A','B');
19   proc sql;
20   select name
21        , case when (name in (&amp;amp;x)) then 'xxx' end as junk
NOTE: Line generated by the macro variable "X".
1    'A','B'
     -
     22
      -
      200
ERROR 22-322: Syntax error, expecting one of the following: a quoted string, a numeric constant, a datetime constant,
              a missing value, (, -, SELECT.

ERROR 200-322: The symbol is not recognized and will be ignored.

22   from sashelp.class
23   ;
24   quit;
&lt;/PRE&gt;</description>
      <pubDate>Thu, 30 Jun 2022 04:54:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/CASE-WHEN-help/m-p/821062#M324143</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-06-30T04:54:40Z</dc:date>
    </item>
    <item>
      <title>Re: CASE WHEN help</title>
      <link>https://communities.sas.com/t5/SAS-Programming/CASE-WHEN-help/m-p/821148#M324184</link>
      <description>are you saying to get rid of the parentheses? I'm going to try that and see if it works. But I was using those macros throughout my program. I will have to do another set of macros wihtout the parentheses then rename them then call them when doing the CASE WHEN statement.</description>
      <pubDate>Thu, 30 Jun 2022 14:30:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/CASE-WHEN-help/m-p/821148#M324184</guid>
      <dc:creator>bhca60</dc:creator>
      <dc:date>2022-06-30T14:30:24Z</dc:date>
    </item>
    <item>
      <title>Re: CASE WHEN help</title>
      <link>https://communities.sas.com/t5/SAS-Programming/CASE-WHEN-help/m-p/821149#M324185</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/290946"&gt;@bhca60&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;are you saying to get rid of the parentheses? I'm going to try that and see if it works. But I was using those macros throughout my program. I will have to do another set of macros wihtout the parentheses then rename them then call them when doing the CASE WHEN statement.&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;If you have parenthesis in this statement I'm sure you can see how it would error out.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt; &amp;amp;cad_suff, &amp;amp;cad_rel&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Since that would resolve to:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;((list of codes), (list of codes))&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You could also just expand your case when statements so that you have each as a separate condition.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;when (compress(icd,".") in &amp;amp;cad_suff&amp;nbsp;or&amp;nbsp;compress(icd,".") in &amp;amp;cad_rel&amp;nbsp;then&amp;nbsp;"CAD"&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 30 Jun 2022 14:35:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/CASE-WHEN-help/m-p/821149#M324185</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2022-06-30T14:35:26Z</dc:date>
    </item>
    <item>
      <title>Re: CASE WHEN help</title>
      <link>https://communities.sas.com/t5/SAS-Programming/CASE-WHEN-help/m-p/821151#M324186</link>
      <description>&lt;P&gt;If the macro variables already have the () then don't add extra ones.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;when (compress(icd,".") in &amp;amp;cad_suff) then 'CAD'
when (compress(icd,".") in &amp;amp;cad_rel ) then 'CAD'&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 30 Jun 2022 14:46:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/CASE-WHEN-help/m-p/821151#M324186</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-06-30T14:46:11Z</dc:date>
    </item>
    <item>
      <title>Re: CASE WHEN help</title>
      <link>https://communities.sas.com/t5/SAS-Programming/CASE-WHEN-help/m-p/821152#M324187</link>
      <description>"Or" doesnt work in case when; i had it like that before and it gave me error</description>
      <pubDate>Thu, 30 Jun 2022 14:47:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/CASE-WHEN-help/m-p/821152#M324187</guid>
      <dc:creator>bhca60</dc:creator>
      <dc:date>2022-06-30T14:47:23Z</dc:date>
    </item>
    <item>
      <title>Re: CASE WHEN help</title>
      <link>https://communities.sas.com/t5/SAS-Programming/CASE-WHEN-help/m-p/821159#M324192</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/290946"&gt;@bhca60&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;"Or" doesnt work in case when; i had it like that before and it gave me error&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table demo as
select *, case when age = 13 or age=15 or age=11 then 'Select'
else 'Not Selected' end as check
from sashelp.class;
quit;

proc print data=demo;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;OR does work, something else was the issue.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 30 Jun 2022 15:04:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/CASE-WHEN-help/m-p/821159#M324192</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2022-06-30T15:04:51Z</dc:date>
    </item>
  </channel>
</rss>

