<?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: How to cross check 2 variables based on condition when joining 2 tables. in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-cross-check-2-variables-based-on-condition-when-joining-2/m-p/800878#M315126</link>
    <description>&lt;P&gt;Hi Paige, Thank you for responding. Here is how my data looks like and my desired output.&lt;/P&gt;
&lt;P&gt;TABLE A&lt;BR /&gt;Account# Name&lt;BR /&gt;A11&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Jon A.&lt;BR /&gt;A11&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Jon A.&lt;BR /&gt;A11&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Mike B.&lt;BR /&gt;A12 &amp;nbsp; &amp;nbsp; &amp;nbsp; Tom C.&lt;/P&gt;
&lt;P&gt;A12&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Tom C.&lt;/P&gt;
&lt;P&gt;A13&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Ed&lt;/P&gt;
&lt;P&gt;TABLE B&lt;BR /&gt;Account# Name&lt;BR /&gt;A11&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Jon A.&lt;BR /&gt;A11&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Jane&lt;BR /&gt;A11&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Mike B.&lt;BR /&gt;A12 &amp;nbsp; &amp;nbsp; &amp;nbsp; Vira&lt;/P&gt;
&lt;P&gt;A12&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Tommy&lt;/P&gt;
&lt;P&gt;A13&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Ed&lt;/P&gt;
&lt;P&gt;A13&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Charles&lt;/P&gt;
&lt;P&gt;A13&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Scott&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;EXPECTED OUTPUT(TABLE C)&lt;BR /&gt;Account# FLAG&lt;BR /&gt;A11&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1&lt;BR /&gt;A12&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;0&lt;/P&gt;
&lt;P&gt;A13&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1&lt;/P&gt;
&lt;P&gt;The expected output is table C. The account# A11 has flag 1 because we have names(JON A., MIKE B.) in common for both tables A/B. I flagged 0 for account# A12 because we do not have common names in both tables A/B for this particular account#.&lt;/P&gt;
&lt;P&gt;So i was wondering how to approach the issue.&lt;/P&gt;
&lt;P&gt;Hope this helps.&lt;/P&gt;</description>
    <pubDate>Tue, 08 Mar 2022 15:59:53 GMT</pubDate>
    <dc:creator>Banana19</dc:creator>
    <dc:date>2022-03-08T15:59:53Z</dc:date>
    <item>
      <title>How to cross check 2 variables based on condition when joining 2 tables.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-cross-check-2-variables-based-on-condition-when-joining-2/m-p/800842#M315108</link>
      <description>&lt;P&gt;Hi All - As the subject line says I've come across a scenario where I need to cross check variables from 2 different tables based on a condition. I wrote a case when expression but I do not think it works. Below is the example of logic I'm trying to use.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;CASE WHEN (COMPRESS(A.Account_Number)=COMPRESS(B.Account_Number)) AND A.name NOT IN B.name THEN '1'&lt;BR /&gt;END AS FLAG&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In the above example, when Table A account number equals to Table B account number then the name variable present in table A should cross check against name variable of table B for that particular account number. If the name variable in Table A is not present in name variable of Table B for that particular account then I'm creating a flag. This needs to performed for when ever both tables account numbers match.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It would be great if you could help me in this issue.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks,&lt;/P&gt;
&lt;P&gt;SD&lt;/P&gt;</description>
      <pubDate>Tue, 08 Mar 2022 13:20:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-cross-check-2-variables-based-on-condition-when-joining-2/m-p/800842#M315108</guid>
      <dc:creator>Banana19</dc:creator>
      <dc:date>2022-03-08T13:20:34Z</dc:date>
    </item>
    <item>
      <title>Re: How to cross check 2 variables based on condition when joining 2 tables.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-cross-check-2-variables-based-on-condition-when-joining-2/m-p/800843#M315109</link>
      <description>&lt;P&gt;Why do you think it doesn't work? Explain.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Typically, these types of comparisons to get matching account_number values is done in a JOIN rather than in a CASE statement, something like this (partial code)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
    create table want as select a.account_number, /* any other variables you want go here */, 
    case when a.name not in b.name then 1 end as flag
    from a left join b on
        compress(a.account_number)=compress(b.account_number)
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I don't really know about the part &lt;FONT face="courier new,courier"&gt;a.name not in b.name&lt;/FONT&gt;, that doesn't feel right to me either, state clearly what you are trying to achieve with this part of the code&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But you really need to take a step back and instead of looking at this as a case statement issue and explaining it that way, just explain the desired output&lt;/P&gt;</description>
      <pubDate>Tue, 08 Mar 2022 13:33:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-cross-check-2-variables-based-on-condition-when-joining-2/m-p/800843#M315109</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2022-03-08T13:33:23Z</dc:date>
    </item>
    <item>
      <title>Re: How to cross check 2 variables based on condition when joining 2 tables.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-cross-check-2-variables-based-on-condition-when-joining-2/m-p/800878#M315126</link>
      <description>&lt;P&gt;Hi Paige, Thank you for responding. Here is how my data looks like and my desired output.&lt;/P&gt;
