<?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: how to concatenate values of the same variables from both datasets in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Merge-how-to-concatenate-values-of-the-same-variables-from-both/m-p/797422#M287515</link>
    <description>&lt;P&gt;Thanks&amp;nbsp;mkeintz for the codes. All of them work beautifully. And Tom's way to get around for 3 datasets is brilliant and very concise. Only wish I could mark both your replies as solutions.&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Sat, 19 Feb 2022 21:27:51 GMT</pubDate>
    <dc:creator>Solph</dc:creator>
    <dc:date>2022-02-19T21:27:51Z</dc:date>
    <item>
      <title>Merge: how to concatenate values of the same variables from both datasets</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merge-how-to-concatenate-values-of-the-same-variables-from-both/m-p/797363#M287468</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have two data sets A and B with some common variables and case IDs, and I want to merge them. Most of all, for cases with the same variable names, I'd like to take values from first A; if missing then take from B. I know I can rename variables in B before merging and then use DO IF to get the job done. But I'm wondering if there is quick way without going through the hassle of renaming and DO IF (as I have need to deal with more than 15 variables on this.&lt;/P&gt;
&lt;P&gt;In SQL I could use&amp;nbsp;coalesce, such as proc sql; select coalesce(a.var1, b.var1) as var1 from A as a full join B as b on a.ID=b.ID ...&lt;/P&gt;
&lt;P&gt;But in Data sets, is there a trick to do this in a couple of simple lines?&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data a; input id 1 var1 3 var2 5 var3 7;
datalines;
1 1 2 
2 2 3 4
3 3 4 5
;
proc print; run;
data b; input id var1 var3 var4;
datalines;
1 11 12 7
2 12 13 8
4 13 14 9
;
proc print; run;

