<?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: Is there a better way to use nested function in PROC SQL? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Is-there-a-better-way-to-use-nested-function-in-PROC-SQL/m-p/658009#M197189</link>
    <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/270406"&gt;@whymath&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Try this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
	select *
	from (select Group, avg(Num) as Mean
		  from test
		  group by Group)
	having Mean = max(Mean);
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Best,&lt;/P&gt;</description>
    <pubDate>Fri, 12 Jun 2020 10:21:59 GMT</pubDate>
    <dc:creator>ed_sas_member</dc:creator>
    <dc:date>2020-06-12T10:21:59Z</dc:date>
    <item>
      <title>Is there a better way to use nested function in PROC SQL?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Is-there-a-better-way-to-use-nested-function-in-PROC-SQL/m-p/658006#M197188</link>
      <description>&lt;DIV class="votecell post-layout--left"&gt;
&lt;DIV class="js-voting-container grid fd-column ai-stretch gs4 fc-black-200" data-post-id="62340658"&gt;&lt;SPAN style="font-family: inherit;"&gt;I am doing something needs more than once query in&lt;/SPAN&gt;&lt;SPAN style="font-family: inherit;"&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;CODE&gt;proc sql&lt;/CODE&gt;&lt;SPAN style="font-family: inherit;"&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="font-family: inherit;"&gt;so I try to write code perform like nest function in&lt;/SPAN&gt;&lt;SPAN style="font-family: inherit;"&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;CODE&gt;data step&lt;/CODE&gt;&lt;SPAN style="font-family: inherit;"&gt;. Here is an example: Data test have 2 variables, Group and Num.&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;DIV class="postcell post-layout--right"&gt;
&lt;DIV class="post-text"&gt;
&lt;PRE&gt;&lt;CODE&gt;data test;
  input Group$ Num;
  cards;
  A 10
  A 30
  B 10
  B 40
  C 30
  C 30
  ;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Now I am&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;STRONG&gt;looking for which&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;CODE&gt;Group&lt;/CODE&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;has the biggest mean value&lt;/STRONG&gt;. So firstly I compute mean for each&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;CODE&gt;Group&lt;/CODE&gt;, secondly get the max value of these means, Finnaly select the&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;CODE&gt;Group&lt;/CODE&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;whose mean has the same value of the result in step 2.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE&gt;proc sql;
  select Group, avg(Num) as Mean
  from test
  group by Group
  having Mean = (
    select max(mean) from (
      select avg(Num) as mean from test group by Group
    ) 
  )
  ;
quit;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Yes, I get the right answer, it's "C". But I don't like this method, it is too lengthy. The following code is wrong in syntax:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE&gt;proc sql;
  select Group, avg(Num) as Mean
  from test
  group by Group
  having Mean = max(avg(Num))
  ;
quit;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;But it is much beautiful, and shorter, too. Do you have any better way to do this?&lt;/P&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;</description>
      <pubDate>Fri, 12 Jun 2020 10:15:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Is-there-a-better-way-to-use-nested-function-in-PROC-SQL/m-p/658006#M197188</guid>
      <dc:creator>whymath</dc:creator>
      <dc:date>2020-06-12T10:15:00Z</dc:date>
    </item>
    <item>
      <title>Re: Is there a better way to use nested function in PROC SQL?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Is-there-a-better-way-to-use-nested-function-in-PROC-SQL/m-p/658009#M197189</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/270406"&gt;@whymath&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Try this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
	select *
	from (select Group, avg(Num) as Mean
		  from test
		  group by Group)
	having Mean = max(Mean);
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Best,&lt;/P&gt;</description>
      <pubDate>Fri, 12 Jun 2020 10:21:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Is-there-a-better-way-to-use-nested-function-in-PROC-SQL/m-p/658009#M197189</guid>
      <dc:creator>ed_sas_member</dc:creator>
      <dc:date>2020-06-12T10:21:59Z</dc:date>
    </item>
  </channel>
</rss>

