<?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: IN dataset option in MERGE two datasets. in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/IN-dataset-option-in-MERGE-two-datasets/m-p/396333#M95675</link>
    <description>&lt;P&gt;If I recall right, the&amp;nbsp;values from the&amp;nbsp;first data set in the merge statement overrides&amp;nbsp;any other values.&lt;/P&gt;
&lt;P&gt;So by ordering the merge statement will probably be sufficient. I don't think that the IN= applies&amp;nbsp;to the described logic.&lt;/P&gt;
&lt;P&gt;&lt;A href="http://support.sas.com/documentation/cdl/en/lrcon/69852/HTML/default/viewer.htm#n1tgk0uanvisvon1r26lc036k0w7.htm" target="_blank"&gt;http://support.sas.com/documentation/cdl/en/lrcon/69852/HTML/default/viewer.htm#n1tgk0uanvisvon1r26lc036k0w7.htm&lt;/A&gt;&lt;/P&gt;</description>
    <pubDate>Fri, 15 Sep 2017 13:52:13 GMT</pubDate>
    <dc:creator>LinusH</dc:creator>
    <dc:date>2017-09-15T13:52:13Z</dc:date>
    <item>
      <title>IN dataset option in MERGE two datasets.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/IN-dataset-option-in-MERGE-two-datasets/m-p/396300#M95664</link>
      <description>&lt;P&gt;I am getting confused with IN= dataset option in MERGE statement with two datasets.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data _NULL_;
    merge txnsfile(IN=txns) dupsfile(IN=dups);
    by key;&lt;BR /&gt;    flle extnrpt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Follwoing is what I want to achieve.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;1. If the key is there in both the files then&amp;nbsp;I want the record from txns file.&lt;BR /&gt;2. If the key is not thre in txns file but is there in dups file then I want the record from dups file.&lt;BR /&gt;3. If the key is&amp;nbsp;there in txns file but not there in dups file then I want it from the txns file.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thansk for your time.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Neal.&lt;/P&gt;</description>
      <pubDate>Fri, 15 Sep 2017 12:46:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/IN-dataset-option-in-MERGE-two-datasets/m-p/396300#M95664</guid>
      <dc:creator>saslovethemost</dc:creator>
      <dc:date>2017-09-15T12:46:49Z</dc:date>
    </item>
    <item>
      <title>Re: IN dataset option in MERGE two datasets.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/IN-dataset-option-in-MERGE-two-datasets/m-p/396307#M95668</link>
      <description>&lt;P&gt;Merge is typically used to merge two datasets "horizontally", i.e. adding variables.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;From your description, it sounds like you may have the same variables in both datasets, is that right?&amp;nbsp; And your goal is to find the KEY values that are in dupsfile but not in txnsfile, and add those records to txnsfile ("vertically"), is that right?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It would be easier to help you if you made added code to make a small sample (~5 records) of txnsfile and dupsfile, and show the output you want.&lt;/P&gt;</description>
      <pubDate>Fri, 15 Sep 2017 13:09:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/IN-dataset-option-in-MERGE-two-datasets/m-p/396307#M95668</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2017-09-15T13:09:03Z</dc:date>
    </item>
    <item>
      <title>Re: IN dataset option in MERGE two datasets.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/IN-dataset-option-in-MERGE-two-datasets/m-p/396333#M95675</link>
      <description>&lt;P&gt;If I recall right, the&amp;nbsp;values from the&amp;nbsp;first data set in the merge statement overrides&amp;nbsp;any other values.&lt;/P&gt;
