<?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: Match with Multiple Occurence of ID in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Match-with-Multiple-Occurence-of-ID/m-p/53166#M11248</link>
    <description>I have always found proc sql the best and easiest way of accomplish many-to-many match merges.&lt;BR /&gt;
&lt;BR /&gt;
However, I don't understand your desired output, either in terms of what is included or how it should be sorted.&lt;BR /&gt;
&lt;BR /&gt;
Does the following do what you are trying to accomplish?:&lt;BR /&gt;
&lt;BR /&gt;
proc sql noprint;&lt;BR /&gt;
  create table want as&lt;BR /&gt;
    select a.ID2, b.*&lt;BR /&gt;
      from datasets1 a, dataset2 b&lt;BR /&gt;
        where a.id=b.id&lt;BR /&gt;
         order by input(substr(ID2,2),best12.) desc, date&lt;BR /&gt;
  ;&lt;BR /&gt;
quit;&lt;BR /&gt;
&lt;BR /&gt;
Art&lt;BR /&gt;
-----------&lt;BR /&gt;
&amp;gt; Hi,&lt;BR /&gt;
&amp;gt; &lt;BR /&gt;
&amp;gt; I want to match  two datasets.&lt;BR /&gt;
&amp;gt; &lt;BR /&gt;
&amp;gt; datasets 1 &lt;BR /&gt;
&amp;gt; ID ID2 &lt;BR /&gt;
&amp;gt; X C25 &lt;BR /&gt;
&amp;gt; X C23&lt;BR /&gt;
&amp;gt; G C24&lt;BR /&gt;
&amp;gt; G C5&lt;BR /&gt;
&amp;gt; D C8&lt;BR /&gt;
&amp;gt; D C9&lt;BR /&gt;
&amp;gt; &lt;BR /&gt;
&amp;gt; data dataset&lt;BR /&gt;
&amp;gt; ID price date&lt;BR /&gt;
&amp;gt; X 0.40 01/01/2000 &lt;BR /&gt;
&amp;gt; X 0.41 01/02/2000&lt;BR /&gt;
&amp;gt; X 0.42 01/03/2000&lt;BR /&gt;
&amp;gt; X 0.40 01/04/2000&lt;BR /&gt;
&amp;gt; X 0.44 01/05/2000&lt;BR /&gt;
&amp;gt; X 0.39 01/06/2000&lt;BR /&gt;
&amp;gt; X 0.20 01/01/2000&lt;BR /&gt;
&amp;gt; D 0.21 01/02/2000&lt;BR /&gt;
&amp;gt; D 0.20 01/03/2000&lt;BR /&gt;
&amp;gt; D 0.23 01/04/2000&lt;BR /&gt;
&amp;gt; D 0.24 01/05/2000&lt;BR /&gt;
&amp;gt; D 0.26 01/06/2000&lt;BR /&gt;
&amp;gt; &lt;BR /&gt;
&amp;gt; &lt;BR /&gt;
&amp;gt; &lt;BR /&gt;
&amp;gt; Where the out pushould  look like this &lt;BR /&gt;
&amp;gt; &lt;BR /&gt;
&amp;gt; ID2 ID price date&lt;BR /&gt;
&amp;gt; C25 X 0.40 01/01/2000 &lt;BR /&gt;
&amp;gt; C25 X 0.41 01/02/2000&lt;BR /&gt;
&amp;gt; C25 X 0.42 01/03/2000&lt;BR /&gt;
&amp;gt; C25 X 0.40 01/04/2000&lt;BR /&gt;
&amp;gt; C25 X 0.44 01/05/2000&lt;BR /&gt;
&amp;gt; C25 X 0.39 01/06/2000&lt;BR /&gt;
&amp;gt; C23 X 0.40 01/01/2000 &lt;BR /&gt;
&amp;gt; C23 X 0.41 01/02/2000&lt;BR /&gt;
&amp;gt; C23 X 0.42 01/03/2000&lt;BR /&gt;
&amp;gt; C23 X 0.40 01/04/2000&lt;BR /&gt;
&amp;gt; C23 X 0.44 01/05/2000&lt;BR /&gt;
&amp;gt; C23 X 0.39 01/06/2000&lt;BR /&gt;
&amp;gt; C9 D 0.21 01/01/2000&lt;BR /&gt;
&amp;gt; C8 D 0.21 01/02/2000&lt;BR /&gt;
&amp;gt; C8 D 0.20 01/03/2000&lt;BR /&gt;
&amp;gt; C8 D 0.23 01/04/2000&lt;BR /&gt;
&amp;gt; C8 D 0.24 01/05/2000&lt;BR /&gt;
&amp;gt; C8 D 0.26 01/06/2000&lt;BR /&gt;
&amp;gt; C9 D 0.21 01/01/2000&lt;BR /&gt;
&amp;gt; C9 D 0.21 01/02/2000&lt;BR /&gt;
&amp;gt; C9 D 0.20 01/03/2000&lt;BR /&gt;
&amp;gt; C9 D 0.23 01/04/2000&lt;BR /&gt;
&amp;gt; C9 D 0.24 01/05/2000&lt;BR /&gt;
&amp;gt; C9 D 0.26 01/06/2000&lt;BR /&gt;
&amp;gt; &lt;BR /&gt;
&amp;gt; The main thing is to ensure the new datsets includes&lt;BR /&gt;
&amp;gt; the ID2 with all the stock price.&lt;BR /&gt;
&amp;gt; &lt;BR /&gt;
&amp;gt; In other words, it is match many-to-many, where I&lt;BR /&gt;
&amp;gt; have multiple occurence for ID in both the datasets.&lt;BR /&gt;
&amp;gt; &lt;BR /&gt;
&amp;gt; I am working on this code right now, but I could not&lt;BR /&gt;
&amp;gt; egt what I want. So may you can help to modify this&lt;BR /&gt;
&amp;gt; code&lt;BR /&gt;
&amp;gt; &lt;BR /&gt;
&amp;gt; (datasets1=data1 dataset2=data2)&lt;BR /&gt;
&amp;gt; &lt;BR /&gt;
&amp;gt; ______________________________________________________&lt;BR /&gt;
&amp;gt; ___&lt;BR /&gt;
&amp;gt; data index;&lt;BR /&gt;
&amp;gt; keep from1 to2 ID;&lt;BR /&gt;
&amp;gt; retain from1;&lt;BR /&gt;
&amp;gt; set data1(keep= ID);&lt;BR /&gt;
&amp;gt; by ID;&lt;BR /&gt;
&amp;gt; if first.ID then from1=_N_;&lt;BR /&gt;
&amp;gt; if last.ID then do;&lt;BR /&gt;
&amp;gt; to2=_N_;&lt;BR /&gt;
&amp;gt; output;&lt;BR /&gt;
&amp;gt; end;&lt;BR /&gt;
&amp;gt; run;&lt;BR /&gt;
&amp;gt; &lt;BR /&gt;
&amp;gt; data finalmatch; &lt;BR /&gt;
&amp;gt; merge data2 (IN=in_lhs)&lt;BR /&gt;
&amp;gt; index (IN=in_ndx);&lt;BR /&gt;
&amp;gt; by ID;&lt;BR /&gt;
&amp;gt; if in_lhs and in_ndx;&lt;BR /&gt;
&amp;gt; Do from_to=from1 to to2;&lt;BR /&gt;
&amp;gt; set data1 point=from_to;&lt;BR /&gt;
&amp;gt; output;&lt;BR /&gt;
&amp;gt; end;&lt;BR /&gt;
&amp;gt; run;&lt;BR /&gt;
&amp;gt; ___________________________&lt;BR /&gt;
&amp;gt; &lt;BR /&gt;
&amp;gt; &lt;BR /&gt;
&amp;gt; &lt;BR /&gt;
&amp;gt; Many thanks</description>
    <pubDate>Tue, 19 Apr 2011 15:22:16 GMT</pubDate>
    <dc:creator>art297</dc:creator>
    <dc:date>2011-04-19T15:22:16Z</dc:date>
    <item>
      <title>Match with Multiple Occurence of ID</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Match-with-Multiple-Occurence-of-ID/m-p/53165#M11247</link>
      <description>Hi,&lt;BR /&gt;
