<?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: Proc sql; coalescing IDs and flagging source data, still leading to duplicates in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Proc-sql-coalescing-IDs-and-flagging-source-data-still-leading/m-p/779605#M248330</link>
    <description>Edit: I need to do this through proc sql and not data steps!</description>
    <pubDate>Wed, 10 Nov 2021 17:03:23 GMT</pubDate>
    <dc:creator>rdum96</dc:creator>
    <dc:date>2021-11-10T17:03:23Z</dc:date>
    <item>
      <title>Proc sql; coalescing IDs and flagging source data, still leading to duplicates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proc-sql-coalescing-IDs-and-flagging-source-data-still-leading/m-p/779603#M248328</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;So I'm merging three sources and trying to stack the unique IDs and also flag which table(s) they were found in. However it's leading to duplicate IDs and flags on separate rows.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Input:&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;Table A&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Table B&amp;nbsp; &amp;nbsp; &amp;nbsp; Table C&lt;/P&gt;&lt;P&gt;ID&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; ID&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;ID&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2&lt;/P&gt;&lt;P&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 4&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;3&lt;/P&gt;&lt;P&gt;3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 9&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;8&lt;/P&gt;&lt;P&gt;4&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Expected output:&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;ID&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;in_A&amp;nbsp; &amp;nbsp; &amp;nbsp;in_B&amp;nbsp; &amp;nbsp; &amp;nbsp;in_C&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 0&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 0&lt;/P&gt;&lt;P&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1&lt;/P&gt;&lt;P&gt;3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 0&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1&lt;/P&gt;&lt;P&gt;4&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 0&lt;/P&gt;&lt;P&gt;8&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;0&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 0&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1&lt;/P&gt;&lt;P&gt;9&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;0&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 0&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Instead I am getting the following output&lt;/STRONG&gt;:&lt;/P&gt;&lt;P&gt;ID&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;in_A&amp;nbsp; &amp;nbsp; &amp;nbsp;in_B&amp;nbsp; &amp;nbsp; &amp;nbsp;in_C&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 0&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 0&lt;/P&gt;&lt;P&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 0&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 0&lt;/P&gt;&lt;P&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;0&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 0&lt;/P&gt;&lt;P&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;0&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 0&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1&lt;/P&gt;&lt;P&gt;3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 0&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 0&lt;/P&gt;&lt;P&gt;3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;0&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 0&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1&lt;/P&gt;&lt;P&gt;4&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 0&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 0&lt;/P&gt;&lt;P&gt;4&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;0&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 0&lt;/P&gt;&lt;P&gt;8&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;0&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 0&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1&lt;/P&gt;&lt;P&gt;9&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;0&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 0&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;proc sql;
   create table all_ids as select
   distinct coalesce(a.ID,b.ID,c.ID) as all_ID, 
   (case when calculated all_ID=a.ID then 1 else 0 end) as in_A,
   (case when calculated all_ID=b.ID then 1 else 0 end) as in_B,
   (case when calculated all_ID=c.ID then 1 else 0 end) as in_C
   from table_A as a full join table_B as b on a.ID=b.ID full join table_C as c on a.ID=c.ID;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 10 Nov 2021 18:04:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proc-sql-coalescing-IDs-and-flagging-source-data-still-leading/m-p/779603#M248328</guid>
      <dc:creator>rdum96</dc:creator>
      <dc:date>2021-11-10T18:04:07Z</dc:date>
    </item>
    <item>
      <title>Re: Proc sql; coalescing IDs and flagging source data, still leading to duplicates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proc-sql-coalescing-IDs-and-flagging-source-data-still-leading/m-p/779605#M248330</link>
      <description>Edit: I need to do this through proc sql and not data steps!</description>
      <pubDate>Wed, 10 Nov 2021 17:03:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proc-sql-coalescing-IDs-and-flagging-source-data-still-leading/m-p/779605#M248330</guid>
      <dc:creator>rdum96</dc:creator>
      <dc:date>2021-11-10T17:03:23Z</dc:date>
    </item>
    <item>
      <title>Re: Proc sql; coalescing IDs and flagging source data, still leading to duplicates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proc-sql-coalescing-IDs-and-flagging-source-data-still-leading/m-p/779612#M248334</link>
      <description>&lt;P&gt;It is extremely likely that you are seeing the result of a previous run of code as when I run your code the sql shows this in the Log:&lt;/P&gt;