&lt;P&gt;So by ordering the merge statement will probably be sufficient. I don't think that the IN= applies&amp;nbsp;to the described logic.&lt;/P&gt;
&lt;P&gt;&lt;A href="http://support.sas.com/documentation/cdl/en/lrcon/69852/HTML/default/viewer.htm#n1tgk0uanvisvon1r26lc036k0w7.htm" target="_blank"&gt;http://support.sas.com/documentation/cdl/en/lrcon/69852/HTML/default/viewer.htm#n1tgk0uanvisvon1r26lc036k0w7.htm&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 15 Sep 2017 13:52:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/IN-dataset-option-in-MERGE-two-datasets/m-p/396333#M95675</guid>
      <dc:creator>LinusH</dc:creator>
      <dc:date>2017-09-15T13:52:13Z</dc:date>
    </item>
    <item>
      <title>Re: IN dataset option in MERGE two datasets.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/IN-dataset-option-in-MERGE-two-datasets/m-p/396344#M95682</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13674"&gt;@LinusH&lt;/a&gt;&amp;nbsp;They way SAS handles variable collisions in a merge are tricky.&amp;nbsp; I think it's better to think of them as collisions to be avoided, rather than a tool to be used.&amp;nbsp; In a one-to-one merge with collisions, the variables from the value from the second dataset will overwrite the value for the first dataset.&amp;nbsp; But in a many(left)-to-one(right) merge with collisions, only the first record for a BY group from the many dataset will have its value overwritten, because when the second record is read it will overwrite the value in the PDV from the right dataset.&amp;nbsp; That's an ugly sentence.&amp;nbsp; Also you can have issues caused by colliding variables having different attributes.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I tend to turn on msglevel=i and treat collision notes as errors. I wish there was an option to make this an error, like MERGENOBY.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here's an example of the many-to-one collision problem which is a surprise to many folks starting to work with the MERGE statement:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data many;
  input id x;
  cards;
1 0
2 0
2 0
3 0
;
run;

data one;
  input id x;
  cards;
2 1
3 1
;
run;

*use a merge to attempt to update the values of x for id=2 and id=3;
*does not produce the desired result;

options msglevel=i;
data want;
  merge many one;
  by id;

  put (id x)(=);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Returns:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;118  options msglevel=i;
119  data want;
120    merge many one;
121    by id;
122
123    put (id x)(=);
124  run;

INFO: The variable x on data set WORK.MANY will be overwritten by data set WORK.ONE.
id=1 x=0
id=2 x=1
id=2 x=0
id=3 x=1
NOTE: There were 4 observations read from the data set WORK.MANY.
NOTE: There were 2 observations read from the data set WORK.ONE.
NOTE: The data set WORK.WANT has 4 observations and 2 variables.
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Note that the INFO: line in the log might actually lead you to think it's workig like you want "Yayy, the value was over-written!" but on the second record for id=2 the value of x was not overwritten.&lt;/P&gt;</description>
      <pubDate>Fri, 15 Sep 2017 14:17:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/IN-dataset-option-in-MERGE-two-datasets/m-p/396344#M95682</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2017-09-15T14:17:36Z</dc:date>
    </item>
    <item>
      <title>Re: IN dataset option in MERGE two datasets.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/IN-dataset-option-in-MERGE-two-datasets/m-p/396376#M95691</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data many;
  input id x;
cards; &lt;BR /&gt;1 0 &lt;BR /&gt;2 0 &lt;BR /&gt;4 0;
run;

data one;
  input id x;
cards; &lt;BR /&gt;2 1 &lt;BR /&gt;3 1 &lt;BR /&gt;4 1;
run;&lt;BR /&gt;&lt;BR /&gt;I want the ouptut like below.&lt;BR /&gt;&lt;BR /&gt;id x&lt;BR /&gt;1  0&lt;BR /&gt;2  0&lt;BR /&gt;3  1&lt;BR /&gt;4  0;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;
&lt;/PRE&gt;</description>
      <pubDate>Fri, 15 Sep 2017 15:01:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/IN-dataset-option-in-MERGE-two-datasets/m-p/396376#M95691</guid>
      <dc:creator>saslovethemost</dc:creator>
      <dc:date>2017-09-15T15:01:00Z</dc:date>
    </item>
    <item>
      <title>Re: IN dataset option in MERGE two datasets.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/IN-dataset-option-in-MERGE-two-datasets/m-p/396383#M95694</link>
      <description>&lt;P&gt;I'd look into the UPDATE statement rather than merge, e.g.:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data trans;
  input id x;
cards; 
1 0
2 0
4 0
;
run;

data master;
  input id x;
cards; 
2 1
3 1
4 1
;
run;

