<?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 two files, add the variable when both ID exist and keep original if ID only in one data in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/merging-two-files-add-the-variable-when-both-ID-exist-and-keep/m-p/264612#M51926</link>
    <description>&lt;P&gt;Each ID is present in each dataset only once?&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Can you post sample data and expected output?&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 18 Apr 2016 16:10:31 GMT</pubDate>
    <dc:creator>Reeza</dc:creator>
    <dc:date>2016-04-18T16:10:31Z</dc:date>
    <item>
      <title>merging two files, add the variable when both ID exist and keep original if ID only in one data set</title>
      <link>https://communities.sas.com/t5/SAS-Programming/merging-two-files-add-the-variable-when-both-ID-exist-and-keep/m-p/264610#M51925</link>
      <description>&lt;P&gt;I have two data files, both have ID and amount (amount_A and amount_B)&lt;/P&gt;
&lt;P&gt;If the ID in both data sets, I want to add the amount together;&lt;/P&gt;
&lt;P&gt;if ID only in one data set, I will keep the original amount (amount_A or amount_B)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;can any one give me a sample code in both proc sql or data step? Thanks a lot.&lt;/P&gt;</description>
      <pubDate>Mon, 18 Apr 2016 16:00:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/merging-two-files-add-the-variable-when-both-ID-exist-and-keep/m-p/264610#M51925</guid>
      <dc:creator>fengyuwuzu</dc:creator>
      <dc:date>2016-04-18T16:00:03Z</dc:date>
    </item>
    <item>
      <title>Re: merging two files, add the variable when both ID exist and keep original if ID only in one data</title>
      <link>https://communities.sas.com/t5/SAS-Programming/merging-two-files-add-the-variable-when-both-ID-exist-and-keep/m-p/264612#M51926</link>
      <description>&lt;P&gt;Each ID is present in each dataset only once?&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Can you post sample data and expected output?&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 18 Apr 2016 16:10:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/merging-two-files-add-the-variable-when-both-ID-exist-and-keep/m-p/264612#M51926</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2016-04-18T16:10:31Z</dc:date>
    </item>
    <item>
      <title>Re: merging two files, add the variable when both ID exist and keep original if ID only in one data</title>
      <link>https://communities.sas.com/t5/SAS-Programming/merging-two-files-add-the-variable-when-both-ID-exist-and-keep/m-p/264614#M51928</link>
      <description>&lt;P&gt;If you are merging in a DATA step, this statement would do it:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;total = sum(amount_A, amount_B);&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The SUM function ignores missing values.&lt;/P&gt;</description>
      <pubDate>Mon, 18 Apr 2016 16:20:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/merging-two-files-add-the-variable-when-both-ID-exist-and-keep/m-p/264614#M51928</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2016-04-18T16:20:44Z</dc:date>
    </item>
    <item>
      <title>Re: merging two files, add the variable when both ID exist and keep original if ID only in one data</title>
      <link>https://communities.sas.com/t5/SAS-Programming/merging-two-files-add-the-variable-when-both-ID-exist-and-keep/m-p/264616#M51929</link>
      <description>&lt;P&gt;yes， each ID appears only once in each set; some IDs appears in both sets&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;example:&lt;/P&gt;
&lt;P&gt;A:&lt;/P&gt;
&lt;P&gt;ID amount&lt;/P&gt;
&lt;P&gt;1 10&lt;/P&gt;
&lt;P&gt;3 15&lt;/P&gt;
&lt;P&gt;4 20&lt;/P&gt;
&lt;P&gt;7 15&lt;/P&gt;
&lt;P&gt;9 12&lt;/P&gt;
&lt;P&gt;10 14&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;B:&lt;/P&gt;
&lt;P&gt;ID amount&lt;/P&gt;
&lt;P&gt;2 15&lt;/P&gt;
&lt;P&gt;3 20&lt;/P&gt;
&lt;P&gt;4 10&lt;/P&gt;
&lt;P&gt;5 12&lt;/P&gt;
&lt;P&gt;7 20&lt;/P&gt;
&lt;P&gt;8 15&lt;/P&gt;
&lt;P&gt;9 10&lt;/P&gt;
&lt;P&gt;11 20&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;want:&lt;/P&gt;
&lt;P&gt;1 10&lt;/P&gt;
&lt;P&gt;2 15&lt;/P&gt;
&lt;P&gt;3 35&lt;/P&gt;
&lt;P&gt;4 30&lt;/P&gt;
&lt;P&gt;5 12&lt;/P&gt;
&lt;P&gt;7 35&lt;/P&gt;
&lt;P&gt;8 15&lt;/P&gt;
&lt;P&gt;9 22&lt;/P&gt;
&lt;P&gt;10 14&lt;/P&gt;
&lt;P&gt;11 20&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;in my real data the ID is characteric, not sorted.&lt;/P&gt;</description>
      <pubDate>Mon, 18 Apr 2016 16:24:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/merging-two-files-add-the-variable-when-both-ID-exist-and-keep/m-p/264616#M51929</guid>
      <dc:creator>fengyuwuzu</dc:creator>
      <dc:date>2016-04-18T16:24:44Z</dc:date>
    </item>
    <item>
      <title>Re: merging two files, add the variable when both ID exist and keep original if ID only in one data</title>
      <link>https://communities.sas.com/t5/SAS-Programming/merging-two-files-add-the-variable-when-both-ID-exist-and-keep/m-p/264618#M51931</link>
      <description>&lt;P&gt;This is great. I was worrying missing values in one causes missing in sum&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I was thinking about using In= option in set statement, and then use if condition. Will this work?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 18 Apr 2016 16:34:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/merging-two-files-add-the-variable-when-both-ID-exist-and-keep/m-p/264618#M51931</guid>
      <dc:creator>fengyuwuzu</dc:creator>
      <dc:date>2016-04-18T16:34:36Z</dc:date>
    </item>
    <item>
      <title>Re: merging two files, add the variable when both ID exist and keep original if ID only in one data</title>
      <link>https://communities.sas.com/t5/SAS-Programming/merging-two-files-add-the-variable-when-both-ID-exist-and-keep/m-p/264619#M51932</link>
      <description>&lt;P&gt;Hi&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The DATA Step can merge two table, it needs sorted input. Whether a row was read from a table for a given id can be checked by uisng the IN= data set option. For an example to show the concept have a look at the code below. The joinType will show which table contributed to a given id value. With this information you can do whatever needs to be done.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;also have a look at the &lt;A href="http://support.sas.com/documentation/cdl/en/lefunctionsref/67960/HTML/default/viewer.htm#p1vjttz6nuankzn1gh4z3wgcu0bf.htm" target="_blank"&gt;COALESCE &lt;/A&gt;function.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have_a;
  infile cards;
  input
    ID
    amount_a
  ;
