<?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: sum two columns from two tables in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/sum-two-columns-from-two-tables/m-p/355388#M264728</link>
    <description>Hi, Can this be done in data step ?</description>
    <pubDate>Tue, 02 May 2017 20:23:17 GMT</pubDate>
    <dc:creator>octrout</dc:creator>
    <dc:date>2017-05-02T20:23:17Z</dc:date>
    <item>
      <title>sum two columns from two tables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sum-two-columns-from-two-tables/m-p/176725#M264726</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi All&lt;/P&gt;&lt;P&gt;is it possible in SAS that sum two variables from different table and put it as a new variable in&amp;nbsp; a new table based on another variable as a key. for example like this :&lt;/P&gt;&lt;P&gt;table A :&lt;/P&gt;&lt;TABLE border="0" cellpadding="0" cellspacing="0" style="width: 216px;"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD align="left" class="xl63" height="19" width="72"&gt;ID&lt;/TD&gt;&lt;TD align="left" class="xl63" style="border-left: none;" width="72"&gt;A&lt;/TD&gt;&lt;TD align="left" class="xl63" style="border-left: none;" width="72"&gt;B&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD align="right" class="xl63" height="19" style="border-top: none;"&gt;1&lt;/TD&gt;&lt;TD align="right" class="xl63" style="border-top: none; border-left: none;"&gt;33&lt;/TD&gt;&lt;TD align="right" class="xl63" style="border-top: none; border-left: none;"&gt;21&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD align="right" class="xl63" height="19" style="border-top: none;"&gt;2&lt;/TD&gt;&lt;TD align="right" class="xl63" style="border-top: none; border-left: none;"&gt;44&lt;/TD&gt;&lt;TD align="right" class="xl63" style="border-top: none; border-left: none;"&gt;22&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD align="right" class="xl63" height="19" style="border-top: none;"&gt;3&lt;/TD&gt;&lt;TD align="right" class="xl63" style="border-top: none; border-left: none;"&gt;55&lt;/TD&gt;&lt;TD align="right" class="xl63" style="border-top: none; border-left: none;"&gt;33&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;Table B :&lt;/P&gt;&lt;TABLE border="0" cellpadding="0" cellspacing="0" width="216"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD align="left" class="xl65" height="19" width="72"&gt;ID&lt;/TD&gt;&lt;TD align="left" class="xl65" style="border-left: none;" width="72"&gt;A&lt;/TD&gt;&lt;TD align="left" class="xl65" style="border-left: none;" width="72"&gt;B&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD align="right" class="xl65" height="19" style="border-top: none;"&gt;1&lt;/TD&gt;&lt;TD align="right" class="xl65" style="border-top: none; border-left: none;"&gt;48&lt;/TD&gt;&lt;TD align="right" class="xl65" style="border-top: none; border-left: none;"&gt;98&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD align="right" class="xl65" height="19" style="border-top: none;"&gt;2&lt;/TD&gt;&lt;TD align="right" class="xl65" style="border-top: none; border-left: none;"&gt;55&lt;/TD&gt;&lt;TD align="right" class="xl65" style="border-top: none; border-left: none;"&gt;12&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD align="right" class="xl65" height="19" style="border-top: none;"&gt;3&lt;/TD&gt;&lt;TD align="right" class="xl65" style="border-top: none; border-left: none;"&gt;67&lt;/TD&gt;&lt;TD align="right" class="xl65" style="border-top: none; border-left: none;"&gt;34&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;and in table c , sum A values and B values for same ID&lt;/P&gt;&lt;P&gt;Table C :&lt;/P&gt;&lt;TABLE border="0" cellpadding="0" cellspacing="0" width="216"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD align="left" class="xl65" height="19" width="72"&gt;ID&lt;/TD&gt;&lt;TD align="left" class="xl65" style="border-left: none;" width="72"&gt;A&lt;/TD&gt;&lt;TD align="left" class="xl65" style="border-left: none;" width="72"&gt;B&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD align="right" class="xl65" height="19" style="border-top: none;"&gt;1&lt;/TD&gt;&lt;TD align="right" class="xl65" style="border-top: none; border-left: none;"&gt;81&lt;/TD&gt;&lt;TD align="right" class="xl65" style="border-top: none; border-left: none;"&gt;119&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD align="right" class="xl65" height="19" style="border-top: none;"&gt;2&lt;/TD&gt;&lt;TD align="right" class="xl65" style="border-top: none; border-left: none;"&gt;99&lt;/TD&gt;&lt;TD align="right" class="xl65" style="border-top: none; border-left: none;"&gt;34&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD align="right" class="xl65" height="19" style="border-top: none;"&gt;3&lt;/TD&gt;&lt;TD align="right" class="xl65" style="border-top: none; border-left: none;"&gt;122&lt;/TD&gt;&lt;TD align="right" class="xl65" style="border-top: none; border-left: none;"&gt;67&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 17 Feb 2014 12:08:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sum-two-columns-from-two-tables/m-p/176725#M264726</guid>
      <dc:creator>Amin</dc:creator>
      <dc:date>2014-02-17T12:08:59Z</dc:date>
    </item>
    <item>
      <title>Re: sum two columns from two tables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sum-two-columns-from-two-tables/m-p/176726#M264727</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;use below attcahed code;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;data tablea;&lt;/P&gt;&lt;P&gt;input ID:$1. num_A num_B;&lt;/P&gt;&lt;P&gt;datalines;&lt;/P&gt;&lt;P&gt;1 33 21&lt;/P&gt;&lt;P&gt;2 44 22&lt;/P&gt;&lt;P&gt;3 55 33&lt;/P&gt;&lt;P&gt;;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;data tableB;&lt;/P&gt;&lt;P&gt;input ID:$1. num_A num_B;&lt;/P&gt;&lt;P&gt;datalines;&lt;/P&gt;&lt;P&gt;1 48 98&lt;/P&gt;&lt;P&gt;2 55 12&lt;/P&gt;&lt;P&gt;3 67 34&lt;/P&gt;&lt;P&gt;;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;proc sql;&lt;/P&gt;&lt;P&gt;create table table_new as&lt;/P&gt;&lt;P&gt;select a.id,a.num_a+b.num_a As Num_a,&lt;/P&gt;&lt;P&gt;a.num_b+b.num_b As Num_b&lt;/P&gt;&lt;P&gt;from tablea a&lt;/P&gt;&lt;P&gt;left join tableB b on a.id=b.id;&lt;/P&gt;&lt;P&gt;quit;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 17 Feb 2014 12:18:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sum-two-columns-from-two-tables/m-p/176726#M264727</guid>
      <dc:creator>TarunKumar</dc:creator>
      <dc:date>2014-02-17T12:18:37Z</dc:date>
    </item>
    <item>
      <title>Re: sum two columns from two tables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sum-two-columns-from-two-tables/m-p/355388#M264728</link>
      <description>Hi, Can this be done in data step ?</description>
      <pubDate>Tue, 02 May 2017 20:23:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sum-two-columns-from-two-tables/m-p/355388#M264728</guid>
      <dc:creator>octrout</dc:creator>
      <dc:date>2017-05-02T20:23:17Z</dc:date>
    </item>
    <item>
      <title>Re: sum two columns from two tables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sum-two-columns-from-two-tables/m-p/355426#M264729</link>
      <description>&lt;P&gt;Yes.&amp;nbsp; Since each table is&amp;nbsp;sorted by ID, you can read them both, interleaving observations by ID.&amp;nbsp; Then just have an array of totals to update with each observations, and with the last obs for each ID, output those totals, as per the DATA WANT step below:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data tablea;
  input ID:$1. num_A num_B;
