<?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: One-to-Many merge only overwrites first record in BY group? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/One-to-Many-merge-only-overwrites-first-record-in-BY-group/m-p/351406#M273726</link>
    <description>Wow, it doesn't say Super User under your name for nothing, Tom! This is closer to what I was looking for, since it can be easily integrated with existing code that we have, but I'll have to digest on that new syntax for a bit. Thanks much!</description>
    <pubDate>Wed, 19 Apr 2017 17:52:18 GMT</pubDate>
    <dc:creator>techsassy</dc:creator>
    <dc:date>2017-04-19T17:52:18Z</dc:date>
    <item>
      <title>One-to-Many merge only overwrites first record in BY group?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/One-to-Many-merge-only-overwrites-first-record-in-BY-group/m-p/351371#M273712</link>
      <description>&lt;P&gt;Hi all,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am having some trouble understanding the behavior of a one-to-many merge situation I am working through on SAS 9.4.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Imagine that our business previously had separate sales targets for each employee within&amp;nbsp;each of our divisions. In the ACCT department, Sally had to make 1 sale per week of item1 and Steve had to make 2 sales per week of item1.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Now we want to assign new standard targets for all items we sell, for all employees in all departments. In the ACCT department, Sally and Steve will now have to make 3 sales per week of item1, their new item1_target.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In the following example1, I notice that when I merge my new_targets onto my old_targets within a department, only the first record containing an old_target gets overwritten with the new_target, instead of all old_targets. By dropping the old_targets in example2, I all employee records correctly receive the new new_target:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data old_targets;
	input department $ name $ item1_target;
	datalines;
ACCT Sally 1
ACCT Steve 2
MGMT Tommy 1
MGMT Terri 1
;
run;

data new_targets;
	input department $ item1_target;
	datalines;
ACCT 3
MGMT 3
;
run;

proc sort data=old_targets; by department; run;
proc sort data=new_targets; by department; run;

data example1;
	merge	old_targets (in=a)
			new_targets (in=b);
	by department;
	if a;
run;

proc print data=example1; title "example1"; run;

proc sort data=old_targets(drop=item1_target); by department; run;
proc sort data=new_targets; by department; run;

data example2;
	merge	old_targets (in=a)
			new_targets (in=b);
	by department;
	if a;
run;