&lt;P&gt;TABLE A&lt;BR /&gt;Account# Name&lt;BR /&gt;A11&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Jon A.&lt;BR /&gt;A11&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Jon A.&lt;BR /&gt;A11&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Mike B.&lt;BR /&gt;A12 &amp;nbsp; &amp;nbsp; &amp;nbsp; Tom C.&lt;/P&gt;
&lt;P&gt;A12&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Tom C.&lt;/P&gt;
&lt;P&gt;A13&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Ed&lt;/P&gt;
&lt;P&gt;TABLE B&lt;BR /&gt;Account# Name&lt;BR /&gt;A11&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Jon A.&lt;BR /&gt;A11&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Jane&lt;BR /&gt;A11&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Mike B.&lt;BR /&gt;A12 &amp;nbsp; &amp;nbsp; &amp;nbsp; Vira&lt;/P&gt;
&lt;P&gt;A12&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Tommy&lt;/P&gt;
&lt;P&gt;A13&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Ed&lt;/P&gt;
&lt;P&gt;A13&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Charles&lt;/P&gt;
&lt;P&gt;A13&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Scott&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;EXPECTED OUTPUT(TABLE C)&lt;BR /&gt;Account# FLAG&lt;BR /&gt;A11&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1&lt;BR /&gt;A12&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;0&lt;/P&gt;
&lt;P&gt;A13&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1&lt;/P&gt;
&lt;P&gt;The expected output is table C. The account# A11 has flag 1 because we have names(JON A., MIKE B.) in common for both tables A/B. I flagged 0 for account# A12 because we do not have common names in both tables A/B for this particular account#.&lt;/P&gt;
&lt;P&gt;So i was wondering how to approach the issue.&lt;/P&gt;
&lt;P&gt;Hope this helps.&lt;/P&gt;</description>
      <pubDate>Tue, 08 Mar 2022 15:59:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-cross-check-2-variables-based-on-condition-when-joining-2/m-p/800878#M315126</guid>
      <dc:creator>Banana19</dc:creator>
      <dc:date>2022-03-08T15:59:53Z</dc:date>
    </item>
    <item>
      <title>Re: How to cross check 2 variables based on condition when joining 2 tables.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-cross-check-2-variables-based-on-condition-when-joining-2/m-p/800910#M315146</link>
      <description>&lt;P&gt;Try this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data a;
infile datalines delimiter = ',' dsd truncover;
input account :$3. name :$10.;
datalines;
A11,Jon A.
A11,Jon A.
A11,Mike B.
A12,Tom C.
A12,Tom C.
A13,Ed
;
run;

data b;
infile datalines delimiter = ',' dsd truncover;
input account :$3. name :$10.;
datalines;
A11,Jon A.
A11,Jane
A11,Mike B.
A12,Vira
A12,Tommy
A13,Ed
A13,Charles
A13,Scott
;
run;

proc sql;
	select
				distinct t1.account,
				case when catx(',', t1.account, t1.name) = catx(',', t2.account, t2.name) then 1 else 0 end as flag
	from
				a as t1
					left join
				b as t2
					on	catx(',', t1.account, t1.name) = catx(',', t2.account, t2.name);
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="maguiremq_0-1646760265106.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/69257iF937AFF55F258790/image-size/medium?v=v2&amp;amp;px=400" role="button" title="maguiremq_0-1646760265106.png" alt="maguiremq_0-1646760265106.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;May be a better way but that works for me.&lt;/P&gt;</description>
      <pubDate>Tue, 08 Mar 2022 17:24:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-cross-check-2-variables-based-on-condition-when-joining-2/m-p/800910#M315146</guid>
      <dc:creator>maguiremq</dc:creator>
      <dc:date>2022-03-08T17:24:33Z</dc:date>
    </item>
    <item>
      <title>Re: How to cross check 2 variables based on condition when joining 2 tables.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-cross-check-2-variables-based-on-condition-when-joining-2/m-p/800925#M315153</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/89915"&gt;@Banana19&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So you want to get &lt;EM&gt;one&lt;/EM&gt; observation per matching account number? Then, using the definition of the flag from your second post (i.e., flag=1 if at least one common name is found), I'd suggest an inner join with a GROUP BY clause:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have_a;
input account $ name &amp;amp;$10.;
cards;
A10       Rahul
A11       Jon A.
A11       Jon A.
A11       Mike B.
A12       Tom C.
A12       Tom C.
A13       Ed
A13       John Doe
;

data have_b;
input account $ name &amp;amp;$10.;
cards;
A11       Jon A.
A11       Jane
A11       Mike B.
A12       Vira
A12       Tommy
A13       Ed
A13       Charles
A13       Scott
A14       Reinhard
;

proc sql;
create table want as
select a.account, max(a.name=b.name) as flag
from have_a a join have_b b
on a.account=b.account
group by a.account;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I've added a few records to the sample data so that different types of joins would create different results. Note that account A13 (with the new "John Doe" record added) still occurs only once in the output dataset.&lt;/P&gt;</description>
      <pubDate>Tue, 08 Mar 2022 18:26:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-cross-check-2-variables-based-on-condition-when-joining-2/m-p/800925#M315153</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2022-03-08T18:26:33Z</dc:date>
    </item>
    <item>
      <title>Re: How to cross check 2 variables based on condition when joining 2 tables.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-cross-check-2-variables-based-on-condition-when-joining-2/m-p/801048#M315223</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have_a;
input account $ name &amp;amp;$10.;
cards;
A10       Rahul
A11       Jon A.
A11       Jon A.
A11       Mike B.
A12       Tom C.
A12       Tom C.
A13       Ed
A13       John Doe
;

data have_b;
input account $ name &amp;amp;$10.;
cards;
A11       Jon A.
A11       Jane
A11       Mike B.
A12       Vira
A12       Tommy
A13       Ed
A13       Charles
A13       Scott
A14       Reinhard
;

proc sql;
create table want as
select a.account, count(b.name) ne 0 as flag
from have_a a left join have_b b 
on a.account=b.account and a.name=b.name
group by a.account;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 09 Mar 2022 11:53:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-cross-check-2-variables-based-on-condition-when-joining-2/m-p/801048#M315223</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2022-03-09T11:53:28Z</dc:date>
    </item>
  </channel>
</rss>

