<?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: Multiple joins by first row in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Multiple-joins-by-first-row/m-p/627258#M185112</link>
    <description>&lt;P&gt;Normally you would do that in two steps. One to combine the datasets that use ITEM and STATE as the keys.&amp;nbsp; Then a second step to combine the datasets that use ITEM as the key.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data one;
 input item $ state $ county $  by_county;
cards;
Apple DC     bb    3
Apple DC     cc     2
Apple MD   aa     4
Pear  VA     cc     6
;

data two;
 input item $ state $ by_state;
cards;
Apple DC    5
Apple MD   4
Pear   VA    6
;

data three; 
  input item $  by_item;
cards;
Apple        9
Pear         6
;

data step1;
  merge one two;
  by item state;
run;

data want;
  merge step1 three;
  by item;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Do you really want the value of BY_ITEM to only appear on one of the multiple rows with that value of ITEM?&lt;/P&gt;
&lt;P&gt;Here is trick to do that.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data step1;
  merge one two;
  by item state;
  output;
  call missing(of _all_);
run;

data want;
  merge step1 three;
  by item;
  output;
  call missing(of _all_);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If (and this is a &lt;STRONG&gt;BIG&lt;/STRONG&gt; if) you are positive that THREE has one and only one observation for every value of ITEM in ONE or TWO then you can try using this to combine in one step.&amp;nbsp; But I would NOT use it for anything serious. Just as a check to see if it could work.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want ;
  merge one two;
  by item state;
  if first.item then set three;
  output;
  call missing(of _all_);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;To take that crazy idea even further if you are sure that TWO has one and only one observation for every ITEM/STATE combination that is in ONE then your code could become:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want ;
  set one ;
  by item state;
  if first.state then set two;
  if first.item then set three;
  output;
  call missing(of _all_);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 25 Feb 2020 18:04:11 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2020-02-25T18:04:11Z</dc:date>
    <item>
      <title>Multiple joins by first row</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Multiple-joins-by-first-row/m-p/627251#M185107</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have 3 datasets and I hope to join them by first row because they are in different levels.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I assigned the base dataset to be (the other 2 will be left merged to the base dataset):&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;item  state  county  by_county

Apple MD   aa     4
Apple DC     bb    3
Apple DC     cc     2
Pear   VA     cc     6&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;2nd dataset:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt; Item state  by_state

Apple MD   4
Apple DC    5
Pear   VA    6&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;3rd dataset:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;item        by_item
Apple        9
Pear         6&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I'm hoping to join the datasets to have output as the following:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Item   state  county  by_county   by_state  by_item
Apple   MD     aa     4            4        9
Apple   DC     bb     3            5
Apple   DC     cc     2       
Pear    VA     cc     6            6        6&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;You can order by item,state and county to determine the first row.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 25 Feb 2020 17:28:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Multiple-joins-by-first-row/m-p/627251#M185107</guid>
      <dc:creator>lydiawawa</dc:creator>
      <dc:date>2020-02-25T17:28:25Z</dc:date>
    </item>
    <item>
      <title>Re: Multiple joins by first row</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Multiple-joins-by-first-row/m-p/627258#M185112</link>
      <description>&lt;P&gt;Normally you would do that in two steps. One to combine the datasets that use ITEM and STATE as the keys.&amp;nbsp; Then a second step to combine the datasets that use ITEM as the key.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data one;
 input item $ state $ county $  by_county;
cards;
Apple DC     bb    3
Apple DC     cc     2
Apple MD   aa     4
Pear  VA     cc     6
;

data two;
 input item $ state $ by_state;
cards;
Apple DC    5
Apple MD   4
Pear   VA    6
;

data three; 
  input item $  by_item;
cards;
Apple        9
Pear         6
;

data step1;
  merge one two;
  by item state;
run;

data want;
  merge step1 three;
  by item;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Do you really want the value of BY_ITEM to only appear on one of the multiple rows with that value of ITEM?&lt;/P&gt;
&lt;P&gt;Here is trick to do that.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data step1;
  merge one two;
  by item state;
  output;
  call missing(of _all_);
run;

data want;
  merge step1 three;
  by item;
  output;
  call missing(of _all_);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If (and this is a &lt;STRONG&gt;BIG&lt;/STRONG&gt; if) you are positive that THREE has one and only one observation for every value of ITEM in ONE or TWO then you can try using this to combine in one step.&amp;nbsp; But I would NOT use it for anything serious. Just as a check to see if it could work.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want ;
  merge one two;
  by item state;
  if first.item then set three;
  output;
  call missing(of _all_);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;To take that crazy idea even further if you are sure that TWO has one and only one observation for every ITEM/STATE combination that is in ONE then your code could become:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want ;
  set one ;
  by item state;
  if first.state then set two;
  if first.item then set three;
  output;
  call missing(of _all_);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 25 Feb 2020 18:04:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Multiple-joins-by-first-row/m-p/627258#M185112</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-02-25T18:04:11Z</dc:date>
    </item>
  </channel>
</rss>