&lt;BR /&gt;
I want to match  two datasets.&lt;BR /&gt;
&lt;BR /&gt;
datasets 1 &lt;BR /&gt;
ID ID2 &lt;BR /&gt;
X C25 &lt;BR /&gt;
X C23&lt;BR /&gt;
G C24&lt;BR /&gt;
G C5&lt;BR /&gt;
D C8&lt;BR /&gt;
D C9&lt;BR /&gt;
&lt;BR /&gt;
data dataset&lt;BR /&gt;
ID price date&lt;BR /&gt;
X 0.40 01/01/2000 &lt;BR /&gt;
X 0.41 01/02/2000&lt;BR /&gt;
X 0.42 01/03/2000&lt;BR /&gt;
X 0.40 01/04/2000&lt;BR /&gt;
X 0.44 01/05/2000&lt;BR /&gt;
X 0.39 01/06/2000&lt;BR /&gt;
X 0.20 01/01/2000&lt;BR /&gt;
D 0.21 01/02/2000&lt;BR /&gt;
D 0.20 01/03/2000&lt;BR /&gt;
D 0.23 01/04/2000&lt;BR /&gt;
D 0.24 01/05/2000&lt;BR /&gt;
D 0.26 01/06/2000&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
Where the out pushould  look like this &lt;BR /&gt;
&lt;BR /&gt;
ID2 ID price date&lt;BR /&gt;
C25 X 0.40 01/01/2000 &lt;BR /&gt;
C25 X 0.41 01/02/2000&lt;BR /&gt;
C25 X 0.42 01/03/2000&lt;BR /&gt;
C25 X 0.40 01/04/2000&lt;BR /&gt;
C25 X 0.44 01/05/2000&lt;BR /&gt;
C25 X 0.39 01/06/2000&lt;BR /&gt;
C23 X 0.40 01/01/2000 &lt;BR /&gt;
C23 X 0.41 01/02/2000&lt;BR /&gt;
C23 X 0.42 01/03/2000&lt;BR /&gt;
C23 X 0.40 01/04/2000&lt;BR /&gt;
C23 X 0.44 01/05/2000&lt;BR /&gt;
C23 X 0.39 01/06/2000&lt;BR /&gt;
C9 D 0.21 01/01/2000&lt;BR /&gt;
C8 D 0.21 01/02/2000&lt;BR /&gt;
C8 D 0.20 01/03/2000&lt;BR /&gt;
C8 D 0.23 01/04/2000&lt;BR /&gt;
C8 D 0.24 01/05/2000&lt;BR /&gt;
C8 D 0.26 01/06/2000&lt;BR /&gt;
C9 D 0.21 01/01/2000&lt;BR /&gt;
C9 D 0.21 01/02/2000&lt;BR /&gt;
C9 D 0.20 01/03/2000&lt;BR /&gt;
C9 D 0.23 01/04/2000&lt;BR /&gt;
C9 D 0.24 01/05/2000&lt;BR /&gt;
C9 D 0.26 01/06/2000&lt;BR /&gt;
&lt;BR /&gt;
The main thing is to ensure the new datsets includes the ID2 with all the stock price.&lt;BR /&gt;
&lt;BR /&gt;
In other words, it is match many-to-many, where I have multiple occurence for ID in both the datasets.&lt;BR /&gt;
&lt;BR /&gt;
I am working on this code right now, but I could not egt what I want. So may you can help to modify this code&lt;BR /&gt;
&lt;BR /&gt;
(datasets1=data1 dataset2=data2)&lt;BR /&gt;
&lt;BR /&gt;
_________________________________________________________&lt;BR /&gt;
data index;&lt;BR /&gt;
keep from1 to2 ID;&lt;BR /&gt;
retain from1;&lt;BR /&gt;
set data1(keep= ID);&lt;BR /&gt;
by ID;&lt;BR /&gt;
if first.ID then from1=_N_;&lt;BR /&gt;
if last.ID then do;&lt;BR /&gt;
to2=_N_;&lt;BR /&gt;
output;&lt;BR /&gt;
end;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
data finalmatch; &lt;BR /&gt;
merge data2 (IN=in_lhs)&lt;BR /&gt;
index (IN=in_ndx);&lt;BR /&gt;
by ID;&lt;BR /&gt;
if in_lhs and in_ndx;&lt;BR /&gt;
Do from_to=from1 to to2;&lt;BR /&gt;
set data1 point=from_to;&lt;BR /&gt;
output;&lt;BR /&gt;
end;&lt;BR /&gt;
run;&lt;BR /&gt;
___________________________&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
Many thanks</description>
      <pubDate>Tue, 19 Apr 2011 13:59:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Match-with-Multiple-Occurence-of-ID/m-p/53165#M11247</guid>
      <dc:creator>q1234</dc:creator>
      <dc:date>2011-04-19T13:59:08Z</dc:date>
    </item>
    <item>
      <title>Re: Match with Multiple Occurence of ID</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Match-with-Multiple-Occurence-of-ID/m-p/53166#M11248</link>
      <description>I have always found proc sql the best and easiest way of accomplish many-to-many match merges.&lt;BR /&gt;
