<?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: Finding common value from two datasets in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Finding-common-value-from-two-datasets/m-p/879246#M347362</link>
    <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
    create table want as select 
        coalesce(a.icustomer,b.icustomer) as icustomer
        ,a.accountnumber
        ,b.debt_code
        ,coalesce(a.rep_code,b.rep_code) as rep_code
        ,coalesce(a.name,b.dr_inits) as name
        ,coalesce(a.surname,b.name) as surname
        ,case when a.name=b.dr_inits and a.surname=b.name then 'Yes' else 'No' end as compare
        from test_1 as a full join test_2 as b
        on a.name=b.dr_inits and a.surname=b.name;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Mon, 05 Jun 2023 13:39:33 GMT</pubDate>
    <dc:creator>PaigeMiller</dc:creator>
    <dc:date>2023-06-05T13:39:33Z</dc:date>
    <item>
      <title>Finding common value from two datasets</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Finding-common-value-from-two-datasets/m-p/879244#M347361</link>
      <description>&lt;P&gt;Hi all,&lt;/P&gt;
&lt;P&gt;I have two datasets. Test1 contains all the account information in 131 repcodes. Test2 Datasets have all the linked accounts from 131 repcodes. So I want to find if the name in linked accounts (Test2) matches with Test1 data then output result should show 'Yes' else 'No'. Can you please suggest the best way to do it? I tried the&amp;nbsp; case when statement but then it only gives me Yes/No information from Test1(only the accounts in Test1). I would like the output data to show all the accountnumbers (Test1 and Test2) and if the name and surname matches with the accountnumber in 131 repcode then Yes, if it doesn't then No.&lt;/P&gt;
&lt;P&gt;Here are the two datasets:&lt;/P&gt;
&lt;P&gt;Data Test_1;&lt;BR /&gt;infile cards expandtabs;&lt;BR /&gt;input icustomer accountnumber rep_code Name $ Surname $;&lt;BR /&gt;datalines ;&lt;BR /&gt;452646 24648463 131 Tom Thomas&lt;BR /&gt;523482 24647866 131 Diva Marl&lt;BR /&gt;25462 47561365 131 Amy Jackson&lt;BR /&gt;324648 24679591 131 Dave Harrison&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;Data Test_2;&lt;BR /&gt;infile cards expandtabs;&lt;BR /&gt;input icustomer debt_code rep_code Dr_inits $ Name $;&lt;BR /&gt;datalines ;&lt;BR /&gt;452646 24648465 139 Tom Thomas&lt;BR /&gt;452646 24645647 241 Tom Thomas&lt;BR /&gt;523482 24647864 135 Diva Marl&lt;BR /&gt;564612 35464841 294 Alis Cook&lt;BR /&gt;646823 34625461 151 Joe Root&lt;BR /&gt;2468764 26574646 502 Martin Luthis&lt;BR /&gt;25462 47561364 168 Amy Jackson&lt;BR /&gt;324648 24679597 505 Dave Harrison&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;Thanks&lt;/P&gt;</description>
      <pubDate>Mon, 05 Jun 2023 13:35:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Finding-common-value-from-two-datasets/m-p/879244#M347361</guid>
      <dc:creator>Sandeep77</dc:creator>
      <dc:date>2023-06-05T13:35:51Z</dc:date>
    </item>
    <item>
      <title>Re: Finding common value from two datasets</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Finding-common-value-from-two-datasets/m-p/879246#M347362</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
    create table want as select 
        coalesce(a.icustomer,b.icustomer) as icustomer
        ,a.accountnumber
        ,b.debt_code
        ,coalesce(a.rep_code,b.rep_code) as rep_code
        ,coalesce(a.name,b.dr_inits) as name
        ,coalesce(a.surname,b.name) as surname
        ,case when a.name=b.dr_inits and a.surname=b.name then 'Yes' else 'No' end as compare
        from test_1 as a full join test_2 as b
        on a.name=b.dr_inits and a.surname=b.name;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 05 Jun 2023 13:39:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Finding-common-value-from-two-datasets/m-p/879246#M347362</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2023-06-05T13:39:33Z</dc:date>
    </item>
    <item>
      <title>Re: Finding common value from two datasets</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Finding-common-value-from-two-datasets/m-p/879247#M347363</link>
      <description>&lt;P&gt;What is it you want to match on?&amp;nbsp; Is it the ICUSTOMER variable?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data Test_1;
  input icustomer accountnumber rep_code Name $ Surname $;
datalines ;
452646 24648463 131 Tom Thomas
523482 24647866 131 Diva Marl
25462  47561365 131 Amy Jackson
324648 24679591 131 Dave Harrison
;

data Test_2;
  input icustomer debt_code rep_code Dr_inits $ Name $;
datalines ;
452646  24648465 139 Tom Thomas
452646  24645647 241 Tom Thomas
523482  24647864 135 Diva Marl
564612  35464841 294 Alis Cook
646823  34625461 151 Joe Root
2468764 26574646 502 Martin Luthis
25462   47561364 168 Amy Jackson
324648  24679597 505 Dave Harrison
;