data want;
  update master trans;
  by id;

  put (id x)(=);
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Returns:&lt;/P&gt;
&lt;PRE&gt;165  data want;
166    update master trans;
167    by id;
168
169    put (id x)(=);
170  run;

id=1 x=0
id=2 x=0
id=3 x=1
id=4 x=0
NOTE: There were 3 observations read from the data set WORK.MASTER.
NOTE: There were 3 observations read from the data set WORK.TRANS.
NOTE: The data set WORK.WANT has 4 observations and 2 variables.
&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 15 Sep 2017 15:13:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/IN-dataset-option-in-MERGE-two-datasets/m-p/396383#M95694</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2017-09-15T15:13:47Z</dc:date>
    </item>
    <item>
      <title>Re: IN dataset option in MERGE two datasets.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/IN-dataset-option-in-MERGE-two-datasets/m-p/396398#M95696</link>
      <description>&lt;P&gt;I second &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/19879"&gt;@Quentin&lt;/a&gt;, 'Update' is the way to go. As an&amp;nbsp;unorthodox alternative, you could also 'set' interlace to have the job done:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data trans;
  input id x;
cards; 
1 0
2 0
4 0
;
run;

data master;
  input id x;
cards; 
2 1
3 1
4 1
;
run;


DATA WANT;
SET TRANS MASTER;
BY ID;
IF FIRST.ID;
RUN;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 15 Sep 2017 15:36:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/IN-dataset-option-in-MERGE-two-datasets/m-p/396398#M95696</guid>
      <dc:creator>Haikuo</dc:creator>
      <dc:date>2017-09-15T15:36:38Z</dc:date>
    </item>
    <item>
      <title>Re: IN dataset option in MERGE two datasets.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/IN-dataset-option-in-MERGE-two-datasets/m-p/396399#M95697</link>
      <description>&lt;P&gt;Looks good, can we achive the same using MERGE statements, thanks in advance.&lt;/P&gt;</description>
      <pubDate>Fri, 15 Sep 2017 15:37:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/IN-dataset-option-in-MERGE-two-datasets/m-p/396399#M95697</guid>
      <dc:creator>saslovethemost</dc:creator>
      <dc:date>2017-09-15T15:37:21Z</dc:date>
    </item>
    <item>
      <title>Re: IN dataset option in MERGE two datasets.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/IN-dataset-option-in-MERGE-two-datasets/m-p/396401#M95698</link>
      <description>&lt;P&gt;That is not a many to one merge as each ID only occurs once per source dataset. &amp;nbsp;Also it does not look like you want to do a merge at all.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This code wil interleave the rows by ID and select the first row for each ID.&lt;/P&gt;
&lt;P&gt;That will produce your desired output.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data first;
  input id x;
cards; 
1 0 
2 0 
4 0
;

data second;
  input id x;
cards; 
2 1 
3 1 
4 1
;

data want ;
  set first second ;
  by id ;
  if first.id;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 15 Sep 2017 15:38:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/IN-dataset-option-in-MERGE-two-datasets/m-p/396401#M95698</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2017-09-15T15:38:19Z</dc:date>
    </item>
    <item>
      <title>Re: IN dataset option in MERGE two datasets.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/IN-dataset-option-in-MERGE-two-datasets/m-p/396405#M95699</link>
      <description>&lt;P&gt;You could, but be aware that you will need to deal each variables one by one&amp;nbsp;if you have many variables, which is ugly coding. So why not 'update'? is this a homework question?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data trans;
  input id x;
cards; 
1 0
2 0
4 0
;
run;

data master;
  input id x;
cards; 
2 1
3 1
4 1
;
run;


DATA WANT(drop=_x);
merge TRANS (in=a) MASTER(in=b rename=x=_x);
BY ID;
IF not a and b then x=_x;
RUN;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 15 Sep 2017 15:44:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/IN-dataset-option-in-MERGE-two-datasets/m-p/396405#M95699</guid>
      <dc:creator>Haikuo</dc:creator>
      <dc:date>2017-09-15T15:44:54Z</dc:date>
    </item>
  </channel>
</rss>