proc print data=example2; title "example2"; run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;IMG src="https://communities.sas.com/t5/image/serverpage/image-id/8409iB291EC1EDCFF35D8/image-size/original?v=1.0&amp;amp;px=-1" border="0" alt="example1.PNG" title="example1.PNG" /&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Can someone help me understand why the merge operation is stopping after the first record for each department, in example1, and if there is any way to avoid this&amp;nbsp;&lt;STRONG&gt;without having to drop/keep/rename any variables?&amp;nbsp;&lt;/STRONG&gt;In the above examples, I have only included a few variables.&amp;nbsp;In reality, we have dozens of variables containing information about each employee and hundreds of sales targets for various items and it is quite time-consuming (and error-prone) to have to either drop every variable which we want to update/overwrite or to keep only variables that we don't want updated/overwitten.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Is there any way to achieve this kind of one-to-many merge such that it updates all records for the given BY variable, rather than just updating the first record in each BY group, without dropping/keeping/renaming variables?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you for your help!&lt;/P&gt;</description>
      <pubDate>Wed, 19 Apr 2017 16:51:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/One-to-Many-merge-only-overwrites-first-record-in-BY-group/m-p/351371#M273712</guid>
      <dc:creator>techsassy</dc:creator>
      <dc:date>2017-04-19T16:51:12Z</dc:date>
    </item>
    <item>
      <title>Re: One-to-Many merge only overwrites first record in BY group?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/One-to-Many-merge-only-overwrites-first-record-in-BY-group/m-p/351376#M273713</link>
      <description>&lt;P&gt;Because it only reads each record in both datasets once each. &amp;nbsp;So when both datasets are contributing for the frist observation in a group then one listed last in the MERGE statement "wins". &amp;nbsp;But for the second, third, etc observation in the group then only the new obsveraions from the datasets that are still contributing are read. So the values will overwrite the values read before.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;DROP or RENAME one of the variables and you can see what is happening. &amp;nbsp;If you want to keep the old value if there is no match then use COALESCE() function.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data example1;
  merge
    old_targets (in=a rename=(item1_target=old_item1_target))
    new_targets (in=b rename=(item1_target=new_item1_target))
  ;
  by department;
  if a;
  item1_target = coalesce(new_item1_target,old_item1_target);
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 19 Apr 2017 16:47:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/One-to-Many-merge-only-overwrites-first-record-in-BY-group/m-p/351376#M273713</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2017-04-19T16:47:47Z</dc:date>
    </item>
    <item>
      <title>Re: One-to-Many merge only overwrites first record in BY group?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/One-to-Many-merge-only-overwrites-first-record-in-BY-group/m-p/351379#M273714</link>
      <description>&lt;P&gt;So there is no way to force SAS to apply the ONE merge for as many observations as exist in the MANY dataset, without dropping/keeping/renaming variables?&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It's a lot of work to rename all of those hundreds of items that we have, just to make SAS perform a simple behavior of matching this one record per department to all of the obversations in the MANY dataset. Is there really no other way to do this?&lt;/P&gt;</description>
      <pubDate>Wed, 19 Apr 2017 16:49:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/One-to-Many-merge-only-overwrites-first-record-in-BY-group/m-p/351379#M273714</guid>
      <dc:creator>techsassy</dc:creator>
      <dc:date>2017-04-19T16:49:55Z</dc:date>
    </item>
    <item>
      <title>Re: One-to-Many merge only overwrites first record in BY group?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/One-to-Many-merge-only-overwrites-first-record-in-BY-group/m-p/351380#M273715</link>
      <description>&lt;P&gt;An approach I'd suggest (I'm sure somebody can code it for you):&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Read your new targets into a hash table.&amp;nbsp; Check for matches, bring in all variables from the hash table when you have a match.&lt;/P&gt;</description>
      <pubDate>Wed, 19 Apr 2017 16:52:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/One-to-Many-merge-only-overwrites-first-record-in-BY-group/m-p/351380#M273715</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2017-04-19T16:52:55Z</dc:date>
    </item>
    <item>
      <title>Re: One-to-Many merge only overwrites first record in BY group?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/One-to-Many-merge-only-overwrites-first-record-in-BY-group/m-p/351382#M273716</link>
      <description>&lt;P&gt;An easy way is have an INDEX on the transaction file.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data old_targets;
  input department $ name $ item1_target;
datalines;
ACCT Sally 1
ACCT Steve 2
MGMT Tommy 1
MGMT Terri 1
UNKN New 2
;

data new_targets ( index=(department) );
  input department $ item1_target;
datalines;
ACCT 3
MGMT 3
UNUSED 4
;

data example3 ;
  set old_targets ;
  set new_targets key=department /unique;
run;
proc print; run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 19 Apr 2017 17:00:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/One-to-Many-merge-only-overwrites-first-record-in-BY-group/m-p/351382#M273716</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2017-04-19T17:00:02Z</dc:date>
    </item>
    <item>
      <title>Re: One-to-Many merge only overwrites first record in BY group?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/One-to-Many-merge-only-overwrites-first-record-in-BY-group/m-p/351386#M273717</link>
      <description>Interesting, thanks for the thoughts! The hash table looks a little complicated, since our code is often shared &amp;amp; modified between team members of varying skill levels, but I'll see if INDEX does the trick.</description>
      <pubDate>Wed, 19 Apr 2017 17:08:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/One-to-Many-merge-only-overwrites-first-record-in-BY-group/m-p/351386#M273717</guid>
      <dc:creator>techsassy</dc:creator>
      <dc:date>2017-04-19T17:08:14Z</dc:date>
    </item>
    <item>
      <title>Re: One-to-Many merge only overwrites first record in BY group?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/One-to-Many-merge-only-overwrites-first-record-in-BY-group/m-p/351389#M273718</link>
      <description>&lt;P&gt;This can be done much more easily with PROC SQL. Here's an example:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;PROC SQL;
	SELECT new_targets.department, name, new_targets.item1_target
	FROM old_targets, new_targets
	WHERE old_targets.department = new_targets.department
	GROUP BY new_targets.department;