cards;
1 10
3 15
4 20
7 15
9 12
10 14
; 

data have_b;
  infile cards;
  input
    ID
    amount_b
  ;
cards;
2 15
3 20
4 10
5 12
7 20
8 15
9 10
11 20
;

data want;
  merge have_a(in=inA) have_b(in=inb);
  by id;
  length joinType $ 2;
  joinType = cats(inA, inB);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Bruno&lt;/P&gt;</description>
      <pubDate>Mon, 18 Apr 2016 16:37:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/merging-two-files-add-the-variable-when-both-ID-exist-and-keep/m-p/264619#M51932</guid>
      <dc:creator>BrunoMueller</dc:creator>
      <dc:date>2016-04-18T16:37:39Z</dc:date>
    </item>
    <item>
      <title>Re: merging two files, add the variable when both ID exist and keep original if ID only in one data</title>
      <link>https://communities.sas.com/t5/SAS-Programming/merging-two-files-add-the-variable-when-both-ID-exist-and-keep/m-p/264621#M51933</link>
      <description>&lt;P&gt;You could use IN=&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;However, it won't help if you are using SET.&amp;nbsp; You would need to use MERGE for IN= to help.&amp;nbsp; If you were using SET, you might code:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want;&lt;/P&gt;
&lt;P&gt;set a b;&lt;/P&gt;
&lt;P&gt;by ID;&lt;/P&gt;
&lt;P&gt;if first.ID then total = amount;&lt;/P&gt;
&lt;P&gt;else total + amount;&lt;/P&gt;
&lt;P&gt;if last.ID;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;There wouldn't be a need for IN= when using SET.&lt;/P&gt;</description>
      <pubDate>Mon, 18 Apr 2016 16:51:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/merging-two-files-add-the-variable-when-both-ID-exist-and-keep/m-p/264621#M51933</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2016-04-18T16:51:06Z</dc:date>
    </item>
    <item>
      <title>Re: merging two files, add the variable when both ID exist and keep original if ID only in one data</title>
      <link>https://communities.sas.com/t5/SAS-Programming/merging-two-files-add-the-variable-when-both-ID-exist-and-keep/m-p/264623#M51934</link>
      <description>Thank you so much. I learned more today.</description>
      <pubDate>Mon, 18 Apr 2016 16:55:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/merging-two-files-add-the-variable-when-both-ID-exist-and-keep/m-p/264623#M51934</guid>
      <dc:creator>fengyuwuzu</dc:creator>
      <dc:date>2016-04-18T16:55:40Z</dc:date>
    </item>
    <item>
      <title>Re: merging two files, add the variable when both ID exist and keep original if ID only in one data</title>
      <link>https://communities.sas.com/t5/SAS-Programming/merging-two-files-add-the-variable-when-both-ID-exist-and-keep/m-p/264624#M51935</link>
      <description>Thank you, Bruno. I learned something was new to me. Thanks!</description>
      <pubDate>Mon, 18 Apr 2016 16:56:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/merging-two-files-add-the-variable-when-both-ID-exist-and-keep/m-p/264624#M51935</guid>
      <dc:creator>fengyuwuzu</dc:creator>
      <dc:date>2016-04-18T16:56:35Z</dc:date>
    </item>
  </channel>
</rss>

