<?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 left join no duplicates in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Proc-sql-left-join-no-duplicates/m-p/719240#M222677</link>
    <description>Why use SQL when a data step implements the required logic so cleanly?</description>
    <pubDate>Sun, 14 Feb 2021 22:48:33 GMT</pubDate>
    <dc:creator>ChrisNZ</dc:creator>
    <dc:date>2021-02-14T22:48:33Z</dc:date>
    <item>
      <title>Proc sql left join no duplicates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proc-sql-left-join-no-duplicates/m-p/719011#M222557</link>
      <description>Hi all,&lt;BR /&gt;I am trying to create an indicator on table1 based on information on table2.&lt;BR /&gt;Table1&lt;BR /&gt;Patient_id comp_id date&lt;BR /&gt;123 A111 1/2/2021&lt;BR /&gt;234 A124 1/3/2021&lt;BR /&gt;567 A234 1/4/2021&lt;BR /&gt;&lt;BR /&gt;Table2&lt;BR /&gt;Patient_id Comp_id Date State&lt;BR /&gt;123 A111 1/5/2020 AZ&lt;BR /&gt;123 A111 1/4/2020 AZ&lt;BR /&gt;123 A111 2/5/2021 PA&lt;BR /&gt;234 A124 1/3/2021 NY&lt;BR /&gt;So, I want to create a column called indicator in table1 and gave indicator value of 1 if the same patient and company is present in table2 and date in table1 is less than any date for that condition in table2 and state is AZ. That means only first row in table1 will have indicator of 1. I tried few ways but got duplicate in some cases. What may be the best way to achieve this with proc sql?</description>
      <pubDate>Fri, 12 Feb 2021 21:06:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proc-sql-left-join-no-duplicates/m-p/719011#M222557</guid>
      <dc:creator>nickspencer</dc:creator>
      <dc:date>2021-02-12T21:06:01Z</dc:date>
    </item>
    <item>
      <title>Re: Proc sql left join no duplicates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proc-sql-left-join-no-duplicates/m-p/719024#M222565</link>
      <description>&lt;P&gt;I think this is pretty simple in a data step. Make sure table1 and table2 are sorted by PATIENT_ID/COMP_ID.&amp;nbsp; Then:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  do until (last.comp_id);
    merge table1 table2 (rename=(date=date2));
    by patient_id comp_id;
    if state='AZ' and date2&amp;lt;date then indicator=1;
  end;
  drop date2 state;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;This assumes there is only one obs per PATIENT_ID/COMP_ID in table 1.&lt;/P&gt;</description>
      <pubDate>Fri, 12 Feb 2021 21:31:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proc-sql-left-join-no-duplicates/m-p/719024#M222565</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2021-02-12T21:31:34Z</dc:date>
    </item>
    <item>
      <title>Re: Proc sql left join no duplicates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proc-sql-left-join-no-duplicates/m-p/719026#M222567</link>
      <description>&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31461"&gt;@mkeintz&lt;/a&gt; this is great and there is only one patiend_id/comp_id. But is there a way to achieve the same in proc sql?</description>
      <pubDate>Fri, 12 Feb 2021 21:36:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proc-sql-left-join-no-duplicates/m-p/719026#M222567</guid>
      <dc:creator>nickspencer</dc:creator>
      <dc:date>2021-02-12T21:36:24Z</dc:date>
    </item>
    <item>
      <title>Re: Proc sql left join no duplicates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proc-sql-left-join-no-duplicates/m-p/719030#M222570</link>
      <description>&lt;P&gt;What do you mean by duplicate?&amp;nbsp; The way in SQL to avoid duplicate is either DISTINCT keyword or GROUP BY.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data one;
  input patient_id $ comp_id $ date :mmddyy.;
  format date yymmdd10.;
cards;
123 A111 1/2/2021
234 A124 1/3/2021
567 A234 1/4/2021

data two;
  input patient_id $ comp_id $ date :mmddyy. state $ ;
  format date yymmdd10.;
cards;
123 A111 1/5/2020 AZ
123 A111 1/4/2020 AZ
123 A111 2/5/2021 PA
234 A124 1/3/2021 NY
;

proc sql ;
create table want as
  select a.patient_id, a.comp_id
       , max(b.state='AZ' ) as any_AZ
  from one a 
  left join two b
    on a.patient_id = b.patient_id
    and a.comp_id = b.comp_id 
    and a.date &amp;gt; b.date
  group by 1,2
;
quit;