QUIT;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Or using aliasing to shorten the code considerably:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;PROC SQL;
	SELECT n.department, name, n.item1_target
	FROM old_targets as o, new_targets AS n
	WHERE o.department = n.department
	GROUP BY n.department;
QUIT;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You don't need to sort anything beforehand and the syntax is fairly simple. Joins like this are an area where SQL really shines above the equivalent&amp;nbsp;DATA step code.&lt;/P&gt;</description>
      <pubDate>Wed, 19 Apr 2017 17:25:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/One-to-Many-merge-only-overwrites-first-record-in-BY-group/m-p/351389#M273718</guid>
      <dc:creator>GinaRepole</dc:creator>
      <dc:date>2017-04-19T17:25:21Z</dc:date>
    </item>
    <item>
      <title>Re: One-to-Many merge only overwrites first record in BY group?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/One-to-Many-merge-only-overwrites-first-record-in-BY-group/m-p/351390#M273719</link>
      <description>Thanks, Gina, will definitely consider that! I really appreciate receiving all of these different strategies, so quickly!</description>
      <pubDate>Wed, 19 Apr 2017 17:19:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/One-to-Many-merge-only-overwrites-first-record-in-BY-group/m-p/351390#M273719</guid>
      <dc:creator>techsassy</dc:creator>
      <dc:date>2017-04-19T17:19:34Z</dc:date>
    </item>
    <item>
      <title>Re: One-to-Many merge only overwrites first record in BY group?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/One-to-Many-merge-only-overwrites-first-record-in-BY-group/m-p/351391#M273720</link>
      <description>&lt;P&gt;The trouble with that SQL solutions is that it will yield different results from the merge if &amp;nbsp;it happens that there are not "new_targets" for some departments.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also SQL &amp;nbsp;requires that you type the individual variable names and the request was to avoid having to do that.&lt;/P&gt;</description>
      <pubDate>Wed, 19 Apr 2017 17:22:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/One-to-Many-merge-only-overwrites-first-record-in-BY-group/m-p/351391#M273720</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2017-04-19T17:22:43Z</dc:date>
    </item>
    <item>
      <title>Re: One-to-Many merge only overwrites first record in BY group?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/One-to-Many-merge-only-overwrites-first-record-in-BY-group/m-p/351393#M273721</link>
      <description>&lt;P&gt;Thanks for pointing out this issue with different results, Tom, and indeed I was hoping to avoid&amp;nbsp;typing out each name. I'll have to do some experimentation with each method.&lt;/P&gt;</description>
      <pubDate>Wed, 19 Apr 2017 17:26:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/One-to-Many-merge-only-overwrites-first-record-in-BY-group/m-p/351393#M273721</guid>
      <dc:creator>techsassy</dc:creator>
      <dc:date>2017-04-19T17:26:38Z</dc:date>
    </item>
    <item>
      <title>Re: One-to-Many merge only overwrites first record in BY group?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/One-to-Many-merge-only-overwrites-first-record-in-BY-group/m-p/351394#M273722</link>
      <description>&lt;P&gt;Awesome question that made me feel I am so dumb at 11:00 PM in Chennai. I will have to read through the documentation thoroughly as I recall something like a many to one merge to happen in your case which did not. I hope Tom,&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/4954"&gt;@Astounding&lt;/a&gt;&amp;nbsp;may spare a little more time in explaining "merge" if they don't mind.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Anyway here is your hash solution if you like. I am so used to using hashes everyday, i totally forgot basics of merge:(&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;data&lt;/STRONG&gt; want;&lt;/P&gt;&lt;P&gt;if (_n_ = &lt;STRONG&gt;1&lt;/STRONG&gt;) then do;&lt;/P&gt;&lt;P&gt;if &lt;STRONG&gt;0&lt;/STRONG&gt; then do;&lt;/P&gt;&lt;P&gt;set old_targets;&lt;/P&gt;&lt;P&gt;set&amp;nbsp; new_targets;&lt;/P&gt;&lt;P&gt;end;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; declare hash myhash(dataset: "new_targets");&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; myhash.definekey('department');&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; myhash.definedata('item1_target');&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; myhash.definedone();&lt;/P&gt;&lt;P&gt;&amp;nbsp;end;&lt;/P&gt;&lt;P&gt;&amp;nbsp;set old_targets;&lt;/P&gt;&lt;P&gt;if myhash.find() ne &lt;STRONG&gt;0&lt;/STRONG&gt; then call missing(item1_target);&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;run&lt;/STRONG&gt;;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;&lt;P&gt;Naveen Srinivasan&lt;/P&gt;</description>
      <pubDate>Wed, 19 Apr 2017 17:30:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/One-to-Many-merge-only-overwrites-first-record-in-BY-group/m-p/351394#M273722</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2017-04-19T17:30:07Z</dc:date>
    </item>
    <item>
      <title>Re: One-to-Many merge only overwrites first record in BY group?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/One-to-Many-merge-only-overwrites-first-record-in-BY-group/m-p/351396#M273723</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/91713"&gt;@GinaRepole&lt;/a&gt;&amp;nbsp;Mam, I can agree to a certain extent: "&lt;SPAN&gt;Joins like this are an area where SQL really shines above the equivalent&amp;nbsp;DATA step code" but doesn't SQL still needs a cartesian and then apply whatever join we specify. I think that's an overhead&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Just my 2 cents,&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Naveen Srinivasan&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 19 Apr 2017 17:33:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/One-to-Many-merge-only-overwrites-first-record-in-BY-group/m-p/351396#M273723</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2017-04-19T17:33:11Z</dc:date>
    </item>
    <item>
      <title>Re: One-to-Many merge only overwrites first record in BY group?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/One-to-Many-merge-only-overwrites-first-record-in-BY-group/m-p/351397#M273724</link>
      <description>Thanks for the hash solution, Naveen! Will check it out.</description>
      <pubDate>Wed, 19 Apr 2017 17:37:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/One-to-Many-merge-only-overwrites-first-record-in-BY-group/m-p/351397#M273724</guid>
      <dc:creator>techsassy</dc:creator>
      <dc:date>2017-04-19T17:37:06Z</dc:date>
    </item>
    <item>
      <title>Re: One-to-Many merge only overwrites first record in BY group?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/One-to-Many-merge-only-overwrites-first-record-in-BY-group/m-p/351399#M273725</link>
      <description>&lt;P&gt;Here is a method that doesn't require creating an INDEX. Instead it keeps a counter of the number of records read from the transaction file and then uses POINT= option on SET statement to re-read the observation many times. &lt;STRIKE&gt;Note that if your transaction file introduces some NEW variables then you will need to remember to clear them out or else the last read value will be retained&lt;/STRIKE&gt;.Actually that was side effect of earlier version I tried where I had a KEEP= option on the MERGE statement.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data old_targets;
  input keyvar $ oldvar $ commonvar;