data comb; format id var1 var2 var3 var4; merge b a; by id; 
proc print; run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;TABLE border="1" width="99.99999999999999%"&gt;
&lt;TBODY&gt;
&lt;TR style="height: 14.5pt;"&gt;
&lt;TD width="12.280701754385964%" height="14px" class="xl68" style="height: 14.5pt; width: 48pt;"&gt;HAVE&lt;/TD&gt;
&lt;TD width="11.779448621553884%" height="14px" class="xl68" style="width: 48pt;"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD width="11.904761904761903%" height="14px" class="xl68" style="width: 48pt;"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD width="11.904761904761903%" height="14px" class="xl68" style="width: 48pt;"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD width="11.904761904761903%" height="14px" class="xl65" style="width: 48pt;"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD width="12.406015037593985%" height="14px" class="xl65" style="width: 48pt;"&gt;WANT&lt;/TD&gt;
&lt;TD width="12.030075187969924%" height="14px" class="xl65" style="width: 48pt;"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD width="5.263157894736842%" height="14px" class="xl65" style="width: 48pt;"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD width="5.263157894736842%" height="14px" class="xl65" style="width: 48pt;"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD width="5.263157894736842%" height="14px" class="xl65" style="width: 48pt;"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD width="12.280701754385964%" height="18px" class="xl69" style="height: 15.0pt; width: 48pt;"&gt;id&lt;/TD&gt;
&lt;TD width="11.779448621553884%" height="18px" class="xl70" style="width: 48pt;"&gt;var1&lt;/TD&gt;
&lt;TD width="11.904761904761903%" height="18px" class="xl70" style="width: 48pt;"&gt;var2&lt;/TD&gt;
&lt;TD width="11.904761904761903%" height="18px" class="xl70" style="width: 48pt;"&gt;var3&lt;/TD&gt;
&lt;TD width="11.904761904761903%" height="18px" class="xl71" style="width: 48pt;"&gt;var4&lt;/TD&gt;
&lt;TD width="12.406015037593985%" height="18px" class="xl69" style="border-left: none; width: 48pt;"&gt;id&lt;/TD&gt;
&lt;TD width="12.030075187969924%" height="18px" class="xl70" style="width: 48pt;"&gt;var1&lt;/TD&gt;
&lt;TD width="5.263157894736842%" height="18px" class="xl70" style="width: 48pt;"&gt;var2&lt;/TD&gt;
&lt;TD width="5.263157894736842%" height="18px" class="xl70" style="width: 48pt;"&gt;var3&lt;/TD&gt;
&lt;TD width="5.263157894736842%" height="18px" class="xl71" style="width: 48pt;"&gt;var4&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD width="12.280701754385964%" height="15px" class="xl72" style="height: 15.0pt;"&gt;1&lt;/TD&gt;
&lt;TD width="11.779448621553884%" height="15px" class="xl66"&gt;1&lt;/TD&gt;
&lt;TD width="11.904761904761903%" height="15px" class="xl66"&gt;2&lt;/TD&gt;
&lt;TD width="11.904761904761903%" height="15px" class="xl66"&gt;.&lt;/TD&gt;
&lt;TD width="11.904761904761903%" height="15px" class="xl73"&gt;7&lt;/TD&gt;
&lt;TD width="12.406015037593985%" height="15px" class="xl72" style="border-left: none;"&gt;1&lt;/TD&gt;
&lt;TD width="12.030075187969924%" height="15px" class="xl66"&gt;1&lt;/TD&gt;
&lt;TD width="5.263157894736842%" height="15px" class="xl66"&gt;2&lt;/TD&gt;
&lt;TD width="5.263157894736842%" height="15px" class="xl67"&gt;12&lt;/TD&gt;
&lt;TD width="5.263157894736842%" height="15px" class="xl73"&gt;7&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD width="12.280701754385964%" height="15px" class="xl72" style="height: 15.0pt;"&gt;2&lt;/TD&gt;
&lt;TD width="11.779448621553884%" height="15px" class="xl66"&gt;.&lt;/TD&gt;
&lt;TD width="11.904761904761903%" height="15px" class="xl66"&gt;3&lt;/TD&gt;
&lt;TD width="11.904761904761903%" height="15px" class="xl66"&gt;4&lt;/TD&gt;
&lt;TD width="11.904761904761903%" height="15px" class="xl73"&gt;8&lt;/TD&gt;
&lt;TD width="12.406015037593985%" height="15px" class="xl72" style="border-left: none;"&gt;2&lt;/TD&gt;
&lt;TD width="12.030075187969924%" height="15px" class="xl67"&gt;40&lt;/TD&gt;
&lt;TD width="5.263157894736842%" height="15px" class="xl66"&gt;3&lt;/TD&gt;
&lt;TD width="5.263157894736842%" height="15px" class="xl66"&gt;4&lt;/TD&gt;
&lt;TD width="5.263157894736842%" height="15px" class="xl73"&gt;8&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD width="12.280701754385964%" height="15px" class="xl72" style="height: 15.0pt;"&gt;3&lt;/TD&gt;
&lt;TD width="11.779448621553884%" height="15px" class="xl66"&gt;3&lt;/TD&gt;
&lt;TD width="11.904761904761903%" height="15px" class="xl66"&gt;.&lt;/TD&gt;
&lt;TD width="11.904761904761903%" height="15px" class="xl66"&gt;5&lt;/TD&gt;
&lt;TD width="11.904761904761903%" height="15px" class="xl73"&gt;.&lt;/TD&gt;
&lt;TD width="12.406015037593985%" height="15px" class="xl72" style="border-left: none;"&gt;3&lt;/TD&gt;
&lt;TD width="12.030075187969924%" height="15px" class="xl66"&gt;3&lt;/TD&gt;
&lt;TD width="5.263157894736842%" height="15px" class="xl66"&gt;.&lt;/TD&gt;
&lt;TD width="5.263157894736842%" height="15px" class="xl66"&gt;5&lt;/TD&gt;
&lt;TD width="5.263157894736842%" height="15px" class="xl73"&gt;.&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 14.5pt;"&gt;
&lt;TD width="12.280701754385964%" height="14px" class="xl74" style="height: 14.5pt;"&gt;4&lt;/TD&gt;
&lt;TD width="11.779448621553884%" height="14px" class="xl75"&gt;13&lt;/TD&gt;
&lt;TD width="11.904761904761903%" height="14px" class="xl75"&gt;.&lt;/TD&gt;
&lt;TD width="11.904761904761903%" height="14px" class="xl75"&gt;14&lt;/TD&gt;
&lt;TD width="11.904761904761903%" height="14px" class="xl76"&gt;9&lt;/TD&gt;
&lt;TD width="12.406015037593985%" height="14px" class="xl74" style="border-left: none;"&gt;4&lt;/TD&gt;
&lt;TD width="12.030075187969924%" height="14px" class="xl75"&gt;13&lt;/TD&gt;
&lt;TD width="5.263157894736842%" height="14px" class="xl75"&gt;.&lt;/TD&gt;
&lt;TD width="5.263157894736842%" height="14px" class="xl75"&gt;14&lt;/TD&gt;
&lt;TD width="5.263157894736842%" height="14px" class="xl76"&gt;9&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 19 Feb 2022 06:10:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merge-how-to-concatenate-values-of-the-same-variables-from-both/m-p/797363#M287468</guid>
      <dc:creator>Solph</dc:creator>
      <dc:date>2022-02-19T06:10:48Z</dc:date>
    </item>
    <item>
      <title>Re: Merge: how to concatenate values of the same variables from both datasets</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merge-how-to-concatenate-values-of-the-same-variables-from-both/m-p/797365#M287470</link>
      <description>&lt;P&gt;Why&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;,,, does the result for ID 2, Var1 have a 40?&amp;nbsp; Shouldn't is be a 2?&lt;/LI&gt;
