<?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: How to print out &amp;quot;N&amp;quot; observations for different groups using SQL in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-print-out-quot-N-quot-observations-for-different-groups/m-p/229126#M267969</link>
    <description>&lt;P&gt;TBH its not really the type of thing you want to do with SQL. &amp;nbsp;I mean you can, create the groups, then assign an incrementor, or do a subloop for max &amp;lt; outer loop max. &amp;nbsp;But its far easier to do in datastep. &amp;nbsp;Why do you need to use SQL? &amp;nbsp;Better to use the right tool for the right job.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 08 Oct 2015 14:10:04 GMT</pubDate>
    <dc:creator>RW9</dc:creator>
    <dc:date>2015-10-08T14:10:04Z</dc:date>
    <item>
      <title>How to print out "N" observations for different groups using SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-print-out-quot-N-quot-observations-for-different-groups/m-p/229117#M267968</link>
      <description>&lt;P&gt;Hi all,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I was wondering if there was a direct way in SQL to print out "n" number of observations by group.&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 procnames"&gt;data&lt;/SPAN&gt;  have&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
&lt;SPAN class="token keyword"&gt;input&lt;/SPAN&gt;  service &lt;SPAN class="token punctuation"&gt;$&lt;/SPAN&gt; PaidAmount : dollar&lt;SPAN class="token punctuation"&gt;.&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
&lt;SPAN class="token procnames"&gt;format&lt;/SPAN&gt;      PaidAmount dollar&lt;SPAN class="token punctuation"&gt;.&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
&lt;SPAN class="token datalines"&gt;&lt;SPAN class="token keyword"&gt;cards&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;&lt;SPAN class="token data string"&gt;
Bicycling  $1000           
Bicycling $2000 &lt;BR /&gt;Bicycling $3000&lt;BR /&gt;Rowing    $600&lt;BR /&gt;Rowing    $900&lt;BR /&gt;Rowing    $400&lt;BR /&gt;Rowing    $500 &lt;BR /&gt;&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;&lt;/SPAN&gt;
&lt;SPAN class="token procnames"&gt;run&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For example, for the above data, I would want to print out the top 2 paid amounts, by service&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc sql outobs = 2 ;&lt;/P&gt;
&lt;P&gt;select service, paidamount format=dollar24.2&lt;/P&gt;
&lt;P&gt;from have&lt;/P&gt;
&lt;P&gt;group by service&lt;/P&gt;
&lt;P&gt;order by paidamount desc ;&lt;/P&gt;
&lt;P&gt;quit ;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Obviously, the above code only prints out the top 2 paid amounts for bicycling since it's limited to outobs = 2. I'm looking more to get a table that is like this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Service&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Paid Amount&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;Bicycling&amp;nbsp;&amp;nbsp;&amp;nbsp; $3000&lt;/P&gt;
&lt;P&gt;Bicycling&amp;nbsp;&amp;nbsp;&amp;nbsp; $2000&lt;/P&gt;
&lt;P&gt;Rowing&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; $900&lt;/P&gt;
&lt;P&gt;Rowing&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; $600&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks in advance!&lt;/P&gt;</description>
      <pubDate>Thu, 08 Oct 2015 13:46:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-print-out-quot-N-quot-observations-for-different-groups/m-p/229117#M267968</guid>
      <dc:creator>johnjinkim</dc:creator>
      <dc:date>2015-10-08T13:46:29Z</dc:date>
    </item>
    <item>
      <title>Re: How to print out "N" observations for different groups using SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-print-out-quot-N-quot-observations-for-different-groups/m-p/229126#M267969</link>
      <description>&lt;P&gt;TBH its not really the type of thing you want to do with SQL. &amp;nbsp;I mean you can, create the groups, then assign an incrementor, or do a subloop for max &amp;lt; outer loop max. &amp;nbsp;But its far easier to do in datastep. &amp;nbsp;Why do you need to use SQL? &amp;nbsp;Better to use the right tool for the right job.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 08 Oct 2015 14:10:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-print-out-quot-N-quot-observations-for-different-groups/m-p/229126#M267969</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2015-10-08T14:10:04Z</dc:date>
    </item>
    <item>
      <title>Re: How to print out "N" observations for different groups using SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-print-out-quot-N-quot-observations-for-different-groups/m-p/229129#M267970</link>
      <description>&lt;P&gt;Like&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/45151"&gt;@RW9﻿&lt;/a&gt;&amp;nbsp;wrote this is something which is done easier using a SAS datastep.&lt;/P&gt;&lt;P&gt;In databases like Oracle there would be&amp;nbsp;Analytics funtions allowing&amp;nbsp;you to do such things - but that's nothing which has been implemented into the SAS SQL flavour.&lt;/P&gt;&lt;P&gt;&lt;A href="http://docs.oracle.com/cd/E11882_01/server.112/e41084/functions004.htm#SQLRF06174" target="_blank"&gt;http://docs.oracle.com/cd/E11882_01/server.112/e41084/functions004.htm#SQLRF06174&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 08 Oct 2015 14:14:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-print-out-quot-N-quot-observations-for-different-groups/m-p/229129#M267970</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2015-10-08T14:14:44Z</dc:date>
    </item>
    <item>
      <title>Re: How to print out "N" observations for different groups using SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-print-out-quot-N-quot-observations-for-different-groups/m-p/229138#M267971</link>
      <description>&lt;P&gt;I think a data step view is a good choice for this task&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data  have;
   input  service :$10. PaidAmount : dollar. @@;
   format      PaidAmount dollar.;
   cards;
Bicycling  $1000           
Bicycling $2000 Bicycling $3000 Rowing    $600 Rowing    $900 Rowing    $400 Rowing    $500
;;;;
   run;
proc print;
   run;

data _2perV / view=_2perV;
   set have;
   by service;
   if first.service then _c=0;
   _c+1;
   if _c le 2 then output;
   drop _c;
   run;
proc sql;
   select * from _2perV;
   quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 08 Oct 2015 14:38:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-print-out-quot-N-quot-observations-for-different-groups/m-p/229138#M267971</guid>
      <dc:creator>data_null__</dc:creator>
      <dc:date>2015-10-08T14:38:29Z</dc:date>
    </item>
  </channel>
</rss>

