<?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: When I remove the title in the subquery the query runs fine with 2 columns please check in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/When-I-remove-the-title-in-the-subquery-the-query-runs-fine-with/m-p/958859#M374207</link>
    <description>&lt;P&gt;What are you trying to do?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Your first query returns two variables (what you called columns), neither of which has been given a NAME.&amp;nbsp; Let's change it to use SASHELP.CLASS so we can experiment easily.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
select avg(age), "Average Estimated Age"
from sashelp.class;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Tom_0-1739214929810.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/104514iEA01BEF6AD82548D/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Tom_0-1739214929810.png" alt="Tom_0-1739214929810.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;If you want to give that variable a LABEL then remove the comma.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
select avg(age) "Average Estimated Age"
from sashelp.class;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Tom_1-1739215122715.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/104515i32CC942D0F70A13E/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Tom_1-1739215122715.png" alt="Tom_1-1739215122715.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;You might also want to give the variable a NAME.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
select avg(age) as avg_age "Average Estimated Age"
from sashelp.class;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;To be clearer that the quoted string is just the LABEL for the variable add the LABEL= keyword.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
select avg(age) as avg_age label="Average Estimated Age"
from sashelp.class;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;For your second query you need to return only ONE value in the subquery to be able to use it with the &amp;gt; comparison operator.&amp;nbsp; So again you need to remove the comma.&amp;nbsp; You could leave the label, but why? There is no way for that label to be printed or saved anywhere with the way that query is constructed.&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
select Name, age
  from sashelp.class
  where age &amp;gt; (select avg(age) from sashelp.class)
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You can avoid the subquery (at least in PROC SQL) if you use the HAVING clause instead of the WHERE clause.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
select Name, age
  from sashelp.class
  having age &amp;gt; avg(age)
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Because as the SAS log states PROC SQL will remerge the aggregate function results for you.&lt;/P&gt;
&lt;PRE&gt;504  proc sql;
505  select Name, age
506    from sashelp.class
507    having age &amp;gt; avg(age)
508  ;
NOTE: The query requires remerging summary statistics back with the original
      data.
509  quit;
&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 10 Feb 2025 19:29:00 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2025-02-10T19:29:00Z</dc:date>
    <item>
      <title>When I remove the title in the subquery the query runs fine with 2 columns please check</title>
      <link>https://communities.sas.com/t5/SAS-Programming/When-I-remove-the-title-in-the-subquery-the-query-runs-fine-with/m-p/958841#M374203</link>
      <description>&lt;DIV&gt;proc sql;&lt;/DIV&gt;&lt;DIV&gt;select avg(PopEstimate1), "Average Estimated Population"&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;from sq.statepopulation;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;quit;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;proc sql;&lt;/DIV&gt;&lt;DIV&gt;select Name, PopEstimate1&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;from sq.statepopulation&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;where PopEstimate1 &amp;gt; (select avg(PopEstimate1),"Average Estimated Population"&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;from sq.statepopulation);&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;quit;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;</description>
      <pubDate>Mon, 10 Feb 2025 18:49:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/When-I-remove-the-title-in-the-subquery-the-query-runs-fine-with/m-p/958841#M374203</guid>
      <dc:creator>hmlong25</dc:creator>
      <dc:date>2025-02-10T18:49:58Z</dc:date>
    </item>
    <item>
      <title>Re: When I remove the title in the subquery the query runs fine with 2 columns please check</title>
      <link>https://communities.sas.com/t5/SAS-Programming/When-I-remove-the-title-in-the-subquery-the-query-runs-fine-with/m-p/958857#M374206</link>
      <description>&lt;P&gt;In your first code comma (,) separates the variable and it's label which is incorrect.&amp;nbsp; Comma separates only variables. So remove the comma and see if this works.&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
select avg(PopEstimate1)  "Average Estimated Population"
from sq.statepopulation;
quit;
 &lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 10 Feb 2025 19:20:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/When-I-remove-the-title-in-the-subquery-the-query-runs-fine-with/m-p/958857#M374206</guid>
      <dc:creator>A_Kh</dc:creator>
      <dc:date>2025-02-10T19:20:16Z</dc:date>
    </item>
    <item>
      <title>Re: When I remove the title in the subquery the query runs fine with 2 columns please check</title>
      <link>https://communities.sas.com/t5/SAS-Programming/When-I-remove-the-title-in-the-subquery-the-query-runs-fine-with/m-p/958859#M374207</link>
      <description>&lt;P&gt;What are you trying to do?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Your first query returns two variables (what you called columns), neither of which has been given a NAME.&amp;nbsp; Let's change it to use SASHELP.CLASS so we can experiment easily.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
