<?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 Subquery evaluated to more than one row - Error in SAS Enterprise Guide</title>
    <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Subquery-evaluated-to-more-than-one-row-Error/m-p/530649#M32918</link>
    <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So I just recently began using SAS/SQL and have been working on creating a query that will give year to year numbers in adjacent columns.&amp;nbsp; I've tried a few different ways but the most direct way to do so seemed to be writing subqueries that would isolate the data by year.&amp;nbsp; I wrote a fairly simple query but I keep getting the "Error: Subquery evaluated to more than one row." message.&amp;nbsp; Below is a the query I wrote, any help would be greatly appreciated.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;

CREATE TABLE DASHBOARD_2 AS

SELECT
	wk AS Week,
	SUM(rev_usd) = 
		(SELECT rev_usd
		FROM lib.TRANSACTIONS
		WHERE yr = 2019
			AND type = 'WORK'
			AND charge &amp;lt;&amp;gt; 'FEES'
			AND part &amp;lt;&amp;gt; 'FULL')
				AS CY_Rev, 
	SUM(rev_usd) =
		(SELECT rev_usd
		FROM lib.TRANSACTIONS
		WHERE yr = 2018
			AND type = 'WORK'
			AND charge &amp;lt;&amp;gt; 'FEES'
			AND part &amp;lt;&amp;gt; 'FULL')
				AS PY_Rev,
	SUM(cxl) =
		(SELECT cxl
		FROM lib.TRANSACTIONS
		WHERE yr = 2019
			AND type = 'WORK'
			AND charge &amp;lt;&amp;gt; 'FEES'
			AND part &amp;lt;&amp;gt; 'FULL')
				AS CY_Cancel, 
	SUM(cxl) =
		(SELECT cxl
		FROM lib.TRANSACTIONS
		WHERE yr = 2018
			AND type = 'WORK'
			AND charge &amp;lt;&amp;gt; 'FEES'
			AND part&amp;lt;&amp;gt; 'FULL')
				AS PY_Cancel
FROM lib.TRANSACTIONS
WHERE yr = 2018 OR 2019
	AND type = 'WORK'
	AND charge &amp;lt;&amp;gt; 'FEES'
	AND part &amp;lt;&amp;gt; 'FULL'
GROUP BY Week
ORDER BY Week DESC;


quit;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Thanks!&lt;/P&gt;</description>
    <pubDate>Mon, 28 Jan 2019 15:38:22 GMT</pubDate>
    <dc:creator>S0MBR0</dc:creator>
    <dc:date>2019-01-28T15:38:22Z</dc:date>
    <item>
      <title>Subquery evaluated to more than one row - Error</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Subquery-evaluated-to-more-than-one-row-Error/m-p/530649#M32918</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So I just recently began using SAS/SQL and have been working on creating a query that will give year to year numbers in adjacent columns.&amp;nbsp; I've tried a few different ways but the most direct way to do so seemed to be writing subqueries that would isolate the data by year.&amp;nbsp; I wrote a fairly simple query but I keep getting the "Error: Subquery evaluated to more than one row." message.&amp;nbsp; Below is a the query I wrote, any help would be greatly appreciated.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;

CREATE TABLE DASHBOARD_2 AS

SELECT
	wk AS Week,
	SUM(rev_usd) = 
		(SELECT rev_usd
		FROM lib.TRANSACTIONS
		WHERE yr = 2019
			AND type = 'WORK'
			AND charge &amp;lt;&amp;gt; 'FEES'
			AND part &amp;lt;&amp;gt; 'FULL')
				AS CY_Rev, 
	SUM(rev_usd) =
		(SELECT rev_usd
		FROM lib.TRANSACTIONS
		WHERE yr = 2018
			AND type = 'WORK'
			AND charge &amp;lt;&amp;gt; 'FEES'
			AND part &amp;lt;&amp;gt; 'FULL')
				AS PY_Rev,
	SUM(cxl) =
		(SELECT cxl
		FROM lib.TRANSACTIONS
		WHERE yr = 2019
			AND type = 'WORK'
			AND charge &amp;lt;&amp;gt; 'FEES'
			AND part &amp;lt;&amp;gt; 'FULL')
				AS CY_Cancel, 
	SUM(cxl) =
		(SELECT cxl
		FROM lib.TRANSACTIONS
		WHERE yr = 2018
			AND type = 'WORK'
			AND charge &amp;lt;&amp;gt; 'FEES'
			AND part&amp;lt;&amp;gt; 'FULL')
				AS PY_Cancel
FROM lib.TRANSACTIONS
WHERE yr = 2018 OR 2019
	AND type = 'WORK'
	AND charge &amp;lt;&amp;gt; 'FEES'
	AND part &amp;lt;&amp;gt; 'FULL'
GROUP BY Week
ORDER BY Week DESC;


quit;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Thanks!&lt;/P&gt;</description>
      <pubDate>Mon, 28 Jan 2019 15:38:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Subquery-evaluated-to-more-than-one-row-Error/m-p/530649#M32918</guid>
      <dc:creator>S0MBR0</dc:creator>
      <dc:date>2019-01-28T15:38:22Z</dc:date>
    </item>
    <item>
      <title>Re: Subquery evaluated to more than one row - Error</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Subquery-evaluated-to-more-than-one-row-Error/m-p/530656#M32919</link>
      <description>&lt;P&gt;What would you like us to say?&amp;nbsp; The error is quite clear, one of the subqueries returns more than one record.&amp;nbsp; As we can't see your data, or operation, there is nothing we can do except repeat the error to you?&lt;/P&gt;