&lt;PRE&gt;91   proc sql;
92      create table all_ids as select
93      distinct coalesce(a.ID,b.ID,c.ID) as all_ID,
94      (case when calculated all_ID=a.ID then 1 else 0 end) as in_A,
95      (case when calculated all_ID=b.ID then 1 else 0 end) as in_B,
96      (case when calculated all_ID=c.ID then 1 else 0 end) as in_C
97      from table_A as a full join table B as b on a.ID=b.ID full join table as c on a.ID=c.ID;
                                            --
                                            73
                                            201
ERROR 73-322: Expecting an ON.

ERROR 201-322: The option is not recognized and will be ignored.

98   quit;
&lt;/PRE&gt;
&lt;P&gt;Which means if you have a data set named All_ids it was created in a previous step as the SQL you show has an error.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Why is there a requirement to use SQL? Does the "requirement" require a single SQL select? Other rules not stated?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Note that the DISTINCT applies to ALL values in the select clause. So you are going to get each combination of the 1/0 values to appear&lt;/P&gt;</description>
      <pubDate>Wed, 10 Nov 2021 17:53:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proc-sql-coalescing-IDs-and-flagging-source-data-still-leading/m-p/779612#M248334</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2021-11-10T17:53:46Z</dc:date>
    </item>
    <item>
      <title>Re: Proc sql; coalescing IDs and flagging source data, still leading to duplicates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proc-sql-coalescing-IDs-and-flagging-source-data-still-leading/m-p/779617#M248337</link>
      <description>&lt;P&gt;Ugh, I didn't name my tables correctly in the first post. I've edited now, table b is table_b and table is table_c. I'm not getting the same errors in my log.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 10 Nov 2021 18:05:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proc-sql-coalescing-IDs-and-flagging-source-data-still-leading/m-p/779617#M248337</guid>
      <dc:creator>rdum96</dc:creator>
      <dc:date>2021-11-10T18:05:22Z</dc:date>
    </item>
    <item>
      <title>Re: Proc sql; coalescing IDs and flagging source data, still leading to duplicates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proc-sql-coalescing-IDs-and-flagging-source-data-still-leading/m-p/779625#M248343</link>
      <description>&lt;P&gt;Still think there may be something on your end.&lt;/P&gt;
&lt;P&gt;I run this, where I have taken the time to actually provide data sets:&lt;/P&gt;
&lt;PRE&gt;data table_A ;
  input ID;
datalines;
1                
2                
3                
4
;

data table_B ;  
  input  ID   ;
datalines;
2   
4   
9  
; 

data table_C;
  input ID;
datalines;
2
3
8
;

proc sql;
   create table all_ids as select
   distinct coalesce(a.ID,b.ID,c.ID) as all_ID, 
   (case when calculated all_ID=a.ID then 1 else 0 end) as in_A,
   (case when calculated all_ID=b.ID then 1 else 0 end) as in_B,
   (case when calculated all_ID=c.ID then 1 else 0 end) as in_C
   from table_A as a full join table_B as b on a.ID=b.ID full join table_c as c on a.ID=c.ID;
quit;&lt;/PRE&gt;
&lt;P&gt;And get this result:&lt;/P&gt;
&lt;PRE&gt;Obs    all_ID    in_A    in_B    in_C

 1        1        1       0       0
 2        2        1       1       1
 3        3        1       0       1
 4        4        1       1       0
 5        8        0       0       1
 6        9        0       1       0

&lt;/PRE&gt;
&lt;P&gt;If you aren't then one (or possibly more) of your data sets is not as you present it.&lt;/P&gt;</description>
      <pubDate>Wed, 10 Nov 2021 18:53:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proc-sql-coalescing-IDs-and-flagging-source-data-still-leading/m-p/779625#M248343</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2021-11-10T18:53:33Z</dc:date>
    </item>
  </channel>
</rss>

