<?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: Merge - One-to-Many Problems in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Merge-One-to-Many-Problems/m-p/288985#M59652</link>
    <description>&lt;P&gt;Are your records unique in Table A and in Table B?&lt;/P&gt;
&lt;P&gt;If not, make them unique in each of the tables first BEFORE the join.&lt;/P&gt;</description>
    <pubDate>Tue, 02 Aug 2016 18:17:30 GMT</pubDate>
    <dc:creator>Reeza</dc:creator>
    <dc:date>2016-08-02T18:17:30Z</dc:date>
    <item>
      <title>Merge - One-to-Many Problems</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merge-One-to-Many-Problems/m-p/288274#M59406</link>
      <description>&lt;P&gt;I am trying to merge 2 datasets using a by (i.e. ID) variable. SAS performs a one-to-many merge, which&amp;nbsp;results in repeated identical observations. I only want unique observations in the final dataset.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For example, in dataset one, there are 5&amp;nbsp;observations for ID=23. In dataset&amp;nbsp;two,&amp;nbsp;there are 2 observations for ID=23. In the final merged dataset, there are 5 observations total for ID=23, but only 2 of them are unique.&amp;nbsp;The other 3 are duplicates of the 2nd / last observation in the second dataset.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks for the help!&lt;/P&gt;</description>
      <pubDate>Sat, 30 Jul 2016 01:10:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merge-One-to-Many-Problems/m-p/288274#M59406</guid>
      <dc:creator>_maldini_</dc:creator>
      <dc:date>2016-07-30T01:10:26Z</dc:date>
    </item>
    <item>
      <title>Re: Merge - One-to-Many Problems</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merge-One-to-Many-Problems/m-p/288276#M59407</link>
      <description>&lt;P&gt;They won't be identical duplicates. What's your filtering criteria?&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 30 Jul 2016 01:21:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merge-One-to-Many-Problems/m-p/288276#M59407</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2016-07-30T01:21:29Z</dc:date>
    </item>
    <item>
      <title>Re: Merge - One-to-Many Problems</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merge-One-to-Many-Problems/m-p/288279#M59408</link>
      <description>&lt;P&gt;Compare:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data a;
input id x$;
datalines;
23 a
23 b
23 c
23 d
23 e
;
data b;
input id y$;
datalines;
23 aa
23 bb
;

data c;
merge a b;
by id;
run;

title "Merge";
proc print; run;

data d;
set a;
set b;
by id;
run;

title "Set Set";
proc print; run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 30 Jul 2016 02:40:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merge-One-to-Many-Problems/m-p/288279#M59408</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2016-07-30T02:40:44Z</dc:date>
    </item>
    <item>
      <title>Re: Merge - One-to-Many Problems</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merge-One-to-Many-Problems/m-p/288280#M59409</link>
      <description>You can't do 1 to many merge if you don't have unique I'd in one data set. &lt;BR /&gt;The merge style you are describing is kinda dangerous leaving matching to chance. How do you know which of the two records should be match to which two records in the other data set?&lt;BR /&gt;To me it sounds like that you need to identify another variable that can expand your merging key.</description>
      <pubDate>Sat, 30 Jul 2016 02:45:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merge-One-to-Many-Problems/m-p/288280#M59409</guid>
      <dc:creator>LinusH</dc:creator>
      <dc:date>2016-07-30T02:45:58Z</dc:date>
    </item>
    <item>
      <title>Re: Merge - One-to-Many Problems</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merge-One-to-Many-Problems/m-p/288291#M59413</link>
      <description>&lt;PRE&gt;
You need a special MERGE .



data a;
input id x$;
datalines;
23 a
23 b
23 c
23 d
23 e
;
data b;
input id y$;
datalines;
23 aa
23 bb
;

data c;
ina=0;inb=0;
merge a(in=ina) b(in=inb);
by id;
if ina and inb;
run;


&lt;/PRE&gt;</description>
      <pubDate>Sat, 30 Jul 2016 05:01:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merge-One-to-Many-Problems/m-p/288291#M59413</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2016-07-30T05:01:03Z</dc:date>
    </item>
    <item>
      <title>Re: Merge - One-to-Many Problems</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merge-One-to-Many-Problems/m-p/288337#M59439</link>
      <description>Hi:&lt;BR /&gt;  But even with the "special" code, where the in variables are set to 0, this is a NOTE that should be dealt with the correct way:&lt;BR /&gt;NOTE: MERGE statement has more than one data set with repeats of BY values.&lt;BR /&gt;&lt;BR /&gt;SAS is not "happy" about duplicate BY values in the datasets being merged. The OP is not doing a one to many merge, the OP is trying to do a many-to-many merge, which might produce undesired results. As Reeza and LinusH suggested, your choices to really get a one-to-many merge will be to reduce the observations in one dataset or the other so you have a unique value (1 obs) for ID in one of the files OR to find another variable that makes the merge BY variable combination result in a one to many merge.&lt;BR /&gt;&lt;BR /&gt;cynthia</description>
      <pubDate>Sat, 30 Jul 2016 16:29:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merge-One-to-Many-Problems/m-p/288337#M59439</guid>
      <dc:creator>Cynthia_sas</dc:creator>
      <dc:date>2016-07-30T16:29:31Z</dc:date>
    </item>
    <item>
      <title>Re: Merge - One-to-Many Problems</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merge-One-to-Many-Problems/m-p/288347#M59443</link>
      <description>&lt;P&gt;What SAS is doing is that when one dataset runs out of observations for a BY group it just keeps the values of the variables contributed from that dataset. &amp;nbsp;So in your 5 to 2 match the last four observations will have the exact same values for the variables contributed by the dataset that only had 2 observations.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So what do you want to happen? &amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you just want one observation per BY group you could just add a subsetting if statement.&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want ;
   merge data2 data5 ;
   by id;
   if first.id ;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you want to prevent the values from the short stack getting carried forward then you could try setting them to missing. &amp;nbsp;You would still have 5 observations, but the last three will ahve missing values for the variables from the short dataset.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want ;
  merge data2 data5 ;
  by id;
  output;
  call missing(of _all_);
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you want every observation from one matched to every observation in the other then just an SQL full join instead. That way instead of 5 observations you will get 10.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
  create table want as 
  select * 
  from data2
  full join data5
  on data2.id = data5.id
 ;
