<?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 Columns were not found in the contributing tables error. Calculated columns with Sums in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Columns-were-not-found-in-the-contributing-tables-error/m-p/628031#M185505</link>
    <description>&lt;P&gt;I have a table with reservation info and a table with vendor info that I am joining. I'm aggregating by vendor so that I can get the total count of reservations they did as well as the total amounts charged to the customer over their reservations and the total amounts charged by the vendor over their reservations. This all works fine. But when I add columns that calculate profit and gross profit percentage, I get an error "&lt;SPAN&gt;The following columns were not found in the contributing tables: CustomerTotal, VendorTotal.&lt;/SPAN&gt;" It seems to not like that I'm calculating with my sum columns in the same query. Is there a way to get around this or am I best off just doing 3 separate queries (one for the initial selects, one for calculating profit, one for calculating percent gross profit)?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;proc sql;&lt;BR /&gt;create table work.VendorGP as&lt;BR /&gt;SELECT aa.AssociateName, COUNT(rr.ReservationID) AS ResCount, SUM(rr.CustomerBaseAmt) AS CustomerTotal, SUM(rr.VendorBaseAmt) AS VendorTotal,&lt;BR /&gt;CustomerTotal-VendorTotal as Profit, Profit/CustomerTotal as PercentGP format PERCENT6.4&lt;BR /&gt;FROM work.RecentResReservations rr JOIN active.ascAssociates aa&lt;BR /&gt;ON rr.AscVendorId = aa.AssociateID&lt;BR /&gt;GROUP BY aa.AssociateName;&lt;BR /&gt;quit;&lt;/PRE&gt;&lt;P&gt;I have a similar query that seems to run correctly but gives the note "&lt;SPAN&gt;The query requires remerging summary statistics back with the original data.&lt;/SPAN&gt;" What triggers that note - the count/sums, the group by clause, or the calculated columns?&lt;/P&gt;</description>
    <pubDate>Thu, 27 Feb 2020 20:13:08 GMT</pubDate>
    <dc:creator>nicwoyak</dc:creator>
    <dc:date>2020-02-27T20:13:08Z</dc:date>
    <item>
      <title>Columns were not found in the contributing tables error. Calculated columns with Sums</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Columns-were-not-found-in-the-contributing-tables-error/m-p/628031#M185505</link>
      <description>&lt;P&gt;I have a table with reservation info and a table with vendor info that I am joining. I'm aggregating by vendor so that I can get the total count of reservations they did as well as the total amounts charged to the customer over their reservations and the total amounts charged by the vendor over their reservations. This all works fine. But when I add columns that calculate profit and gross profit percentage, I get an error "&lt;SPAN&gt;The following columns were not found in the contributing tables: CustomerTotal, VendorTotal.&lt;/SPAN&gt;" It seems to not like that I'm calculating with my sum columns in the same query. Is there a way to get around this or am I best off just doing 3 separate queries (one for the initial selects, one for calculating profit, one for calculating percent gross profit)?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;proc sql;&lt;BR /&gt;create table work.VendorGP as&lt;BR /&gt;SELECT aa.AssociateName, COUNT(rr.ReservationID) AS ResCount, SUM(rr.CustomerBaseAmt) AS CustomerTotal, SUM(rr.VendorBaseAmt) AS VendorTotal,&lt;BR /&gt;CustomerTotal-VendorTotal as Profit, Profit/CustomerTotal as PercentGP format PERCENT6.4&lt;BR /&gt;FROM work.RecentResReservations rr JOIN active.ascAssociates aa&lt;BR /&gt;ON rr.AscVendorId = aa.AssociateID&lt;BR /&gt;GROUP BY aa.AssociateName;&lt;BR /&gt;quit;&lt;/PRE&gt;&lt;P&gt;I have a similar query that seems to run correctly but gives the note "&lt;SPAN&gt;The query requires remerging summary statistics back with the original data.&lt;/SPAN&gt;" What triggers that note - the count/sums, the group by clause, or the calculated columns?&lt;/P&gt;</description>
      <pubDate>Thu, 27 Feb 2020 20:13:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Columns-were-not-found-in-the-contributing-tables-error/m-p/628031#M185505</guid>
      <dc:creator>nicwoyak</dc:creator>
      <dc:date>2020-02-27T20:13:08Z</dc:date>
    </item>
    <item>
      <title>Re: Columns were not found in the contributing tables error. Calculated columns with Sums</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Columns-were-not-found-in-the-contributing-tables-error/m-p/628040#M185506</link>
      <description>&lt;P&gt;When you reference something like:&lt;/P&gt;
&lt;PRE&gt;CustomerTotal-VendorTotal as Profit&lt;/PRE&gt;
&lt;P&gt;SQL expects that CustomerTotal and VendorTotal are variables in the source table. Since you are creating those variables as the same time then that is not the case (this time). So reuse the summary functions as used to create them:&lt;/P&gt;
&lt;PRE&gt;SUM(rr.CustomerBaseAmt) - SUM(rr.VendorBaseAmt) AS Profit&lt;/PRE&gt;
&lt;P&gt;Similar for the percent calculation. Though since you are using the calculated variable Profit it'll get a bit uglier.&lt;/P&gt;
&lt;P&gt;In some cases you can use the CALCULATED instruction to mean use of the value you created:&lt;/P&gt;
&lt;PRE&gt;Calculated(CustomerTotal) -Calculated(VendorTotal) as Profit.&lt;/PRE&gt;
&lt;P&gt;You might want to be thankful that your source data set didn't have CustomerTotal or VendorTotal variables already existing as you'd likely go a bit bonkers trying to figure out whey the shown calculated Profit and PercentGP looked very odd or generated different warnings.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The question about remerging note&amp;nbsp;would require the actual code to tell why.&lt;/P&gt;</description>
      <pubDate>Thu, 27 Feb 2020 20:35:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Columns-were-not-found-in-the-contributing-tables-error/m-p/628040#M185506</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2020-02-27T20:35:13Z</dc:date>
    </item>
  </channel>
</rss>

