<?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 multiple subqueries with CASE/WHEN in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Using-multiple-subqueries-with-CASE-WHEN/m-p/797854#M313698</link>
    <description>&lt;P&gt;Updating an existing dataset is not a normal process flow for data analysis.&lt;/P&gt;
&lt;P&gt;Sounds like you just want to merge the datasets by the three key variables and set FLAG to 3 when there is a match.&lt;/P&gt;
&lt;P&gt;So let's make some actual datasets from your posted listings.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data A ;
  input var1 var2 $ var3 expect;
cards;
1 April 12 1
2 May   14 2
3 June  15 2
4 July  17 3
;

data B;
  input var1 var2 $ var3;
cards;
1 April 13
4 July  17
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Now a simple merge will let us test if the records in A are also in B or not.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want ;
  merge A(in=ina) B(in=inb);
  by var1-var3;
  if ina;
  if inb then flag=3;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Results&lt;/P&gt;
&lt;PRE&gt;Obs    var1    var2     var3    expect    flag

 1       1     April     12        1        .
 2       2     May       14        2        .
 3       3     June      15        2        .
 4       4     July      17        3        3
&lt;/PRE&gt;
&lt;P&gt;It is not clear to me where the values of 1 and 2 that were in your original table A are supposed to come from.&lt;/P&gt;</description>
    <pubDate>Tue, 22 Feb 2022 16:29:39 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2022-02-22T16:29:39Z</dc:date>
    <item>
      <title>Using multiple subqueries with CASE/WHEN</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-multiple-subqueries-with-CASE-WHEN/m-p/797590#M313571</link>
      <description>&lt;P&gt;Hello everyone!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I would like to update the values of a variable using 3 conditions. Particularly, I want to add a third value in the variable named "Flag" when the values of the variables (var1, var2, var3) are the same with the corresponding ones of the table B at the same time.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Unfortunately, the below code does not work properly.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;proc sql;

update tableA

set flag = case when (var1 in (select var1 from tableB) and var2 in&amp;nbsp;(select var2 from tableB) and&amp;nbsp;var3 in (select var3 from tableB))

then '3'

end;

quit;&lt;/PRE&gt;&lt;P&gt;What I want to achieve (an example):&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;U&gt;Table A:&lt;/U&gt;&lt;/P&gt;&lt;TABLE border="0" cellspacing="0" cellpadding="0"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;var1&lt;/TD&gt;&lt;TD&gt;var2&lt;/TD&gt;&lt;TD&gt;var3&lt;/TD&gt;&lt;TD&gt;flag&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;April&lt;/TD&gt;&lt;TD&gt;12&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;May&lt;/TD&gt;&lt;TD&gt;14&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;June&lt;/TD&gt;&lt;TD&gt;15&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;STRONG&gt;4&lt;/STRONG&gt;&lt;/TD&gt;&lt;TD&gt;&lt;STRONG&gt;July&lt;/STRONG&gt;&lt;/TD&gt;&lt;TD&gt;&lt;STRONG&gt;17&lt;/STRONG&gt;&lt;/TD&gt;&lt;TD&gt;&lt;U&gt;&lt;STRONG&gt;3&lt;/STRONG&gt;&lt;/U&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;U&gt;Table B:&lt;/U&gt;&lt;/P&gt;&lt;TABLE border="0" cellspacing="0" cellpadding="0"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;var1&lt;/TD&gt;&lt;TD&gt;var2&lt;/TD&gt;&lt;TD&gt;var3&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;April&lt;/TD&gt;&lt;TD&gt;13&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;STRONG&gt;4&lt;/STRONG&gt;&lt;/TD&gt;&lt;TD&gt;&lt;STRONG&gt;July&lt;/STRONG&gt;&lt;/TD&gt;&lt;TD&gt;&lt;STRONG&gt;17&lt;/STRONG&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Do you have any idea?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you in advance!&lt;/P&gt;</description>
      <pubDate>Tue, 22 Feb 2022 16:10:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-multiple-subqueries-with-CASE-WHEN/m-p/797590#M313571</guid>
      <dc:creator>Chrisas</dc:creator>
      <dc:date>2022-02-22T16:10:54Z</dc:date>
    </item>
    <item>
      <title>Re: Using multiple subqueries with CASE/WHEN</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-multiple-subqueries-with-CASE-WHEN/m-p/797599#M313573</link>
      <description>&lt;P&gt;I would do such in a data step:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data tablea_new;
set tablea;
if _n_ = 1
then do;
  declare hash v1 (dataset:"tableb");
  v1.definekey("var1");
  v1.definedone();
  declare hash v2 (dataset:"tableb");
  v2.definekey("var2");
  v2.definedone();
  declare hash v3 (dataset:"tableb");
  v3.definekey("var3");
  v3.definedone();
end;
if v1.check() = 0 and v2.check() = 0 and v3.check() = 0 then flag = "3";
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 21 Feb 2022 14:19:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-multiple-subqueries-with-CASE-WHEN/m-p/797599#M313573</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2022-02-21T14:19:58Z</dc:date>
    </item>
    <item>
      <title>Re: Using multiple subqueries with CASE/WHEN</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-multiple-subqueries-with-CASE-WHEN/m-p/797602#M313574</link>
      <description>&lt;P&gt;Thank you very much for your response!&lt;/P&gt;&lt;P&gt;But, again I have the same issue. For example, it flags a case equal to 3, while the three conditions are not satisfied at the same time. How is this possible?&lt;/P&gt;</description>
      <pubDate>Mon, 21 Feb 2022 14:56:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-multiple-subqueries-with-CASE-WHEN/m-p/797602#M313574</guid>
      <dc:creator>Chrisas</dc:creator>
      <dc:date>2022-02-21T14:56:16Z</dc:date>
    </item>
    <item>
      <title>Re: Using multiple subqueries with CASE/WHEN</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-multiple-subqueries-with-CASE-WHEN/m-p/797616#M313578</link>
      <description>&lt;P&gt;That is a very strange looking query. You are testing to see if all of the values of VAR1,VAR2, and VAR3 were each ever independently includes in those similarly named variables in TABLEB.&amp;nbsp; Wouldn't it be clearer to keep the set of values you want to test against in three different datasets?&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Can you show some example data for TABLEA and TABLEB?&lt;/P&gt;
