<?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: Merging data sets one having multiple data lines in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Merging-data-sets-one-having-multiple-data-lines/m-p/393713#M94834</link>
    <description>&lt;P&gt;Are there other variables with the same name in both sets? What do you want to happen with those if there are? You can only have one variable with a given name in a result.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It is usually best to provide small examples of you input data set(s) and the code you used with those sets. Then describe what is in the output that you do not want or is missing.&lt;/P&gt;
&lt;P&gt;Things as simple as the order of the data sets appearing on a merge statement in a data step make big differences in the output.&lt;/P&gt;
&lt;P&gt;See this brief example:&lt;/P&gt;
&lt;PRE&gt;data one;
   do x=1 to 3;
      do y = 4 to 6;
      output;
      end;
   end;
run;

data two;
   do x= 2 to 5;
      y= x+10;
      output;
   end;
run;

proc sort data=one;
   by x;
run;

proc sort data=two;
   by x;
run;

data mergeone;
   merge one two;
   by x;
run;

data mergetwo;
   merge two one;
   by x;
run;&lt;/PRE&gt;
&lt;P&gt;Notice that the values for y are different for the two sets.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Merge with a by&amp;nbsp;clause:&amp;nbsp;when a value does not exist in both sets means that a record without&amp;nbsp;a match is copied to the result.&lt;/P&gt;
&lt;P&gt;So you are likely getting more records than you expect. You can use the data set IN= option to test which data set contributed the current observation and discard or keep.&lt;/P&gt;
&lt;PRE&gt;data mergethree;
   merge two (in=InTWo)
         one (In=InOne);
   by x;
   if InTwo and InOne; /* keep records where only the X matches in both sets*/
run;
data mergefour;
   merge two (in=InTWo)
         one (In=InOne);
   by x;
   if InTwo ; /* keep records that come from two and the values from one when the match*/
run;&lt;/PRE&gt;</description>
    <pubDate>Wed, 06 Sep 2017 21:05:50 GMT</pubDate>
    <dc:creator>ballardw</dc:creator>
    <dc:date>2017-09-06T21:05:50Z</dc:date>
    <item>
      <title>Merging data sets one having multiple data lines</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merging-data-sets-one-having-multiple-data-lines/m-p/393423#M94727</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;I want to merge two datasets like "A" and "B" by a variable called ID. "A" has multiple data lines meaning multiple rows against same ID but "B" has single row against a ID. "A" not necessarily have all the ID as "B". When I tried merge statement, it appears with some extra rows in the output file. Like, "A" has 794 IDs and each ID has multiple data lines totalling 96246 rows. "B" has 1004 IDs and each ID has one corresponding row. When I merge, it appears with 96961 rows. How can I do it perfectly?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Cheers &amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 06 Sep 2017 04:15:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merging-data-sets-one-having-multiple-data-lines/m-p/393423#M94727</guid>
      <dc:creator>strqimr</dc:creator>
      <dc:date>2017-09-06T04:15:52Z</dc:date>
    </item>
    <item>
      <title>Re: Merging data sets one having multiple data lines</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merging-data-sets-one-having-multiple-data-lines/m-p/393425#M94728</link>
      <description>&lt;P&gt;Whars your definition of perfect?&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For joins I prefer SQL and I suspect a right or left join is what you're looking for here.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 06 Sep 2017 05:05:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merging-data-sets-one-having-multiple-data-lines/m-p/393425#M94728</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2017-09-06T05:05:38Z</dc:date>
    </item>
    <item>
      <title>Re: Merging data sets one having multiple data lines</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merging-data-sets-one-having-multiple-data-lines/m-p/393426#M94729</link>
      <description>&lt;P&gt;Thanks Reeza. Let me try.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 06 Sep 2017 05:09:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merging-data-sets-one-having-multiple-data-lines/m-p/393426#M94729</guid>
      <dc:creator>strqimr</dc:creator>
      <dc:date>2017-09-06T05:09:09Z</dc:date>
    </item>
    <item>
      <title>Re: Merging data sets one having multiple data lines</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merging-data-sets-one-having-multiple-data-lines/m-p/393432#M94734</link>
      <description>&lt;P&gt;For joins I generally prefer data steps as monitoring what goes in and out is easy.&lt;/P&gt;
&lt;P&gt;With SQL, the log won't even mention how many rows were read.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 06 Sep 2017 05:47:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merging-data-sets-one-having-multiple-data-lines/m-p/393432#M94734</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2017-09-06T05:47:13Z</dc:date>
    </item>
    <item>
      <title>Re: Merging data sets one having multiple data lines</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merging-data-sets-one-having-multiple-data-lines/m-p/393713#M94834</link>
      <description>&lt;P&gt;Are there other variables with the same name in both sets? What do you want to happen with those if there are? You can only have one variable with a given name in a result.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It is usually best to provide small examples of you input data set(s) and the code you used with those sets. Then describe what is in the output that you do not want or is missing.&lt;/P&gt;
&lt;P&gt;Things as simple as the order of the data sets appearing on a merge statement in a data step make big differences in the output.&lt;/P&gt;
&lt;P&gt;See this brief example:&lt;/P&gt;
&lt;PRE&gt;data one;
   do x=1 to 3;
      do y = 4 to 6;
      output;
      end;
   end;
run;

data two;
   do x= 2 to 5;
      y= x+10;
      output;
   end;
run;

proc sort data=one;
   by x;
run;

proc sort data=two;
   by x;
run;

data mergeone;
   merge one two;
   by x;
run;

data mergetwo;
   merge two one;
   by x;
run;&lt;/PRE&gt;
&lt;P&gt;Notice that the values for y are different for the two sets.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Merge with a by&amp;nbsp;clause:&amp;nbsp;when a value does not exist in both sets means that a record without&amp;nbsp;a match is copied to the result.&lt;/P&gt;
&lt;P&gt;So you are likely getting more records than you expect. You can use the data set IN= option to test which data set contributed the current observation and discard or keep.&lt;/P&gt;
&lt;PRE&gt;data mergethree;
   merge two (in=InTWo)
         one (In=InOne);
   by x;
   if InTwo and InOne; /* keep records where only the X matches in both sets*/
run;
data mergefour;
   merge two (in=InTWo)
         one (In=InOne);
   by x;
   if InTwo ; /* keep records that come from two and the values from one when the match*/
run;&lt;/PRE&gt;</description>
      <pubDate>Wed, 06 Sep 2017 21:05:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merging-data-sets-one-having-multiple-data-lines/m-p/393713#M94834</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2017-09-06T21:05:50Z</dc:date>
    </item>
  </channel>
</rss>