datalines;
1 33 21
2 44 22
3 55 33
run;

data tableB;
  input ID:$1. num_A num_B;
datalines;
1 48 98
2 55 12
3 67 34
run;

data want ;
  set tablea tableb;
  by id;
  array sums {2} _temporary_;
  array nums {2} num_a num_b;

  /* Starting an ID group?  Zero out the sums */
  if first.id then do I=1 to dim(sums);
    sums{I}=0;
  end;

  /* Increment the sums and copy results back to NUM_A and NUM_B */
  do I=1 to dim(sums);
    sums{i}+nums{I};
    nums{I}=sums{I};
  end;

  if last.id;   /* Subsetting IF */
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 02 May 2017 22:49:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sum-two-columns-from-two-tables/m-p/355426#M264729</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2017-05-02T22:49:00Z</dc:date>
    </item>
    <item>
      <title>Re: sum two columns from two tables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sum-two-columns-from-two-tables/m-p/355467#M264730</link>
      <description>&lt;P&gt;How do I subtract instead of sums ? Thanks !&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE class=" language-sas"&gt;&lt;CODE class="  language-sas"&gt;&lt;SPAN class="token comment"&gt;/* Increment the sums and copy results back to NUM_A and NUM_B */&lt;/SPAN&gt;
  do I&lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt;&lt;SPAN class="token number"&gt;1&lt;/SPAN&gt; to &lt;SPAN class="token function"&gt;dim&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;sums&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
    sums&lt;SPAN class="token punctuation"&gt;{&lt;/SPAN&gt;i&lt;SPAN class="token punctuation"&gt;}&lt;/SPAN&gt;&lt;SPAN class="token operator"&gt;+&lt;/SPAN&gt;nums&lt;SPAN class="token punctuation"&gt;{&lt;/SPAN&gt;I&lt;SPAN class="token punctuation"&gt;}&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
    nums&lt;SPAN class="token punctuation"&gt;{&lt;/SPAN&gt;I&lt;SPAN class="token punctuation"&gt;}&lt;/SPAN&gt;&lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt;sums&lt;SPAN class="token punctuation"&gt;{&lt;/SPAN&gt;I&lt;SPAN class="token punctuation"&gt;}&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
  end&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 03 May 2017 03:27:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sum-two-columns-from-two-tables/m-p/355467#M264730</guid>
      <dc:creator>octrout</dc:creator>
      <dc:date>2017-05-03T03:27:54Z</dc:date>
    </item>
    <item>
      <title>Re: sum two columns from two tables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sum-two-columns-from-two-tables/m-p/355622#M264731</link>
      <description>&lt;P&gt;What values are you subtacting from what other values?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Do you always have exactly two tables, and are those tables identical in their ID structure?&amp;nbsp; If so, assuming you want to substract table B from table A,&amp;nbsp;you can still interleave the data sets using a SET with a BY statement, and then use the DIF function, as in:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#000080" face="SAS Monospace" size="2"&gt;&lt;STRONG&gt;data&lt;/STRONG&gt;&lt;/FONT&gt;&lt;FONT face="SAS Monospace" size="2"&gt; want;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000ff" face="SAS Monospace" size="2"&gt;&amp;nbsp; set&lt;/FONT&gt;&lt;FONT face="SAS Monospace" size="2"&gt; tableb tablea;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000ff" face="SAS Monospace" size="2"&gt;&amp;nbsp; by&lt;/FONT&gt;&lt;FONT face="SAS Monospace" size="2"&gt; id;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000ff" face="SAS Monospace" size="2"&gt;&amp;nbsp; array&lt;/FONT&gt;&lt;FONT face="SAS Monospace" size="2"&gt; nums {&lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT color="#008080" face="SAS Monospace" size="2"&gt;2&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="SAS Monospace" size="2"&gt;} num_a num_b;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000ff" face="SAS Monospace" size="2"&gt;&amp;nbsp; do&lt;/FONT&gt;&lt;FONT face="SAS Monospace" size="2"&gt; I=&lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT color="#008080" face="SAS Monospace" size="2"&gt;1&lt;/FONT&gt;&lt;/STRONG&gt; &lt;FONT color="#0000ff" face="SAS Monospace" size="2"&gt;to&lt;/FONT&gt;&lt;FONT face="SAS Monospace" size="2"&gt; dim(sums);&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="SAS Monospace" size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; nums{I}=dif(nums{I});&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000ff" face="SAS Monospace" size="2"&gt;&amp;nbsp; end&lt;/FONT&gt;&lt;FONT face="SAS Monospace" size="2"&gt;;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000ff" face="SAS Monospace" size="2"&gt;&amp;nbsp; if&lt;/FONT&gt;&lt;FONT face="SAS Monospace" size="2"&gt; last.id;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#000080" face="SAS Monospace" size="2"&gt;&lt;STRONG&gt;run&lt;/STRONG&gt;&lt;/FONT&gt;&lt;FONT face="SAS Monospace" size="2"&gt;;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The DIF function is&amp;nbsp; X-lag(X).&amp;nbsp; So&amp;nbsp;when the record-in-hand is the second record for a by-group (i.e. the tablea record), it's subtracting the prior value (i.e. from tableB above) from the current (tableA).&amp;nbsp;&amp;nbsp; Of course when the record-in-hand is the beginning of a by-group, the result is nonsense, but the subsetting "if last.id;" statement excludes that record.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you had used "set tablea tableb" instead, you would have gotten B-A.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 03 May 2017 14:25:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sum-two-columns-from-two-tables/m-p/355622#M264731</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2017-05-03T14:25:41Z</dc:date>
    </item>
  </channel>
</rss>

