<?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: The art of SUBQUERY in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/The-art-of-SUBQUERY/m-p/304709#M60857</link>
    <description>&lt;P&gt;Unless I misunderstand the data structure, I think the way I would do this is:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;proc sql;&lt;/STRONG&gt;&lt;BR /&gt;&lt;STRONG&gt;create table dataset2&amp;nbsp;as&lt;/STRONG&gt;&lt;BR /&gt;&lt;STRONG&gt;select var1,&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;mean(var2) as average,&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;sum( case when var2&amp;gt;=1 then var3 else 0 end) as total1,&lt;BR /&gt;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;sum(case when var2&amp;lt;1 then var3 else 0 end) as total2&lt;/STRONG&gt;&lt;BR /&gt;&lt;STRONG&gt;from dataset1&lt;/STRONG&gt;&lt;BR /&gt;&lt;STRONG&gt;group by var1;&lt;/STRONG&gt;&lt;BR /&gt;&lt;STRONG&gt;quit;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Dan Keating&lt;/STRONG&gt;&lt;/P&gt;</description>
    <pubDate>Fri, 14 Oct 2016 16:17:12 GMT</pubDate>
    <dc:creator>NewsGuy</dc:creator>
    <dc:date>2016-10-14T16:17:12Z</dc:date>
    <item>
      <title>The art of SUBQUERY</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/The-art-of-SUBQUERY/m-p/304417#M60834</link>
      <description>&lt;P&gt;&lt;STRONG&gt;proc sql;&lt;/STRONG&gt;&lt;BR /&gt;&lt;STRONG&gt;create table dataset2&amp;nbsp;as&lt;/STRONG&gt;&lt;BR /&gt;&lt;STRONG&gt;select var1,&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;mean(var2) as average,&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;(select sum(var3) from dataset1 where var2&amp;gt;=1 group by var1),&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;(select sum(var3) from dataset1 where var2&amp;gt;=1 group by var1)&lt;/STRONG&gt;&lt;BR /&gt;&lt;STRONG&gt;from dataset1&lt;/STRONG&gt;&lt;BR /&gt;&lt;STRONG&gt;group by var1;&lt;/STRONG&gt;&lt;BR /&gt;&lt;STRONG&gt;quit;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;&amp;nbsp;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;ERROR: SUBQUERY EVALUATED TO MORE THAN ONE ROW&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Hi everyone,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am new to SAS and I'd like to understand about subqueries. So what I want is to have a table with 4 columns:&lt;/P&gt;&lt;OL&gt;&lt;LI&gt;var1&lt;/LI&gt;&lt;LI&gt;mean(var2)&lt;/LI&gt;&lt;LI&gt;sum(var3) only where var2&amp;gt;=1&lt;/LI&gt;&lt;LI&gt;sum(var3) only where var2&amp;lt;1&lt;/LI&gt;&lt;/OL&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Therefore I want the "where" statement to trigger only for the last two columns. The subquery&amp;nbsp;works if I dont add the "group by" but in that case I am not able to have the breakdown by var1. On the other hand if I add the "group by" in the subquery I get this error:&amp;nbsp;Subquery evaluated to more than one row.&lt;/P&gt;&lt;P&gt;I know that I could obtain the same table with joins but I really would like to understand better how to exploit subqueries.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks in advance for your help.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 13 Oct 2016 16:31:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/The-art-of-SUBQUERY/m-p/304417#M60834</guid>
      <dc:creator>Riversus</dc:creator>
      <dc:date>2016-10-13T16:31:41Z</dc:date>
    </item>
    <item>
      <title>Re: The art of SUBQUERY</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/The-art-of-SUBQUERY/m-p/304439#M60835</link>
      <description>&lt;P&gt;Since you want Var1 to be the same in the main query and the subquery, you must explicitly correlate the subqueries with the main query. Try:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table dataset2 as
select var1,
	mean(var2) as average,
	(select sum(var3) from dataset1 where var1=A.var1 and var2&amp;gt;=1 group by var1) as var2Sup1,
	(select sum(var3) from dataset1 where var1=A.var1 and  var2&amp;lt;1 group by var1) as var2Inf1
