<?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 join on multiple variable selecting matched variable only once in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/join-on-multiple-variable-selecting-matched-variable-only-once/m-p/298009#M60313</link>
    <description>&lt;P&gt;Dear experts,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I should merge two data sets on two variables but taking the observation on the left part of the join only once. The output below generated should have for the second and fourth observation a value=""&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;DATA data1;&lt;BR /&gt;INPUT code $2. location $3. location2 $4.;&lt;BR /&gt;DATALINES;&lt;BR /&gt;61 UK ABC&lt;BR /&gt;45 CH DEF&lt;BR /&gt;03 CH DEF&lt;BR /&gt;11 JP GHI&lt;BR /&gt;14 JP GHI&lt;BR /&gt;;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;DATA data2;&lt;BR /&gt;INPUT value $3. location $3. location2 $4.;&lt;BR /&gt;DATALINES;&lt;BR /&gt;100 UK ABC&lt;BR /&gt;200 CH DEF&lt;BR /&gt;300 JP GHI&lt;BR /&gt;;&lt;/P&gt;&lt;P&gt;proc sql; create table merge as select t0.*, t1.* from data1 t0 left outer join data2 t1&lt;BR /&gt;on (t1.location=t0.location and t1.location2=t0.location2) ;quit;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks a lo in advance. BRs, SH&lt;/P&gt;</description>
    <pubDate>Tue, 13 Sep 2016 12:51:59 GMT</pubDate>
    <dc:creator>Sir_Highbury</dc:creator>
    <dc:date>2016-09-13T12:51:59Z</dc:date>
    <item>
      <title>join on multiple variable selecting matched variable only once</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/join-on-multiple-variable-selecting-matched-variable-only-once/m-p/298009#M60313</link>
      <description>&lt;P&gt;Dear experts,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I should merge two data sets on two variables but taking the observation on the left part of the join only once. The output below generated should have for the second and fourth observation a value=""&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;DATA data1;&lt;BR /&gt;INPUT code $2. location $3. location2 $4.;&lt;BR /&gt;DATALINES;&lt;BR /&gt;61 UK ABC&lt;BR /&gt;45 CH DEF&lt;BR /&gt;03 CH DEF&lt;BR /&gt;11 JP GHI&lt;BR /&gt;14 JP GHI&lt;BR /&gt;;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;DATA data2;&lt;BR /&gt;INPUT value $3. location $3. location2 $4.;&lt;BR /&gt;DATALINES;&lt;BR /&gt;100 UK ABC&lt;BR /&gt;200 CH DEF&lt;BR /&gt;300 JP GHI&lt;BR /&gt;;&lt;/P&gt;&lt;P&gt;proc sql; create table merge as select t0.*, t1.* from data1 t0 left outer join data2 t1&lt;BR /&gt;on (t1.location=t0.location and t1.location2=t0.location2) ;quit;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks a lo in advance. BRs, SH&lt;/P&gt;</description>
      <pubDate>Tue, 13 Sep 2016 12:51:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/join-on-multiple-variable-selecting-matched-variable-only-once/m-p/298009#M60313</guid>
      <dc:creator>Sir_Highbury</dc:creator>
      <dc:date>2016-09-13T12:51:59Z</dc:date>
    </item>
    <item>
      <title>Re: join on multiple variable selecting matched variable only once</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/join-on-multiple-variable-selecting-matched-variable-only-once/m-p/298013#M60314</link>
      <description>&lt;P&gt;That'snot how SQL works. You requirement is row (observation) oriented, while&amp;nbsp;SQL is column oriented.&lt;/P&gt;
&lt;P&gt;SQL first performs a Cartesian&amp;nbsp;join (in theory) and the filters using the on (or where criteria when used).&lt;/P&gt;
&lt;P&gt;You need to use a data&amp;nbsp;step to solve this.&lt;/P&gt;
&lt;P&gt;Perhaps you need a better data model so you can describe&amp;nbsp;which is the first observation in the "join group" (in case of resorting for instance), and why.&lt;/P&gt;</description>
      <pubDate>Tue, 13 Sep 2016 13:04:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/join-on-multiple-variable-selecting-matched-variable-only-once/m-p/298013#M60314</guid>
      <dc:creator>LinusH</dc:creator>
      <dc:date>2016-09-13T13:04:48Z</dc:date>
    </item>
    <item>
      <title>Re: join on multiple variable selecting matched variable only once</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/join-on-multiple-variable-selecting-matched-variable-only-once/m-p/298015#M60315</link>
      <description>&lt;P&gt;Thats not a logical join though. &amp;nbsp;There is no way of knowing, from the data, that any row is "first". &amp;nbsp;This is information you need to put in before joining, and example given below. &amp;nbsp;Note alo that the use of * causes warnings as variables appear in multiple tables - its not a good idea to use this syntax.&lt;/P&gt;