&lt;LI&gt;... does the result for ID 3, Var2 have a missing value.&amp;nbsp; Shouldn't it be a 4?&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If my suggested values are actuallly correct, then:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  update b a;
  by id;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The UPDATE statement is a lot like merge.&amp;nbsp; When the corresponding value in the second dataset (A) is not missing, it will replace the value in B.&amp;nbsp; But if the value in A is missing then the original value in B is kept.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Another way to do this is:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set a b;
  by id;
  array vars var1-var4;
  do over vars;
    vars=ifn(first.id=0,coalesce(lag(vars),vars),vars);
  end;
  if last.id;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;But the UPDATE statement, can't be extended to more-than-two datasets (say you have datasets A, B, and C, and you want the equivalent of coalesce(a.var,b.var,c.var)).&amp;nbsp; That could be done via:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set  a b c;
  by id;
  array vars {4} var1-var4;
  array tmp {4} _temporary_ ;
  if first.id then call missing(of tmp{*});
  do i=1 to 4;
    tmp{i}=coalesce(tmp{i},vars{i});
  end;
  if last.id;
  do i=1 to 4;
    vars{i}=tmp{i};
  end;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;But actually the update statement can be used with more-than-two datasets, per&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;&amp;nbsp;'s reply to my post in this thread.&amp;nbsp; True, it's an extra data step, but because the extra step is a data set VIEW (not a data set FILE), it does not require extra disk usage.&amp;nbsp; Very nice.&lt;/P&gt;</description>
      <pubDate>Sat, 19 Feb 2022 22:16:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merge-how-to-concatenate-values-of-the-same-variables-from-both/m-p/797365#M287470</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2022-02-19T22:16:50Z</dc:date>
    </item>
    <item>
      <title>Re: Merge: how to concatenate values of the same variables from both datasets</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merge-how-to-concatenate-values-of-the-same-variables-from-both/m-p/797402#M287500</link>
      <description>&lt;P&gt;You are misusing "concatenate". That means to "add on to" or such. If I have a value of 'a' and concatenate with a value of 'b' the result is 'ab' (or if done in the other order) 'ba'. It does not replace values.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;COALESCE works in a data step but you would need to rename at least one of the variables.&lt;/P&gt;
&lt;P&gt;Your "example" have does not indicate 1) which data set is "have" (does not match either A or B), 2) and should show the two "have" data sets each clearly and then the "want".&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>Sat, 19 Feb 2022 18:14:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merge-how-to-concatenate-values-of-the-same-variables-from-both/m-p/797402#M287500</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2022-02-19T18:14:46Z</dc:date>
    </item>
    <item>
      <title>Re: Merge: how to concatenate values of the same variables from both datasets</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merge-how-to-concatenate-values-of-the-same-variables-from-both/m-p/797405#M287503</link>
      <description>&lt;P&gt;Update seems like what they want.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;To get around the limit of only two datasets (original and transactions) you could make a dataset view.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Plus the original dataset needs to have unique observations per BY group.&amp;nbsp; Perhaps your original dataset has multiple observations per id, perhaps because of repeated observation over rime.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You could get around both limitations by using a data step view and OBS=0 dataset option.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So make a view that interleaves the datasets by the id variables.&amp;nbsp; Set the datasets in reverse order of precedence.&amp;nbsp; Use this as the transactions dataset.&amp;nbsp; Then use OBS=0 option on any of the dataset as the original dataset.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data all / view=all;
   set c b a ;
   by id;
run;
data want;
  update a(obs=0) all;
  by id;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 19 Feb 2022 18:38:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merge-how-to-concatenate-values-of-the-same-variables-from-both/m-p/797405#M287503</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-02-19T18:38:40Z</dc:date>
    </item>
    <item>
      <title>Re: Merge: how to concatenate values of the same variables from both datasets</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merge-how-to-concatenate-values-of-the-same-variables-from-both/m-p/797422#M287515</link>
      <description>&lt;P&gt;Thanks&amp;nbsp;mkeintz for the codes. All of them work beautifully. And Tom's way to get around for 3 datasets is brilliant and very concise. Only wish I could mark both your replies as solutions.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 19 Feb 2022 21:27:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merge-how-to-concatenate-values-of-the-same-variables-from-both/m-p/797422#M287515</guid>
      <dc:creator>Solph</dc:creator>
      <dc:date>2022-02-19T21:27:51Z</dc:date>
    </item>
  </channel>
</rss>

