<?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: using proc sql with case in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/using-proc-sql-with-case/m-p/867331#M342545</link>
    <description>&lt;P&gt;What do you want to achieve in particular with this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;case when HCPCS_CD= '55700' | '55705&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;?&lt;/P&gt;</description>
    <pubDate>Thu, 30 Mar 2023 19:59:17 GMT</pubDate>
    <dc:creator>Kurt_Bremser</dc:creator>
    <dc:date>2023-03-30T19:59:17Z</dc:date>
    <item>
      <title>using proc sql with case</title>
      <link>https://communities.sas.com/t5/SAS-Programming/using-proc-sql-with-case/m-p/867297#M342537</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;Am learning the proc sql with case and where is my code. I want to generate multiple variable (MRI, BIOP &amp;amp; MRI_BIOP) if the condition is true but this is not giving me the expected results. Am help?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;proc sql;&lt;BR /&gt;create table want as&lt;BR /&gt;select *,&lt;BR /&gt;case when HCPCS_CD= '55700' | '55705' then 1 else 0 end as BIOP,&lt;BR /&gt;case when HCPCS_CD= '77021' then 1 else 0 end as MRI_BIOP ,&lt;BR /&gt;case when HCPCS_CD in ('72195', '72196', '72197', '76376', '76377', '76498', '72148', '72146', '72198', '8895', '8896', 'B43', 'B53', 'BW3')&lt;BR /&gt;then 1 else 0 end as MRI&lt;/P&gt;&lt;P&gt;from diag_out;&lt;BR /&gt;quit;&lt;/P&gt;</description>
      <pubDate>Thu, 30 Mar 2023 18:36:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/using-proc-sql-with-case/m-p/867297#M342537</guid>
      <dc:creator>CathyVI</dc:creator>
      <dc:date>2023-03-30T18:36:23Z</dc:date>
    </item>
    <item>
      <title>Re: using proc sql with case</title>
      <link>https://communities.sas.com/t5/SAS-Programming/using-proc-sql-with-case/m-p/867331#M342545</link>
      <description>&lt;P&gt;What do you want to achieve in particular with this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;case when HCPCS_CD= '55700' | '55705&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;?&lt;/P&gt;</description>
      <pubDate>Thu, 30 Mar 2023 19:59:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/using-proc-sql-with-case/m-p/867331#M342545</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2023-03-30T19:59:17Z</dc:date>
    </item>
    <item>
      <title>Re: using proc sql with case</title>
      <link>https://communities.sas.com/t5/SAS-Programming/using-proc-sql-with-case/m-p/867346#M342549</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am using "|" to represent "or".&amp;nbsp; I may be wrong that's why I need guidance.&lt;/P&gt;&lt;P&gt;My thought is like using the if then statement&lt;/P&gt;&lt;P&gt;if NCPCS_CD = '55700' or '55705' then BIOp =1; else BIOP = 0;&lt;/P&gt;&lt;P&gt;but i want this is sql for learning purposes&lt;/P&gt;</description>
      <pubDate>Thu, 30 Mar 2023 20:25:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/using-proc-sql-with-case/m-p/867346#M342549</guid>
      <dc:creator>CathyVI</dc:creator>
      <dc:date>2023-03-30T20:25:04Z</dc:date>
    </item>
    <item>
      <title>Re: using proc sql with case</title>
      <link>https://communities.sas.com/t5/SAS-Programming/using-proc-sql-with-case/m-p/867351#M342552</link>
      <description>&lt;P&gt;&amp;nbsp;You pretty much need a complete expression after OR&lt;/P&gt;