&lt;PRE&gt;data data1;
  input code $2. location $3. location2 $4. first;
datalines;
61 UK ABC 1
45 CH DEF 1
03 CH DEF 0
11 JP GHI 1
14 JP GHI 0
;
run;

data data2;
  input value $3. location $3. location2 $4. first;
datalines;
100 UK ABC 1
200 CH DEF 1
300 JP GHI 1
; 
run;

proc sql; 
  create table MERGE as 
  select  T0.*, 
          T1.* 
  from    DATA1 T0 
  left join DATA2 T1
  on      T1.LOCATION=T0.LOCATION 
  and     T1.LOCATION2=T0.LOCATION2
  and     T1.FIRST=T0.FIRST;
quit;&lt;/PRE&gt;</description>
      <pubDate>Tue, 13 Sep 2016 13:06:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/join-on-multiple-variable-selecting-matched-variable-only-once/m-p/298015#M60315</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2016-09-13T13:06:56Z</dc:date>
    </item>
    <item>
      <title>Re: join on multiple variable selecting matched variable only once</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/join-on-multiple-variable-selecting-matched-variable-only-once/m-p/298017#M60316</link>
      <description>&lt;P&gt;Try the data step method:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=data1;
by location location2;
run;

proc sort data=data2;
by location location2;
run;

data merge2;
merge
  data1 (in=a)
  data2 (in=b)
;
by location location2;
if a;
if first.location2;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You might want to consider additional sort criteria for data1 to put the desired code first.&lt;/P&gt;</description>
      <pubDate>Tue, 13 Sep 2016 13:08:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/join-on-multiple-variable-selecting-matched-variable-only-once/m-p/298017#M60316</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2016-09-13T13:08:45Z</dc:date>
    </item>
    <item>
      <title>Re: join on multiple variable selecting matched variable only once</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/join-on-multiple-variable-selecting-matched-variable-only-once/m-p/298039#M60317</link>
      <description>&lt;P&gt;The DATA step is a workable approach here.&amp;nbsp; SQL does not guarantee any "order" to the incoming records.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here are a couple of variations to consider on the DATA step approach.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Instead of sorting/merging by LOCATION LOCATION2, you could sort/merge by LOCATION2 LOCATION.&amp;nbsp; In the sample data you have provided, that would preserve the current order of the observations (and could conceivably eliminate the need for sorting).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For the final statement, you could consider this variation:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;if first.location2=0 then value=' ';&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;That would keep all records, but reset VALUE to a blank value.&lt;/P&gt;</description>
      <pubDate>Tue, 13 Sep 2016 13:47:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/join-on-multiple-variable-selecting-matched-variable-only-once/m-p/298039#M60317</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2016-09-13T13:47:47Z</dc:date>
    </item>
    <item>
      <title>Re: join on multiple variable selecting matched variable only once</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/join-on-multiple-variable-selecting-matched-variable-only-once/m-p/298266#M60327</link>
      <description>&lt;P&gt;I just showed that I was not able to do it (using sql) and I asked how to get the output I want from the input I have. Where did I write to do it necessarily in sql? The only implicit constrain is using SAS. Pheraphs what I was doing is not related to any data model?&amp;nbsp;Please try to answer accordingly to the question, thanks anyway for your effort.&lt;/P&gt;</description>
      <pubDate>Wed, 14 Sep 2016 09:58:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/join-on-multiple-variable-selecting-matched-variable-only-once/m-p/298266#M60327</guid>
      <dc:creator>Sir_Highbury</dc:creator>
      <dc:date>2016-09-14T09:58:38Z</dc:date>
    </item>
  </channel>
</rss>