quit;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 30 Jul 2016 19:30:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merge-One-to-Many-Problems/m-p/288347#M59443</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2016-07-30T19:30:05Z</dc:date>
    </item>
    <item>
      <title>Re: Merge - One-to-Many Problems</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merge-One-to-Many-Problems/m-p/288755#M59581</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom﻿&lt;/a&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks for your clear and detailed explanation here. Unfortunately, I am still struggling to solve this problem.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In some situations, the short stack is dataset two, in other situations, the short stack is dataset one (I should have made this clear in the original post).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Dataset one will never have MORE&amp;nbsp;than 5 observations per BY group variable, but it could have LESS&amp;nbsp;(Some participants dropped out).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Dataset two may have more, or less, than 5 observations.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I want as many observations per BY group as their are in dataset two. For example, if there are 3 observations in dataset two, then I want the variables from dataset one repeated for each of the 3 observations in dataset two. If there are 15 observations in dataset two, then I want the variables from dataset one repeated for each of the 15 observations.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Does this make sense?&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I want dataset two to determine the final number of observations in the joined dataset.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks again for your assistance.&lt;/P&gt;</description>
      <pubDate>Mon, 01 Aug 2016 23:04:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merge-One-to-Many-Problems/m-p/288755#M59581</guid>
      <dc:creator>_maldini_</dc:creator>
      <dc:date>2016-08-01T23:04:21Z</dc:date>
    </item>
    <item>
      <title>Re: Merge - One-to-Many Problems</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merge-One-to-Many-Problems/m-p/288759#M59584</link>
      <description>&lt;P&gt;Someone said it earlier. You need to add variables to your filter. If the rows are not exact duplicates which variables differentiate them? Those are the variables you use to create your additional filters.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 01 Aug 2016 23:06:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merge-One-to-Many-Problems/m-p/288759#M59584</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2016-08-01T23:06:34Z</dc:date>
    </item>
    <item>
      <title>Re: Merge - One-to-Many Problems</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merge-One-to-Many-Problems/m-p/288762#M59587</link>
      <description>&lt;P&gt;Let's call the dataset that you want to control the number of observations MASTER.&lt;/P&gt;
&lt;P&gt;You can use the IN= dataset option, but you need to reset it so that it doesn't continue to be true after the MASTER dataset runs out of observations.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want ;
   merge master (in=in1) other ;
   by id;
   if in1 then output;
   in1=0;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 01 Aug 2016 23:13:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merge-One-to-Many-Problems/m-p/288762#M59587</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2016-08-01T23:13:47Z</dc:date>
    </item>
    <item>
      <title>Re: Merge - One-to-Many Problems</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merge-One-to-Many-Problems/m-p/288959#M59644</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza﻿&lt;/a&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;lt;You need to add variables to your filter.&amp;gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I'm not clear on what you mean by "filter". Do you mean add additional variables to the BY statement?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;lt;If the rows are not exact duplicates which variables differentiate them?&amp;gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The rows are exact duplicates. In the attached image, the last 3 rows are identical. I only want one of them in the final dataset.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks for your help!&lt;/P&gt;&lt;BR /&gt;&lt;IMG src="https://communities.sas.com/t5/image/serverpage/image-id/12813i07C236E5801B9C90/image-size/large?v=1.0&amp;amp;px=600" border="0" alt="sas communities.jpg" title="sas communities.jpg" /&gt;</description>
      <pubDate>Tue, 02 Aug 2016 15:32:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merge-One-to-Many-Problems/m-p/288959#M59644</guid>
      <dc:creator>_maldini_</dc:creator>
      <dc:date>2016-08-02T15:32:08Z</dc:date>
    </item>
    <item>
      <title>Re: Merge - One-to-Many Problems</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merge-One-to-Many-Problems/m-p/288985#M59652</link>
      <description>&lt;P&gt;Are your records unique in Table A and in Table B?&lt;/P&gt;
&lt;P&gt;If not, make them unique in each of the tables first BEFORE the join.&lt;/P&gt;</description>
      <pubDate>Tue, 02 Aug 2016 18:17:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merge-One-to-Many-Problems/m-p/288985#M59652</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2016-08-02T18:17:30Z</dc:date>
    </item>
  </channel>
</rss>