proc print data=want;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;       patient_
Obs       id       comp_id    any_AZ

 1       123        A111         1
 2       234        A124         0
 3       567        A234         0

&lt;/PRE&gt;</description>
      <pubDate>Fri, 12 Feb 2021 21:59:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proc-sql-left-join-no-duplicates/m-p/719030#M222570</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-02-12T21:59:51Z</dc:date>
    </item>
    <item>
      <title>Re: Proc sql left join no duplicates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proc-sql-left-join-no-duplicates/m-p/719035#M222571</link>
      <description>&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt; this works great except that I have number of other columns in table1 to group by along with state column(may have different value). Is there a way to handle that?</description>
      <pubDate>Fri, 12 Feb 2021 22:10:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proc-sql-left-join-no-duplicates/m-p/719035#M222571</guid>
      <dc:creator>nickspencer</dc:creator>
      <dc:date>2021-02-12T22:10:13Z</dc:date>
    </item>
    <item>
      <title>Re: Proc sql left join no duplicates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proc-sql-left-join-no-duplicates/m-p/719058#M222582</link>
      <description>&lt;P&gt;I have no idea what you mean.&amp;nbsp; Please show a clearer example with a least a couple of these other "&lt;SPAN&gt;columns" (did you mean input variables? output variables? levels of one or more of the input variables? something else?)&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 13 Feb 2021 00:03:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proc-sql-left-join-no-duplicates/m-p/719058#M222582</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-02-13T00:03:21Z</dc:date>
    </item>
    <item>
      <title>Re: Proc sql left join no duplicates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proc-sql-left-join-no-duplicates/m-p/719097#M222612</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data one;
  input patient_id $ comp_id $ date :mmddyy.;
  format date yymmdd10.;
cards;
123 A111 1/2/2021
234 A124 1/3/2021
567 A234 1/4/2021

data two;
  input patient_id $ comp_id $ date :mmddyy. state $ ;
  format date yymmdd10.;
cards;
123 A111 1/5/2020 AZ
123 A111 1/4/2020 AZ
123 A111 2/5/2021 PA
234 A124 1/3/2021 NY
;

proc sql ;
create table want as
  select a.*,
   exists(select * from two where patient_id=a.patient_id and comp_id=a.comp_id and state='AZ' and date&amp;lt;a.date) as flag
  from one a 
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 13 Feb 2021 11:57:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proc-sql-left-join-no-duplicates/m-p/719097#M222612</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2021-02-13T11:57:21Z</dc:date>
    </item>
    <item>
      <title>Re: Proc sql left join no duplicates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proc-sql-left-join-no-duplicates/m-p/719120#M222620</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/206798"&gt;@nickspencer&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;Hi all,&lt;BR /&gt;I am trying to create an indicator on table1 based on information on table2.&lt;BR /&gt;Table1&lt;BR /&gt;Patient_id comp_id date&lt;BR /&gt;123 A111 1/2/2021&lt;BR /&gt;234 A124 1/3/2021&lt;BR /&gt;567 A234 1/4/2021&lt;BR /&gt;&lt;BR /&gt;Table2&lt;BR /&gt;Patient_id Comp_id Date State&lt;BR /&gt;123 A111 1/5/2020 AZ&lt;BR /&gt;123 A111 1/4/2020 AZ&lt;BR /&gt;123 A111 2/5/2021 PA&lt;BR /&gt;234 A124 1/3/2021 NY&lt;BR /&gt;So, I want to create a column called indicator in table1 and gave indicator value of 1 if the same patient and company is present in table2 and date in table1 is less than any date for that condition in table2 and state is AZ. That means only first row in table1 will have indicator of 1. I tried few ways but got duplicate in some cases. What may be the best way to achieve this with proc sql?&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;LARGE ECONOMY SIZED HINT: If you want someone to improve on your code &lt;STRONG&gt;provide the code.&lt;/STRONG&gt;&lt;/P&gt;</description>
      <pubDate>Sat, 13 Feb 2021 16:32:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proc-sql-left-join-no-duplicates/m-p/719120#M222620</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2021-02-13T16:32:32Z</dc:date>
    </item>
    <item>
      <title>Re: Proc sql left join no duplicates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proc-sql-left-join-no-duplicates/m-p/719240#M222677</link>
      <description>Why use SQL when a data step implements the required logic so cleanly?</description>
      <pubDate>Sun, 14 Feb 2021 22:48:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proc-sql-left-join-no-duplicates/m-p/719240#M222677</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2021-02-14T22:48:33Z</dc:date>
    </item>
  </channel>
</rss>

