<?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 many to many in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/Match-many-to-many/m-p/53132#M14655</link>
    <description>Hi,&lt;BR /&gt;
&lt;BR /&gt;
Many thanks for your replay. It seems that I have not explained the problem very well.&lt;BR /&gt;
this is the current format of my 2 datasets for monthly stock price&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;
I  want the out put looks 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

Message was edited by: q1234</description>
    <pubDate>Tue, 19 Apr 2011 11:55:08 GMT</pubDate>
    <dc:creator>q1234</dc:creator>
    <dc:date>2011-04-19T11:55:08Z</dc:date>
    <item>
      <title>Match many to many</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Match-many-to-many/m-p/53129#M14652</link>
      <description>Hi, &lt;BR /&gt;
Please any one can help me to do the following match?&lt;BR /&gt;
I have two data sets 1 and 2 where I have have the same ID &lt;BR /&gt;
&lt;BR /&gt;
dataset1&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;
dataset2&lt;BR /&gt;
ID      price1        price 2        price3&lt;BR /&gt;
X          .5              .9              1.4&lt;BR /&gt;
G         .6              .7              .9 &lt;BR /&gt;
D         .4              1.3            .8&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
the expected out put should like this &lt;BR /&gt;
 &lt;BR /&gt;
ID     ID2          price1       price2        price3&lt;BR /&gt;
X       C25           .5              .9              1.4&lt;BR /&gt;
X       C23           .5              .9              1.4&lt;BR /&gt;
G        C24         .6               .7               .9&lt;BR /&gt;
G        C5           .6               .7               .9&lt;BR /&gt;
D.............&lt;BR /&gt;
My current code after sorting the datasets by ID is as follows&lt;BR /&gt;
&lt;BR /&gt;
data new;&lt;BR /&gt;
merge  dataset1 dataset2;&lt;BR /&gt;
by ID;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
The problem is that each ID from dataset2 match just one ID from dataset 1.&lt;BR /&gt;
I need to modify this code in order to give the price information for the ID in datasets 1&lt;BR /&gt;
&lt;BR /&gt;
Many thanks</description>
      <pubDate>Tue, 19 Apr 2011 10:33:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Match-many-to-many/m-p/53129#M14652</guid>
      <dc:creator>q1234</dc:creator>
      <dc:date>2011-04-19T10:33:59Z</dc:date>
    </item>
    <item>
      <title>Re: Match many to many</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Match-many-to-many/m-p/53130#M14653</link>
      <description>Some sample data/code for anyone looking at this:&lt;BR /&gt;
&lt;BR /&gt;
data dataset1;&lt;BR /&gt;
format id $1.;&lt;BR /&gt;
format id2 $3.;&lt;BR /&gt;
infile datalines dsd delimiter=' ';&lt;BR /&gt;
input id $ id2 $;&lt;BR /&gt;
datalines;&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;
run;&lt;BR /&gt;
&lt;BR /&gt;
data dataset2;&lt;BR /&gt;
format id $1.;&lt;BR /&gt;
format price1 5.2;&lt;BR /&gt;
format price2 5.2;&lt;BR /&gt;
format price3 5.2;&lt;BR /&gt;
infile datalines dsd delimiter=' ';&lt;BR /&gt;
input id $ price1 price2 price3;&lt;BR /&gt;
datalines;&lt;BR /&gt;
X .5 .9 1.4&lt;BR /&gt;
G .6 .7 .9&lt;BR /&gt;
D .4 1.3 .8&lt;BR /&gt;
;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
proc sort data=dataset1;&lt;BR /&gt;
by id;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
proc sort data=dataset2;&lt;BR /&gt;
by id;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
data dataset3;&lt;BR /&gt;
merge dataset1 dataset2;&lt;BR /&gt;
by id;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
Dataset 3 looks like:&lt;BR /&gt;
&lt;BR /&gt;
D	C8	0.40	1.30	0.80&lt;BR /&gt;
D	C9	0.40	1.30	0.80&lt;BR /&gt;
G	C24	0.60	0.70	0.90&lt;BR /&gt;
G	C5	0.60	0.70	0.90&lt;BR /&gt;
X	C25	0.50	0.90	1.40&lt;BR /&gt;
X	C23	0.50	0.90	1.40&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
In response to your question - I'm not sure what the problem is?  The above code, taken from the description of what you've done, seems to do exactly what you need and the output matches your requirement.&lt;BR /&gt;
&lt;BR /&gt;
The only condition would have to be that dataset2 must be unique by ID, which is the case at least for your sample data.</description>
      <pubDate>Tue, 19 Apr 2011 11:24:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Match-many-to-many/m-p/53130#M14653</guid>
      <dc:creator>DF</dc:creator>
      <dc:date>2011-04-19T11:24:06Z</dc:date>
    </item>
    <item>
      <title>Re: Match many to many</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Match-many-to-many/m-p/53131#M14654</link>
      <description>Hi ,&lt;BR /&gt;
You can try the following if you can use the PROC SQL for your solution.&lt;BR /&gt;
&lt;BR /&gt;
data ds1;&lt;BR /&gt;
	input ID $ ID2 $;&lt;BR /&gt;
	cards;&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 ds2;&lt;BR /&gt;
	input ID $ price1 price2 price3;&lt;BR /&gt;
	cards;&lt;BR /&gt;
X .5 .9 1.4&lt;BR /&gt;
G .6 .7 .9 &lt;BR /&gt;
D .4 1.3 .8&lt;BR /&gt;
;&lt;BR /&gt;
proc sql;&lt;BR /&gt;
	create table ds3 as&lt;BR /&gt;
		select * from ds1,ds2 where ds1.id = ds2.id;&lt;BR /&gt;
quit;&lt;BR /&gt;
&lt;BR /&gt;
Thanks,&lt;BR /&gt;
Amar Mundankar.</description>
      <pubDate>Tue, 19 Apr 2011 11:48:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Match-many-to-many/m-p/53131#M14654</guid>
      <dc:creator>AmarMundankar</dc:creator>
      <dc:date>2011-04-19T11:48:36Z</dc:date>
    </item>
    <item>
      <title>Re: Match many to many</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Match-many-to-many/m-p/53132#M14655</link>
      <description>Hi,&lt;BR /&gt;
&lt;BR /&gt;
Many thanks for your replay. It seems that I have not explained the problem very well.&lt;BR /&gt;
this is the current format of my 2 datasets for monthly stock price&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;
I  want the out put looks 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

Message was edited by: q1234</description>
      <pubDate>Tue, 19 Apr 2011 11:55:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Match-many-to-many/m-p/53132#M14655</guid>
      <dc:creator>q1234</dc:creator>
      <dc:date>2011-04-19T11:55:08Z</dc:date>
    </item>
  </channel>
</rss>