proc sql;
create table want as 
select coalesce(a.icustomer,b.icustomer) as icustomer
     , a.accountnumber
     , a.rep_code as rep_code1
     , a.name as first_name1
     , a.surname as surname as last_name1
     , b.debt_code
     , b.rep_code as rep_code2
     , b.dr_inits as first_name2
     , b.name as last_name2
     , case when (a.surname=b.name and a.name=b.dr_inits) then 'YES' else 'NO' end as Match
from test_1 a
full join test_2 b
   on a.icustomer=b.icustomer
  and a.surname=b.name
  and a.name=b.dr_inits
order by 1,2
;

quit;

proc print;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;                                      rep_    first_    last_         debt_      rep_    first_    last_
Obs    icustomer    accountnumber    code1    name1     name1         code      code2    name2     name2       Match

 1        25462        47561365       131      Amy      Jackson     47561364     168     Amy       Jackson      YES
 2       324648        24679591       131      Dave     Harrison    24679597     505     Dave      Harrison     YES
 3       452646        24648463       131      Tom      Thomas      24648465     139     Tom       Thomas       YES
 4       452646        24648463       131      Tom      Thomas      24645647     241     Tom       Thomas       YES
 5       523482        24647866       131      Diva     Marl        24647864     135     Diva      Marl         YES
 6       564612               .         .                           35464841     294     Alis      Cook         NO
 7       646823               .         .                           34625461     151     Joe       Root         NO
 8      2468764               .         .                           26574646     502     Martin    Luthis       NO
&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 05 Jun 2023 13:53:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Finding-common-value-from-two-datasets/m-p/879247#M347363</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-06-05T13:53:22Z</dc:date>
    </item>
    <item>
      <title>Re: Finding common value from two datasets</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Finding-common-value-from-two-datasets/m-p/879249#M347364</link>
      <description>Thank you &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;. It worked but I don't know why it is showing more records. I have total 76k accounts but when I ran this code, it shows around 100K records. Do you know what could be the reason for this?</description>
      <pubDate>Mon, 05 Jun 2023 14:14:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Finding-common-value-from-two-datasets/m-p/879249#M347364</guid>
      <dc:creator>Sandeep77</dc:creator>
      <dc:date>2023-06-05T14:14:19Z</dc:date>
    </item>
    <item>
      <title>Re: Finding common value from two datasets</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Finding-common-value-from-two-datasets/m-p/879250#M347365</link>
      <description>&lt;P&gt;That is the nature of an SQL join.&amp;nbsp; &amp;nbsp;It 2 observations from one dataset match 4 observations form the other the other you get 2*4 = 8 observations, not 2+4 or max(2,4) observations you might get with a SET or MERGE statement.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For example for this particular query if the same ICUSTOMER has two different names then you will get two different output observations.&lt;/P&gt;</description>
      <pubDate>Mon, 05 Jun 2023 14:26:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Finding-common-value-from-two-datasets/m-p/879250#M347365</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-06-05T14:26:55Z</dc:date>
    </item>
    <item>
      <title>Re: Finding common value from two datasets</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Finding-common-value-from-two-datasets/m-p/879251#M347366</link>
      <description>&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt; The reason for accounts to be repeated is the accountnumber is repeated for some records where they do not have linked accounts outside 131 repcode. I tried using distinct a.accountnumber to avoid this issue but it shows syntax error. Can you suggest how can I avoid duplicate accountnumber?</description>
      <pubDate>Mon, 05 Jun 2023 14:30:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Finding-common-value-from-two-datasets/m-p/879251#M347366</guid>
      <dc:creator>Sandeep77</dc:creator>
      <dc:date>2023-06-05T14:30:52Z</dc:date>
    </item>
    <item>
      <title>Re: Finding common value from two datasets</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Finding-common-value-from-two-datasets/m-p/879252#M347367</link>
      <description>&lt;P&gt;If the issue is that observations from TEST_1 have the same other values except for ACCOUNTNUMBER and you just want to take the first ACCOUNTNUMBER then I would do that before trying to combine the datasets.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;That is not something that SQL is very good at.&amp;nbsp; It would be better to do that with a data step.&lt;/P&gt;</description>
      <pubDate>Mon, 05 Jun 2023 14:35:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Finding-common-value-from-two-datasets/m-p/879252#M347367</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-06-05T14:35:54Z</dc:date>
    </item>
    <item>
      <title>Re: Finding common value from two datasets</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Finding-common-value-from-two-datasets/m-p/879254#M347368</link>
      <description>&lt;P&gt;Thank you!&lt;/P&gt;</description>
      <pubDate>Mon, 05 Jun 2023 14:39:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Finding-common-value-from-two-datasets/m-p/879254#M347368</guid>
      <dc:creator>Sandeep77</dc:creator>
      <dc:date>2023-06-05T14:39:34Z</dc:date>
    </item>
  </channel>
</rss>