from dataset1&amp;nbsp;as A
group by var1;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 13 Oct 2016 18:13:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/The-art-of-SUBQUERY/m-p/304439#M60835</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2016-10-13T18:13:08Z</dc:date>
    </item>
    <item>
      <title>Re: The art of SUBQUERY</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/The-art-of-SUBQUERY/m-p/304441#M60836</link>
      <description>&lt;P&gt;For an overview of the types of applications that subqueries support, see this topic &lt;A href="http://blogs.sas.com/content/sasdummy/2013/06/28/building-an-sql-subquery-in-sas-enterprise-guide/" target="_self"&gt;about subqueries in SAS Enterprise Guide&lt;/A&gt;. &amp;nbsp;If you have SAS Enterprise Guide you can use point-and-click methods to generate some syntax you might learn from. &amp;nbsp;BUT, the SAS Enterprise Guide methods don't support correlated subqueries -- which&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/462"&gt;@PGStats﻿&lt;/a&gt;&amp;nbsp;just helped you with.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;In EG, here are the types of subqueries that you can create using query-based templates:&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;Subqueries that return a single value&lt;/LI&gt;
&lt;LI&gt;Subqueries that return multiple values (multiple rows of a single field)&lt;/LI&gt;
&lt;LI&gt;Subqueries that appear as part of a filter of the raw data (on the WHERE clause)&lt;/LI&gt;
&lt;LI&gt;Subqueries that appear as part of a filter on the grouped data (on the HAVING clause)&lt;/LI&gt;
&lt;LI&gt;Subqueries that appear as part of a recode condition as part of recoding a column in a computed column (on the SELECT clause)&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;Here are the types of subqueries that you &lt;STRONG&gt;cannot create&lt;/STRONG&gt; using query-based templates:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;Subqueries that form a derived table (subqueries that appear on the FROM clause)&lt;/LI&gt;
&lt;LI&gt;Subqueries that refer to columns on the outer query (correlated subqueries)&lt;/LI&gt;
&lt;/UL&gt;</description>
      <pubDate>Thu, 13 Oct 2016 18:25:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/The-art-of-SUBQUERY/m-p/304441#M60836</guid>
      <dc:creator>ChrisHemedinger</dc:creator>
      <dc:date>2016-10-13T18:25:15Z</dc:date>
    </item>
    <item>
      <title>Re: The art of SUBQUERY</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/The-art-of-SUBQUERY/m-p/304444#M60837</link>
      <description>To build on what Chris explained, in our SQL1 class, we show the types of subqueries that you can write in PROC SQL code and we also explain when/how to write non-correlated subqueries and use in-line views and have a discussion of correlated subqueries:&lt;BR /&gt;&lt;A href="https://support.sas.com/edu/schedules.html?ctry=us&amp;amp;crs=SQL1#s1=3" target="_blank"&gt;https://support.sas.com/edu/schedules.html?ctry=us&amp;amp;crs=SQL1#s1=3&lt;/A&gt;  &lt;BR /&gt;&lt;BR /&gt;cynthia</description>
      <pubDate>Thu, 13 Oct 2016 18:38:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/The-art-of-SUBQUERY/m-p/304444#M60837</guid>
      <dc:creator>Cynthia_sas</dc:creator>
      <dc:date>2016-10-13T18:38:39Z</dc:date>
    </item>
    <item>
      <title>Re: The art of SUBQUERY</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/The-art-of-SUBQUERY/m-p/304453#M60838</link>
      <description>&lt;P&gt;I should add that no subquery or join is needed to perform that operation. You can simply 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 dataset2 as
select var1,
	mean(var2) as average,
	sum(var3 * (var2&amp;gt;=1)) as var2Sup1,
	sum(var3 * (var2&amp;lt;1)) as var2Inf1