&lt;P&gt;Also, you will find it easier, if you want to use SAS, to learn Base SAS programming and use the functions, datasteps, and procedures used to manipulate data.&amp;nbsp; In most scenarios it will be far quicker, easier to program and more maintainable.&amp;nbsp; Sums can be done in proc means, summary, report etc.&lt;/P&gt;</description>
      <pubDate>Mon, 28 Jan 2019 15:56:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Subquery-evaluated-to-more-than-one-row-Error/m-p/530656#M32919</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2019-01-28T15:56:39Z</dc:date>
    </item>
    <item>
      <title>Re: Subquery evaluated to more than one row - Error</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Subquery-evaluated-to-more-than-one-row-Error/m-p/530696#M32920</link>
      <description>&lt;P&gt;You also have a logic error in this statement:&lt;/P&gt;
&lt;PRE&gt;WHERE yr = 2018 OR 2019
&lt;/PRE&gt;
&lt;P&gt;I suspect you intended to shortcut&lt;/P&gt;
&lt;P&gt;where yr=2018 or yr=2019 but that is not what your code would do.&lt;/P&gt;
&lt;P&gt;The comparison you wrote tests for yr=2018 OR if the value following or is true. SAS treats 2019 as "true" so the last where would select all records.&lt;/P&gt;
&lt;P&gt;What you likely wanted to use is&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;where yr in (2018, 2019)&lt;/PRE&gt;</description>
      <pubDate>Mon, 28 Jan 2019 18:05:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Subquery-evaluated-to-more-than-one-row-Error/m-p/530696#M32920</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2019-01-28T18:05:11Z</dc:date>
    </item>
    <item>
      <title>Re: Subquery evaluated to more than one row - Error</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Subquery-evaluated-to-more-than-one-row-Error/m-p/530699#M32921</link>
      <description>&lt;P&gt;Two things to add, re-reading your post. This syntax:&lt;/P&gt;
&lt;PRE class=" language-sas"&gt;&lt;CODE class="  language-sas"&gt;&lt;SPAN class="token function"&gt;SUM&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;rev_usd&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt; &lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt; 
		&lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;&lt;SPAN class="token statement"&gt;SELECT&lt;/SPAN&gt; rev_usd&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Does not look correct syntax at all, you do not put sum()=(subquery).&amp;nbsp; Possibly you have taken that from a specific database?&lt;/P&gt;
&lt;P&gt;Secondly, the where clause after the group by most likely does not do what you think it does.&amp;nbsp; Group by should use having clause.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 28 Jan 2019 18:07:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Subquery-evaluated-to-more-than-one-row-Error/m-p/530699#M32921</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2019-01-28T18:07:46Z</dc:date>
    </item>
    <item>
      <title>Re: Subquery evaluated to more than one row - Error</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Subquery-evaluated-to-more-than-one-row-Error/m-p/530719#M32923</link>
      <description>&lt;P&gt;Thank you ballardw, this is something I did not notice.&amp;nbsp; I revised this as well as a bit of the subquery syntax and I've gotten it to work.&amp;nbsp; I really appreciate the help!&lt;/P&gt;</description>
      <pubDate>Mon, 28 Jan 2019 18:34:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Subquery-evaluated-to-more-than-one-row-Error/m-p/530719#M32923</guid>
      <dc:creator>S0MBR0</dc:creator>
      <dc:date>2019-01-28T18:34:31Z</dc:date>
    </item>
    <item>
      <title>Re: Subquery evaluated to more than one row - Error</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Subquery-evaluated-to-more-than-one-row-Error/m-p/530723#M32924</link>
      <description>&lt;P&gt;It looks like you might be trying to do:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
CREATE TABLE DASHBOARD_2 AS
SELECT
	wk AS Week,
	SUM( case when yr = 2019 then rev_usd else . end) AS CY_Rev,
 	SUM( case when yr = 2018 then rev_usd else . end) AS PY_Rev, 
 	SUM( case when yr = 2019 then cxl else . end) AS CY_Cancel,
  	SUM( case when yr = 2018 then cxl else . end) AS PY_Cancel
FROM lib.TRANSACTIONS
WHERE yr in (2018, 2019)
	AND type = 'WORK'
	AND charge &amp;lt;&amp;gt; 'FEES'
	AND part &amp;lt;&amp;gt; 'FULL'
GROUP BY wk
ORDER BY Week DESC;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;(untested)&lt;/P&gt;</description>
      <pubDate>Mon, 28 Jan 2019 19:05:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Subquery-evaluated-to-more-than-one-row-Error/m-p/530723#M32924</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2019-01-28T19:05:17Z</dc:date>
    </item>
    <item>
      <title>Re: Subquery evaluated to more than one row - Error</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Subquery-evaluated-to-more-than-one-row-Error/m-p/530776#M32925</link>
      <description>&lt;P&gt;That is precisely what I was trying to do, thank you very much PGStats!&lt;/P&gt;</description>
      <pubDate>Mon, 28 Jan 2019 20:44:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Subquery-evaluated-to-more-than-one-row-Error/m-p/530776#M32925</guid>
      <dc:creator>S0MBR0</dc:creator>
      <dc:date>2019-01-28T20:44:11Z</dc:date>
    </item>
  </channel>
</rss>

