<?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: proc sql: Does something similar to &amp;quot;Partition By&amp;quot; exist? in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/proc-sql-Does-something-similar-to-quot-Partition-By-quot-exist/m-p/75427#M21878</link>
    <description>If you only want the top 5, the &lt;I&gt;outobs=&lt;/I&gt; option is your friend.&lt;BR /&gt;
&lt;BR /&gt;
If you have &lt;I&gt;group by&lt;/I&gt;, you probably need 2 steps as in:&lt;BR /&gt;
&lt;BR /&gt;
[pre]&lt;BR /&gt;
&lt;BR /&gt;
proc sql;                                     * find best selling products by region;&lt;BR /&gt;
  create view SUM as &lt;BR /&gt;
  select REGION, PRODUCT, sum(SALES) as SALES&lt;BR /&gt;
  from SASHELP.SHOES&lt;BR /&gt;
  group by REGION, PRODUCT&lt;BR /&gt;
  order by REGION, SALES desc ;&lt;BR /&gt;
quit;&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
data TOP3;                                    * keep the top 3 for each region;&lt;BR /&gt;
  set SUM;&lt;BR /&gt;
  by REGION ;&lt;BR /&gt;
  if first.REGION or lag(first.REGION) or lag2(first.REGION);&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
data TOP3;                                    * keep the top 3 for each region, alternative method;&lt;BR /&gt;
  set SUM;&lt;BR /&gt;
  by region ;&lt;BR /&gt;
  if first.REGION then N=0;&lt;BR /&gt;
  N+1;&lt;BR /&gt;
  if N &amp;lt;= 3;&lt;BR /&gt;
  drop N;&lt;BR /&gt;
run;</description>
    <pubDate>Wed, 14 Oct 2009 03:07:48 GMT</pubDate>
    <dc:creator>ChrisNZ</dc:creator>
    <dc:date>2009-10-14T03:07:48Z</dc:date>
    <item>
      <title>proc sql: Does something similar to "Partition By" exist?</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/proc-sql-Does-something-similar-to-quot-Partition-By-quot-exist/m-p/75425#M21876</link>
      <description>Hi all,&lt;BR /&gt;
  I have a question regarding proc sql sintax. &lt;BR /&gt;
I need to find top 5 rows in a query which contains group by. &lt;BR /&gt;
In particolar I need top 5 for each aggegation groups and not 5 rows at all.&lt;BR /&gt;
In Oracle sintax I think I could use "&lt;B&gt;Partition By&lt;/B&gt;" over a given rank in order to apply this wish.&lt;BR /&gt;
Could you please suggest me a workaround to use in proc sql or a specific function if existing?&lt;BR /&gt;
&lt;BR /&gt;
I really thank you a lot in advance for your support!&lt;BR /&gt;
&lt;BR /&gt;
KR&lt;BR /&gt;
&lt;BR /&gt;
Daniele.</description>
      <pubDate>Sat, 10 Oct 2009 13:31:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/proc-sql-Does-something-similar-to-quot-Partition-By-quot-exist/m-p/75425#M21876</guid>
      <dc:creator>deleted_user</dc:creator>
      <dc:date>2009-10-10T13:31:13Z</dc:date>
    </item>
    <item>
      <title>Re: proc sql: Does something similar to "Partition By" exist?</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/proc-sql-Does-something-similar-to-quot-Partition-By-quot-exist/m-p/75426#M21877</link>
      <description>I don't know a one-step approach, but I could do it in three.&lt;BR /&gt;
&lt;BR /&gt;
First SORT by group, then use PROC RANK with a BY on the group to get the ranks, and then either a DATA step or PROC SQL to pull them off.</description>
      <pubDate>Mon, 12 Oct 2009 02:02:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/proc-sql-Does-something-similar-to-quot-Partition-By-quot-exist/m-p/75426#M21877</guid>
      <dc:creator>Doc_Duke</dc:creator>
      <dc:date>2009-10-12T02:02:33Z</dc:date>
    </item>
    <item>
      <title>Re: proc sql: Does something similar to "Partition By" exist?</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/proc-sql-Does-something-similar-to-quot-Partition-By-quot-exist/m-p/75427#M21878</link>
      <description>If you only want the top 5, the &lt;I&gt;outobs=&lt;/I&gt; option is your friend.&lt;BR /&gt;
&lt;BR /&gt;
If you have &lt;I&gt;group by&lt;/I&gt;, you probably need 2 steps as in:&lt;BR /&gt;
&lt;BR /&gt;
[pre]&lt;BR /&gt;
&lt;BR /&gt;
proc sql;                                     * find best selling products by region;&lt;BR /&gt;
  create view SUM as &lt;BR /&gt;
  select REGION, PRODUCT, sum(SALES) as SALES&lt;BR /&gt;
  from SASHELP.SHOES&lt;BR /&gt;
  group by REGION, PRODUCT&lt;BR /&gt;
  order by REGION, SALES desc ;&lt;BR /&gt;
quit;&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
data TOP3;                                    * keep the top 3 for each region;&lt;BR /&gt;
  set SUM;&lt;BR /&gt;
  by REGION ;&lt;BR /&gt;
  if first.REGION or lag(first.REGION) or lag2(first.REGION);&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
data TOP3;                                    * keep the top 3 for each region, alternative method;&lt;BR /&gt;
  set SUM;&lt;BR /&gt;
  by region ;&lt;BR /&gt;
  if first.REGION then N=0;&lt;BR /&gt;
  N+1;&lt;BR /&gt;
  if N &amp;lt;= 3;&lt;BR /&gt;
  drop N;&lt;BR /&gt;
run;</description>
      <pubDate>Wed, 14 Oct 2009 03:07:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/proc-sql-Does-something-similar-to-quot-Partition-By-quot-exist/m-p/75427#M21878</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2009-10-14T03:07:48Z</dc:date>
    </item>
  </channel>
</rss>