&lt;BR /&gt;
However, I don't understand your desired output, either in terms of what is included or how it should be sorted.&lt;BR /&gt;
&lt;BR /&gt;
Does the following do what you are trying to accomplish?:&lt;BR /&gt;
&lt;BR /&gt;
proc sql noprint;&lt;BR /&gt;
  create table want as&lt;BR /&gt;
    select a.ID2, b.*&lt;BR /&gt;
      from datasets1 a, dataset2 b&lt;BR /&gt;
        where a.id=b.id&lt;BR /&gt;
         order by input(substr(ID2,2),best12.) desc, date&lt;BR /&gt;
  ;&lt;BR /&gt;
quit;&lt;BR /&gt;
&lt;BR /&gt;
Art&lt;BR /&gt;
-----------&lt;BR /&gt;
&amp;gt; Hi,&lt;BR /&gt;
&amp;gt; &lt;BR /&gt;
&amp;gt; I want to match  two datasets.&lt;BR /&gt;
&amp;gt; &lt;BR /&gt;
&amp;gt; datasets 1 &lt;BR /&gt;
&amp;gt; ID ID2 &lt;BR /&gt;
&amp;gt; X C25 &lt;BR /&gt;
&amp;gt; X C23&lt;BR /&gt;
&amp;gt; G C24&lt;BR /&gt;
&amp;gt; G C5&lt;BR /&gt;
&amp;gt; D C8&lt;BR /&gt;
&amp;gt; D C9&lt;BR /&gt;
&amp;gt; &lt;BR /&gt;
&amp;gt; data dataset&lt;BR /&gt;
&amp;gt; ID price date&lt;BR /&gt;
&amp;gt; X 0.40 01/01/2000 &lt;BR /&gt;
&amp;gt; X 0.41 01/02/2000&lt;BR /&gt;
&amp;gt; X 0.42 01/03/2000&lt;BR /&gt;
&amp;gt; X 0.40 01/04/2000&lt;BR /&gt;
&amp;gt; X 0.44 01/05/2000&lt;BR /&gt;
&amp;gt; X 0.39 01/06/2000&lt;BR /&gt;
&amp;gt; X 0.20 01/01/2000&lt;BR /&gt;
&amp;gt; D 0.21 01/02/2000&lt;BR /&gt;
&amp;gt; D 0.20 01/03/2000&lt;BR /&gt;
&amp;gt; D 0.23 01/04/2000&lt;BR /&gt;
&amp;gt; D 0.24 01/05/2000&lt;BR /&gt;
&amp;gt; D 0.26 01/06/2000&lt;BR /&gt;
&amp;gt; &lt;BR /&gt;
&amp;gt; &lt;BR /&gt;
&amp;gt; &lt;BR /&gt;
&amp;gt; Where the out pushould  look like this &lt;BR /&gt;
&amp;gt; &lt;BR /&gt;
&amp;gt; ID2 ID price date&lt;BR /&gt;
&amp;gt; C25 X 0.40 01/01/2000 &lt;BR /&gt;
&amp;gt; C25 X 0.41 01/02/2000&lt;BR /&gt;
&amp;gt; C25 X 0.42 01/03/2000&lt;BR /&gt;
&amp;gt; C25 X 0.40 01/04/2000&lt;BR /&gt;
&amp;gt; C25 X 0.44 01/05/2000&lt;BR /&gt;
&amp;gt; C25 X 0.39 01/06/2000&lt;BR /&gt;
&amp;gt; C23 X 0.40 01/01/2000 &lt;BR /&gt;
&amp;gt; C23 X 0.41 01/02/2000&lt;BR /&gt;
&amp;gt; C23 X 0.42 01/03/2000&lt;BR /&gt;
&amp;gt; C23 X 0.40 01/04/2000&lt;BR /&gt;
&amp;gt; C23 X 0.44 01/05/2000&lt;BR /&gt;
&amp;gt; C23 X 0.39 01/06/2000&lt;BR /&gt;
&amp;gt; C9 D 0.21 01/01/2000&lt;BR /&gt;
&amp;gt; C8 D 0.21 01/02/2000&lt;BR /&gt;
&amp;gt; C8 D 0.20 01/03/2000&lt;BR /&gt;
&amp;gt; C8 D 0.23 01/04/2000&lt;BR /&gt;
&amp;gt; C8 D 0.24 01/05/2000&lt;BR /&gt;
&amp;gt; C8 D 0.26 01/06/2000&lt;BR /&gt;
&amp;gt; C9 D 0.21 01/01/2000&lt;BR /&gt;
&amp;gt; C9 D 0.21 01/02/2000&lt;BR /&gt;
&amp;gt; C9 D 0.20 01/03/2000&lt;BR /&gt;
&amp;gt; C9 D 0.23 01/04/2000&lt;BR /&gt;
&amp;gt; C9 D 0.24 01/05/2000&lt;BR /&gt;
&amp;gt; C9 D 0.26 01/06/2000&lt;BR /&gt;
&amp;gt; &lt;BR /&gt;
&amp;gt; The main thing is to ensure the new datsets includes&lt;BR /&gt;
&amp;gt; the ID2 with all the stock price.&lt;BR /&gt;
&amp;gt; &lt;BR /&gt;
&amp;gt; In other words, it is match many-to-many, where I&lt;BR /&gt;
&amp;gt; have multiple occurence for ID in both the datasets.&lt;BR /&gt;
&amp;gt; &lt;BR /&gt;
&amp;gt; I am working on this code right now, but I could not&lt;BR /&gt;
&amp;gt; egt what I want. So may you can help to modify this&lt;BR /&gt;
&amp;gt; code&lt;BR /&gt;
&amp;gt; &lt;BR /&gt;
&amp;gt; (datasets1=data1 dataset2=data2)&lt;BR /&gt;
&amp;gt; &lt;BR /&gt;
&amp;gt; ______________________________________________________&lt;BR /&gt;
&amp;gt; ___&lt;BR /&gt;
&amp;gt; data index;&lt;BR /&gt;
&amp;gt; keep from1 to2 ID;&lt;BR /&gt;
&amp;gt; retain from1;&lt;BR /&gt;
&amp;gt; set data1(keep= ID);&lt;BR /&gt;
&amp;gt; by ID;&lt;BR /&gt;
&amp;gt; if first.ID then from1=_N_;&lt;BR /&gt;
&amp;gt; if last.ID then do;&lt;BR /&gt;
&amp;gt; to2=_N_;&lt;BR /&gt;
&amp;gt; output;&lt;BR /&gt;
&amp;gt; end;&lt;BR /&gt;
&amp;gt; run;&lt;BR /&gt;
&amp;gt; &lt;BR /&gt;
&amp;gt; data finalmatch; &lt;BR /&gt;
&amp;gt; merge data2 (IN=in_lhs)&lt;BR /&gt;
&amp;gt; index (IN=in_ndx);&lt;BR /&gt;
&amp;gt; by ID;&lt;BR /&gt;
&amp;gt; if in_lhs and in_ndx;&lt;BR /&gt;
&amp;gt; Do from_to=from1 to to2;&lt;BR /&gt;
&amp;gt; set data1 point=from_to;&lt;BR /&gt;
&amp;gt; output;&lt;BR /&gt;
&amp;gt; end;&lt;BR /&gt;
&amp;gt; run;&lt;BR /&gt;
&amp;gt; ___________________________&lt;BR /&gt;
&amp;gt; &lt;BR /&gt;
&amp;gt; &lt;BR /&gt;
&amp;gt; &lt;BR /&gt;
&amp;gt; Many thanks</description>
      <pubDate>Tue, 19 Apr 2011 15:22:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Match-with-Multiple-Occurence-of-ID/m-p/53166#M11248</guid>
      <dc:creator>art297</dc:creator>
      <dc:date>2011-04-19T15:22:16Z</dc:date>
    </item>
    <item>
      <title>Re: Match with Multiple Occurence of ID</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Match-with-Multiple-Occurence-of-ID/m-p/53167#M11249</link>
      <description>Hi,&lt;BR /&gt;
&lt;BR /&gt;
I have changed the order of the datasets in my previous code and it works perfectly.&lt;BR /&gt;
&lt;BR /&gt;
Many thanks for your help</description>
      <pubDate>Tue, 19 Apr 2011 16:28:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Match-with-Multiple-Occurence-of-ID/m-p/53167#M11249</guid>
      <dc:creator>q1234</dc:creator>
      <dc:date>2011-04-19T16:28:55Z</dc:date>
    </item>
  </channel>
</rss>