from dataset1
group by var1;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 13 Oct 2016 19:17:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/The-art-of-SUBQUERY/m-p/304453#M60838</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2016-10-13T19:17:29Z</dc:date>
    </item>
    <item>
      <title>Re: The art of SUBQUERY</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/The-art-of-SUBQUERY/m-p/304542#M60843</link>
      <description>&lt;P&gt;&lt;STRONG&gt;proc sql;&lt;/STRONG&gt;&lt;BR /&gt;&lt;STRONG&gt;create table dataset2&amp;nbsp;as&lt;/STRONG&gt;&lt;BR /&gt;&lt;STRONG&gt;select var1,&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;mean(var2) as average,&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;(select sum(var3) from dataset1 where var2&amp;gt;=1 and var1=a.var1),&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;(select sum(var3) from dataset1 where var2&amp;gt;=1 and var1=a.var1)&lt;/STRONG&gt;&lt;BR /&gt;&lt;STRONG&gt;from dataset1 as a&lt;/STRONG&gt;&lt;BR /&gt;&lt;STRONG&gt;group by var1;&lt;/STRONG&gt;&lt;BR /&gt;&lt;STRONG&gt;quit;&lt;/STRONG&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 14 Oct 2016 06:13:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/The-art-of-SUBQUERY/m-p/304542#M60843</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2016-10-14T06:13:27Z</dc:date>
    </item>
    <item>
      <title>Re: The art of SUBQUERY</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/The-art-of-SUBQUERY/m-p/304558#M60844</link>
      <description>&lt;P&gt;I chose this answer as solution but I'd like to understand how the formula exactly work.&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 function"&gt;sum&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;var3 &lt;SPAN class="token operator"&gt;*&lt;/SPAN&gt; &lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;var2&lt;SPAN class="token operator"&gt;&amp;gt;=&lt;/SPAN&gt;&lt;SPAN class="token number"&gt;1&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt; as var2Sup1&lt;BR /&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;it sums var3 by var1 as I want but I dont understand how the multiplication works. Is (var2&amp;gt;=1) a dummy variable?&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 14 Oct 2016 07:32:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/The-art-of-SUBQUERY/m-p/304558#M60844</guid>
      <dc:creator>Riversus</dc:creator>
      <dc:date>2016-10-14T07:32:55Z</dc:date>
    </item>
    <item>
      <title>Re: The art of SUBQUERY</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/The-art-of-SUBQUERY/m-p/304561#M60845</link>
      <description>&lt;P&gt;Also this code is good. Thank you &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt; I dont know how to select more than a solution to the post&lt;/P&gt;</description>
      <pubDate>Fri, 14 Oct 2016 07:34:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/The-art-of-SUBQUERY/m-p/304561#M60845</guid>
      <dc:creator>Riversus</dc:creator>
      <dc:date>2016-10-14T07:34:16Z</dc:date>
    </item>
    <item>
      <title>Re: The art of SUBQUERY</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/The-art-of-SUBQUERY/m-p/304709#M60857</link>
      <description>&lt;P&gt;Unless I misunderstand the data structure, I think the way I would do this is:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;proc sql;&lt;/STRONG&gt;&lt;BR /&gt;&lt;STRONG&gt;create table dataset2&amp;nbsp;as&lt;/STRONG&gt;&lt;BR /&gt;&lt;STRONG&gt;select var1,&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;mean(var2) as average,&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;sum( case when var2&amp;gt;=1 then var3 else 0 end) as total1,&lt;BR /&gt;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;sum(case when var2&amp;lt;1 then var3 else 0 end) as total2&lt;/STRONG&gt;&lt;BR /&gt;&lt;STRONG&gt;from dataset1&lt;/STRONG&gt;&lt;BR /&gt;&lt;STRONG&gt;group by var1;&lt;/STRONG&gt;&lt;BR /&gt;&lt;STRONG&gt;quit;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Dan Keating&lt;/STRONG&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 14 Oct 2016 16:17:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/The-art-of-SUBQUERY/m-p/304709#M60857</guid>
      <dc:creator>NewsGuy</dc:creator>
      <dc:date>2016-10-14T16:17:12Z</dc:date>
    </item>
    <item>
      <title>Re: The art of SUBQUERY</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/The-art-of-SUBQUERY/m-p/304714#M60858</link>
      <description>&lt;P&gt;Hey&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/3719"&gt;@NewsGuy﻿&lt;/a&gt;, I think your approach is the same as&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/462"&gt;@PGStats﻿&lt;/a&gt;, except that you used a CASE instead of relying on the fact that an equality check (var2&amp;gt;=1) will resolve to a 1 or a 0, depending on the value of var2. &amp;nbsp;Your approach is more explicit, while&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/462"&gt;@PGStats﻿&lt;/a&gt;&amp;nbsp;approach is more elegant (even if it takes a moment to think it through).&lt;/P&gt;</description>
      <pubDate>Fri, 14 Oct 2016 16:30:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/The-art-of-SUBQUERY/m-p/304714#M60858</guid>
      <dc:creator>ChrisHemedinger</dc:creator>
      <dc:date>2016-10-14T16:30:57Z</dc:date>
    </item>
    <item>
      <title>Re: The art of SUBQUERY</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/The-art-of-SUBQUERY/m-p/304716#M60859</link>
      <description>I completely agree that it is the same and it is not a sub-query, so if the goal was to learn subqueries, it doesn't help.&lt;BR /&gt;I love the more elegant way and will try to use it, as well. But I offered mine up since it's something I use constantly and thought people might like it.&lt;BR /&gt;&lt;BR /&gt;Dan&lt;BR /&gt;</description>
      <pubDate>Fri, 14 Oct 2016 16:32:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/The-art-of-SUBQUERY/m-p/304716#M60859</guid>
      <dc:creator>NewsGuy</dc:creator>
      <dc:date>2016-10-14T16:32:57Z</dc:date>
    </item>
  </channel>
</rss>