datalines;
ACCT Sally 1
ACCT Steve 2
MGMT Tommy 1
MGMT Terri 1
UNKN New 2
;

data new_targets ;
  input keyvar $ commonvar newvar $;
datalines;
ACCT 3 x
MGMT 3 y
UNUSED 4 z
;

data example4 ;
  title1 'Example4 - Uses POINT= option';
  merge old_targets(in=in_old) new_targets (in=in_new) ;
  by keyvar ;
  point + first.keyvar*in_new ;
  if in_old;
  if in_new then set new_targets point=point;
run;
proc print; 
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 19 Apr 2017 18:44:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/One-to-Many-merge-only-overwrites-first-record-in-BY-group/m-p/351399#M273725</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2017-04-19T18:44:49Z</dc:date>
    </item>
    <item>
      <title>Re: One-to-Many merge only overwrites first record in BY group?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/One-to-Many-merge-only-overwrites-first-record-in-BY-group/m-p/351406#M273726</link>
      <description>Wow, it doesn't say Super User under your name for nothing, Tom! This is closer to what I was looking for, since it can be easily integrated with existing code that we have, but I'll have to digest on that new syntax for a bit. Thanks much!</description>
      <pubDate>Wed, 19 Apr 2017 17:52:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/One-to-Many-merge-only-overwrites-first-record-in-BY-group/m-p/351406#M273726</guid>
      <dc:creator>techsassy</dc:creator>
      <dc:date>2017-04-19T17:52:18Z</dc:date>
    </item>
    <item>
      <title>Re: One-to-Many merge only overwrites first record in BY group?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/One-to-Many-merge-only-overwrites-first-record-in-BY-group/m-p/351408#M273727</link>
      <description>&lt;P&gt;Naveen, thanks for coding this.&amp;nbsp; Just a couple of notes ...&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If there are dozens of new targets instead of just 1, how would you code the definedata line?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;When finding a match, the objective is to replace the old values with the new values.&amp;nbsp; When there is no match, the old values should remain in place.&amp;nbsp; I'm not sure that call missing() does that, but there should be an easy fix.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Regarding MERGE, Tom mentioned the crucial feature in one of his posts:&amp;nbsp; each observation gets read once.&amp;nbsp; So the process for each value of the BY variable&amp;nbsp;is (a) see if there is another observation to read from the first data set (if so read it in), then (b) see if there is another observation to read from the second data set (if so, read it in).&amp;nbsp; That's what produces the original, strange-if-you-haven't-seen-it-before, result.&amp;nbsp; My recollection is that the documentation skirts around the issue (in some places), saying something like, "The value you end up with is the last value read."&lt;/P&gt;</description>
      <pubDate>Wed, 19 Apr 2017 18:00:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/One-to-Many-merge-only-overwrites-first-record-in-BY-group/m-p/351408#M273727</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2017-04-19T18:00:03Z</dc:date>
    </item>
    <item>
      <title>Re: One-to-Many merge only overwrites first record in BY group?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/One-to-Many-merge-only-overwrites-first-record-in-BY-group/m-p/351421#M273728</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/4954"&gt;@Astounding&lt;/a&gt;Thank you Sir for the notes:&lt;/P&gt;&lt;P&gt;2. --&amp;gt;&lt;SPAN&gt;When finding a match, the objective is to replace the old values with the new values.&amp;nbsp; When there is no match, the old values should remain in place.&amp;nbsp; I'm not sure that call missing() does that, but there should be an easy fix.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;Answer:&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;data&lt;/STRONG&gt; want;&lt;/P&gt;&lt;P&gt;if (_n_ = &lt;STRONG&gt;1&lt;/STRONG&gt;) then do;&lt;/P&gt;&lt;P&gt;if &lt;STRONG&gt;0&lt;/STRONG&gt; then do;&lt;/P&gt;&lt;P&gt;set old_targets;&lt;/P&gt;&lt;P&gt;set&amp;nbsp; new_targets;&lt;/P&gt;&lt;P&gt;end;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; declare hash myhash(dataset: "new_targets");&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; myhash.definekey('department');&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; myhash.definedata('item1_target');&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; myhash.definedone();&lt;/P&gt;&lt;P&gt;&amp;nbsp;end;&lt;/P&gt;&lt;P&gt;&amp;nbsp;set old_targets;&lt;/P&gt;&lt;P&gt;_item1_target=item1_target;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;STRONG&gt; /*simple reset*/&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;if myhash.find() ne &lt;STRONG&gt;0&lt;/STRONG&gt; then item1_target=_item1_target;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;STRONG&gt;&amp;nbsp; /*simple reset*/&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;drop _:;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;run&lt;/STRONG&gt;;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;1.&lt;SPAN&gt;If there are dozens of new targets instead of just 1, how would you code the definedata line?&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&lt;STRONG&gt;* &lt;/STRONG&gt;if i understand you correctly&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;answer: &amp;nbsp;&lt;/P&gt;&lt;P&gt;declare hash myhash(dataset: "new_targets");&lt;STRONG&gt;/*this argument tag accepts dataset options just like keep= or drop= inside literal*/&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; myhash.definekey('department');&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;STRONG&gt; myhash.definedata(all:'yes');&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; myhash.definedone();&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;&lt;P&gt;Naveen Srinivasan&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 19 Apr 2017 18:44:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/One-to-Many-merge-only-overwrites-first-record-in-BY-group/m-p/351421#M273728</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2017-04-19T18:44:26Z</dc:date>
    </item>
  </channel>
</rss>