select avg(age), "Average Estimated Age"
from sashelp.class;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Tom_0-1739214929810.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/104514iEA01BEF6AD82548D/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Tom_0-1739214929810.png" alt="Tom_0-1739214929810.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;If you want to give that variable a LABEL then remove the comma.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
select avg(age) "Average Estimated Age"
from sashelp.class;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Tom_1-1739215122715.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/104515i32CC942D0F70A13E/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Tom_1-1739215122715.png" alt="Tom_1-1739215122715.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;You might also want to give the variable a NAME.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
select avg(age) as avg_age "Average Estimated Age"
from sashelp.class;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;To be clearer that the quoted string is just the LABEL for the variable add the LABEL= keyword.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
select avg(age) as avg_age label="Average Estimated Age"
from sashelp.class;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;For your second query you need to return only ONE value in the subquery to be able to use it with the &amp;gt; comparison operator.&amp;nbsp; So again you need to remove the comma.&amp;nbsp; You could leave the label, but why? There is no way for that label to be printed or saved anywhere with the way that query is constructed.&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
select Name, age
  from sashelp.class
  where age &amp;gt; (select avg(age) from sashelp.class)
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You can avoid the subquery (at least in PROC SQL) if you use the HAVING clause instead of the WHERE clause.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
select Name, age
  from sashelp.class
  having age &amp;gt; avg(age)
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Because as the SAS log states PROC SQL will remerge the aggregate function results for you.&lt;/P&gt;
&lt;PRE&gt;504  proc sql;
505  select Name, age
506    from sashelp.class
507    having age &amp;gt; avg(age)
508  ;
NOTE: The query requires remerging summary statistics back with the original
      data.
509  quit;
&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 10 Feb 2025 19:29:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/When-I-remove-the-title-in-the-subquery-the-query-runs-fine-with/m-p/958859#M374207</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2025-02-10T19:29:00Z</dc:date>
    </item>
    <item>
      <title>Re: When I remove the title in the subquery the query runs fine with 2 columns please check</title>
      <link>https://communities.sas.com/t5/SAS-Programming/When-I-remove-the-title-in-the-subquery-the-query-runs-fine-with/m-p/958880#M374220</link>
      <description>The comma put the Label next to the average amount instead of above it. I should not have the comma, I agree. I think the Label= option is a better one also</description>
      <pubDate>Mon, 10 Feb 2025 21:48:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/When-I-remove-the-title-in-the-subquery-the-query-runs-fine-with/m-p/958880#M374220</guid>
      <dc:creator>hmlong25</dc:creator>
      <dc:date>2025-02-10T21:48:23Z</dc:date>
    </item>
    <item>
      <title>Re: When I remove the title in the subquery the query runs fine with 2 columns please check</title>
      <link>https://communities.sas.com/t5/SAS-Programming/When-I-remove-the-title-in-the-subquery-the-query-runs-fine-with/m-p/958883#M374221</link>
      <description>Thank you! Is it easier to use the Having clause when the query requires remerging summary stats back with the original instead of using a subquery as a general rule?</description>
      <pubDate>Mon, 10 Feb 2025 21:54:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/When-I-remove-the-title-in-the-subquery-the-query-runs-fine-with/m-p/958883#M374221</guid>
      <dc:creator>hmlong25</dc:creator>
      <dc:date>2025-02-10T21:54:14Z</dc:date>
    </item>
    <item>
      <title>Re: When I remove the title in the subquery the query runs fine with 2 columns please check</title>
      <link>https://communities.sas.com/t5/SAS-Programming/When-I-remove-the-title-in-the-subquery-the-query-runs-fine-with/m-p/958884#M374222</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/472415"&gt;@hmlong25&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;Thank you! Is it easier to use the Having clause when the query requires remerging summary stats back with the original instead of using a subquery as a general rule?&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;That is a matter personal preferance.&lt;/P&gt;
&lt;P&gt;I find it easier and clearer.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But if you have to use other SQL dialects you might not want to use it since they generally not support it so you might want to use a method that works the same in all of the SQL dialects you have to use commonly.&lt;/P&gt;</description>
      <pubDate>Mon, 10 Feb 2025 21:57:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/When-I-remove-the-title-in-the-subquery-the-query-runs-fine-with/m-p/958884#M374222</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2025-02-10T21:57:28Z</dc:date>
    </item>
  </channel>
</rss>