&lt;P&gt;When A='NBC' &lt;STRONG&gt;or &lt;/STRONG&gt;A='PDQ'&lt;/P&gt;
&lt;P&gt;The IN operator works also;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;When A in ('NBC' 'PDQ') ..&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What happens when you use something like&lt;/P&gt;
&lt;PRE&gt; NCPCS_CD = '55700' or '55705' &lt;/PRE&gt;
&lt;P&gt;is the value after the OR is expected to be the result of a logical expression. SAS expects such results to be numeric and ideally&amp;nbsp; 0 (false) or 1 (true). So will attempt to convert the value to a numeric and if so uses that result. SAS will use any value other than Missing and zero as true though.&lt;/P&gt;
&lt;P&gt;This example shows the result of the comparison:&lt;/P&gt;
&lt;PRE&gt;data junk;
 NCPCS_CD = '11111';
 x = (NCPCS_CD = '55700' or '55705' );
 y = (NCPCS_CD = '55700' or NCPCS_CD = '55705' );
 z = (NCPCS_CD in ( '55700' '55705') );
run;&lt;/PRE&gt;
&lt;P&gt;The result for X is always 1 (true) because of the conversion of 55705 to numeric.&lt;/P&gt;
&lt;P&gt;IF you had used a value that could not be converted to a number in the X expression you would have received an invalid data message:&lt;/P&gt;
&lt;PRE&gt;180  data junk;
181   NCPCS_CD = '11111';
182   x = (NCPCS_CD = '55700' or 'ABCDE' );
183   y = (NCPCS_CD = '55700' or NCPCS_CD = '55705' );
184   z = (NCPCS_CD in ( '55700' '55705') );
185  run;

NOTE: Character values have been converted to numeric values at the places given by:
      (Line):(Column).
      182:29
NOTE: Invalid numeric data, 'ABCDE' , at line 182 column 29.
NCPCS_CD=11111 x=0 y=0 z=0 _ERROR_=1 _N_=1
NOTE: The data set WORK.JUNK has 1 observations and 4 variables.
NOTE: DATA statement used (Total process time):
      real time           0.01 seconds
      cpu time            0.00 seconds
&lt;/PRE&gt;
&lt;P&gt;Unfortunately Proc SQL is not quite as nice as the data step about generating conversion and invalid data messages.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 30 Mar 2023 20:48:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/using-proc-sql-with-case/m-p/867351#M342552</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2023-03-30T20:48:23Z</dc:date>
    </item>
    <item>
      <title>Re: using proc sql with case</title>
      <link>https://communities.sas.com/t5/SAS-Programming/using-proc-sql-with-case/m-p/867353#M342554</link>
      <description>&lt;P&gt;OR is a binary logical operator which combines logical expressions. So, in your case,&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;NCPCS_CD = '55700'&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;is one logical expression, and&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;'55705' &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;is considered the other. Can you see your mistake now?&lt;/P&gt;
&lt;P&gt;Use IN, like you did in the other CASE.&lt;/P&gt;</description>
      <pubDate>Thu, 30 Mar 2023 20:56:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/using-proc-sql-with-case/m-p/867353#M342554</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2023-03-30T20:56:51Z</dc:date>
    </item>
    <item>
      <title>Re: using proc sql with case</title>
      <link>https://communities.sas.com/t5/SAS-Programming/using-proc-sql-with-case/m-p/867354#M342555</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/253321"&gt;@CathyVI&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am using "|" to represent "or".&amp;nbsp; I may be wrong that's why I need guidance.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;OR can be replaced by the IN operator, it's less typing.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;case when HCPCS_CD in ('55700','55705') then 1 else 0 end as BIOP,&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And even less typing, you don't even need CASE WHEN, you don't need THEN and you don't need ELSE and you don't need END&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;HCPCS_CD in ('55700','55705') as BIOP,&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;As others have pointed out, you can also do this using OR with the proper syntax (which is more typing):&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;case when HCPCS_CD= '55700' or HCPCS_CD='55705' then 1 else 0 end as BIOP,&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 30 Mar 2023 21:01:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/using-proc-sql-with-case/m-p/867354#M342555</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2023-03-30T21:01:52Z</dc:date>
    </item>
  </channel>
</rss>