&lt;P&gt;Make sure to include some where you want the flag to be changed to '3' and some where you don't.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 21 Feb 2022 16:43:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-multiple-subqueries-with-CASE-WHEN/m-p/797616#M313578</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-02-21T16:43:43Z</dc:date>
    </item>
    <item>
      <title>Re: Using multiple subqueries with CASE/WHEN</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-multiple-subqueries-with-CASE-WHEN/m-p/797679#M313616</link>
      <description>&lt;P&gt;If you only want those where&amp;nbsp;&lt;EM&gt;all&lt;/EM&gt; conditions are met within a&amp;nbsp;&lt;EM&gt;single&lt;/EM&gt; observation of the second table, then you must use a join instead of subqueries.&lt;/P&gt;</description>
      <pubDate>Mon, 21 Feb 2022 23:05:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-multiple-subqueries-with-CASE-WHEN/m-p/797679#M313616</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2022-02-21T23:05:29Z</dc:date>
    </item>
    <item>
      <title>Re: Using multiple subqueries with CASE/WHEN</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-multiple-subqueries-with-CASE-WHEN/m-p/797786#M313668</link>
      <description>&lt;P&gt;As Tom said . Post an example include input Dataset and output Dataset ,that could explain your question very well.&lt;/P&gt;
&lt;P&gt;Or maybe you want this one .&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;proc sql;

update tableA as a

set flag = case when exists(   select * from tableB as b where catx('|',b.var1, b.var2 , b.var3) = catx('|',a.var1, a.var2 , a.var3)   )
then '3'
end;

quit;&lt;/PRE&gt;</description>
      <pubDate>Tue, 22 Feb 2022 12:16:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-multiple-subqueries-with-CASE-WHEN/m-p/797786#M313668</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2022-02-22T12:16:21Z</dc:date>
    </item>
    <item>
      <title>Re: Using multiple subqueries with CASE/WHEN</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-multiple-subqueries-with-CASE-WHEN/m-p/797847#M313697</link>
      <description>Hi, thank you very much for your response! I have edited my post and I added an example.</description>
      <pubDate>Tue, 22 Feb 2022 16:12:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-multiple-subqueries-with-CASE-WHEN/m-p/797847#M313697</guid>
      <dc:creator>Chrisas</dc:creator>
      <dc:date>2022-02-22T16:12:40Z</dc:date>
    </item>
    <item>
      <title>Re: Using multiple subqueries with CASE/WHEN</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-multiple-subqueries-with-CASE-WHEN/m-p/797854#M313698</link>
      <description>&lt;P&gt;Updating an existing dataset is not a normal process flow for data analysis.&lt;/P&gt;
&lt;P&gt;Sounds like you just want to merge the datasets by the three key variables and set FLAG to 3 when there is a match.&lt;/P&gt;
&lt;P&gt;So let's make some actual datasets from your posted listings.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data A ;
  input var1 var2 $ var3 expect;
cards;
1 April 12 1
2 May   14 2
3 June  15 2
4 July  17 3
;

data B;
  input var1 var2 $ var3;
cards;
1 April 13
4 July  17
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Now a simple merge will let us test if the records in A are also in B or not.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want ;
  merge A(in=ina) B(in=inb);
  by var1-var3;
  if ina;
  if inb then flag=3;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Results&lt;/P&gt;
&lt;PRE&gt;Obs    var1    var2     var3    expect    flag

 1       1     April     12        1        .
 2       2     May       14        2        .
 3       3     June      15        2        .
 4       4     July      17        3        3
&lt;/PRE&gt;
&lt;P&gt;It is not clear to me where the values of 1 and 2 that were in your original table A are supposed to come from.&lt;/P&gt;</description>
      <pubDate>Tue, 22 Feb 2022 16:29:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-multiple-subqueries-with-CASE-WHEN/m-p/797854#M313698</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-02-22T16:29:39Z</dc:date>
    </item>
    <item>
      <title>Re: Using multiple subqueries with CASE/WHEN</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-multiple-subqueries-with-CASE-WHEN/m-p/797884#M313706</link>
      <description>&lt;P&gt;Use an EXISTS clause in your UPDATE query:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data A ;
  input var1 var2 $ var3 expect;
cards;
1 April 12 1
2 May   14 2
3 June  15 2
4 July  17 999
;

data B;
  input var1 var2 $ var3;
cards;
1 April 13
4 July  17
;

proc sql;
update A 
set expect = 3 
where exists (select * from B where var1 = A.var1 and var2 = a.var2 and var3=A.var3);
select * from A;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="PGStats_0-1645561420603.png" style="width: 999px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/68748i81CF767539A044CC/image-size/large?v=v2&amp;amp;px=999" role="button" title="PGStats_0-1645561420603.png" alt="PGStats_0-1645561420603.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 22 Feb 2022 20:24:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-multiple-subqueries-with-CASE-WHEN/m-p/797884#M313706</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2022-02-22T20:24:13Z</dc:date>
    </item>
  </channel>
</rss>

