<?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 it possible to leave only largest values in table? in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/Is-it-possible-to-leave-only-largest-values-in-table/m-p/29050#M6797</link>
    <description>Hello,&lt;BR /&gt;
&lt;BR /&gt;
One solution is offered by proc means with output out statement, combined with transpose procedure:&lt;BR /&gt;
&lt;BR /&gt;
[pre]&lt;BR /&gt;
data have;&lt;BR /&gt;
input Country $  Region $ Item $ Week Sales ;&lt;BR /&gt;
datalines;&lt;BR /&gt;
LV r1 i1 1 12&lt;BR /&gt;
LV r2 i1 1 11&lt;BR /&gt;
LV r3 i1 1 10&lt;BR /&gt;
LV r4 i1 1 15&lt;BR /&gt;
LV r5 i1 1 13&lt;BR /&gt;
LT r6 i1 1 12&lt;BR /&gt;
LT r7 i1 1 10&lt;BR /&gt;
LT r8 i1 1 11&lt;BR /&gt;
LT r9 i1 1 18&lt;BR /&gt;
LT r0 i1 1 20&lt;BR /&gt;
;&lt;BR /&gt;
&lt;BR /&gt;
proc means data=have nway noprint;&lt;BR /&gt;
class Country Item Week;&lt;BR /&gt;
&lt;BR /&gt;
output out=want_ntr  idgroup(max(sales) out[3] (sales)=)/autoname ;&lt;BR /&gt;
&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
proc transpose data=want_ntr out=want (drop=_name_);&lt;BR /&gt;
by Country Item Week;&lt;BR /&gt;
&lt;BR /&gt;
var sales:;&lt;BR /&gt;
&lt;BR /&gt;
run;&lt;BR /&gt;
[/pre]&lt;BR /&gt;
&lt;BR /&gt;
Marius</description>
    <pubDate>Mon, 14 Mar 2011 18:37:28 GMT</pubDate>
    <dc:creator>deleted_user</dc:creator>
    <dc:date>2011-03-14T18:37:28Z</dc:date>
    <item>
      <title>Is it possible to leave only largest values in table?</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Is-it-possible-to-leave-only-largest-values-in-table/m-p/29049#M6796</link>
      <description>There are two countries, around 50 regions and a lot of different items in my dataset. &lt;BR /&gt;
What I want to do?&lt;BR /&gt;
Leave in table only rows with top 3 "sales" for each country, each item and week. In other words, if I have data like this:&lt;BR /&gt;
Country Region Item Week Sales&lt;BR /&gt;
LV         r1       i1      1       12&lt;BR /&gt;
LV         r2       i1      1       11&lt;BR /&gt;
LV         r3       i1      1       10&lt;BR /&gt;
LV         r4       i1      1       15&lt;BR /&gt;
LV         r5       i1      1       13&lt;BR /&gt;
......&lt;BR /&gt;
LT         r6       i1      1       12&lt;BR /&gt;
LT         r7       i1      1       10&lt;BR /&gt;
LT         r8       i1      1       11&lt;BR /&gt;
LT         r9       i1      1       18&lt;BR /&gt;
LT         r0       i1      1       20&lt;BR /&gt;
&lt;BR /&gt;
I want to get such results:&lt;BR /&gt;
Country Region Item Week Sales&lt;BR /&gt;
LV         r1       i1      1       12&lt;BR /&gt;
LV         r4       i1      1       15&lt;BR /&gt;
LV         r5       i1      1       13&lt;BR /&gt;
......&lt;BR /&gt;
LT         r6       i1      1       12&lt;BR /&gt;
LT         r9       i1      1       18&lt;BR /&gt;
LT         r0       i1      1       20&lt;BR /&gt;
&lt;BR /&gt;
Is it possible to do so?&lt;BR /&gt;
&lt;BR /&gt;
I got a feeling it should be possible with PROC UNIVARIATE, but not sure, how.

Message was edited by: efunk</description>
      <pubDate>Mon, 14 Mar 2011 18:25:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Is-it-possible-to-leave-only-largest-values-in-table/m-p/29049#M6796</guid>
      <dc:creator>efunk</dc:creator>
      <dc:date>2011-03-14T18:25:35Z</dc:date>
    </item>
    <item>
      <title>Re: Is it possible to leave only largest values in table?</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Is-it-possible-to-leave-only-largest-values-in-table/m-p/29050#M6797</link>
      <description>Hello,&lt;BR /&gt;
&lt;BR /&gt;
One solution is offered by proc means with output out statement, combined with transpose procedure:&lt;BR /&gt;
&lt;BR /&gt;
[pre]&lt;BR /&gt;
data have;&lt;BR /&gt;
input Country $  Region $ Item $ Week Sales ;&lt;BR /&gt;
datalines;&lt;BR /&gt;
LV r1 i1 1 12&lt;BR /&gt;
LV r2 i1 1 11&lt;BR /&gt;
LV r3 i1 1 10&lt;BR /&gt;
LV r4 i1 1 15&lt;BR /&gt;
LV r5 i1 1 13&lt;BR /&gt;
LT r6 i1 1 12&lt;BR /&gt;
LT r7 i1 1 10&lt;BR /&gt;
LT r8 i1 1 11&lt;BR /&gt;
LT r9 i1 1 18&lt;BR /&gt;
LT r0 i1 1 20&lt;BR /&gt;
;&lt;BR /&gt;
&lt;BR /&gt;
proc means data=have nway noprint;&lt;BR /&gt;
class Country Item Week;&lt;BR /&gt;
&lt;BR /&gt;
output out=want_ntr  idgroup(max(sales) out[3] (sales)=)/autoname ;&lt;BR /&gt;
&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
proc transpose data=want_ntr out=want (drop=_name_);&lt;BR /&gt;
by Country Item Week;&lt;BR /&gt;
&lt;BR /&gt;
var sales:;&lt;BR /&gt;
&lt;BR /&gt;
run;&lt;BR /&gt;
[/pre]&lt;BR /&gt;
&lt;BR /&gt;
Marius</description>
      <pubDate>Mon, 14 Mar 2011 18:37:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Is-it-possible-to-leave-only-largest-values-in-table/m-p/29050#M6797</guid>
      <dc:creator>deleted_user</dc:creator>
      <dc:date>2011-03-14T18:37:28Z</dc:date>
    </item>
    <item>
      <title>Re: Is it possible to leave only largest values in table?</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Is-it-possible-to-leave-only-largest-values-in-table/m-p/29051#M6798</link>
      <description>What do you want in the case of ties?&lt;BR /&gt;
&lt;BR /&gt;
You could also use the OBS info from the IDGROUP statement to POINT to the obs that you want for the subset.&lt;BR /&gt;
&lt;BR /&gt;
[pre]&lt;BR /&gt;
data have;&lt;BR /&gt;
   input (Country Region Item)(:$2.) Week Sales;&lt;BR /&gt;
   cards;&lt;BR /&gt;
LV r1 i1 1 12&lt;BR /&gt;
LV r2 i1 1 11&lt;BR /&gt;
LV r3 i1 1 10&lt;BR /&gt;
LV r4 i1 1 15&lt;BR /&gt;
LV r5 i1 1 13&lt;BR /&gt;
LT r6 i1 1 12&lt;BR /&gt;
LT r7 i1 1 10&lt;BR /&gt;
LT r8 i1 1 11&lt;BR /&gt;
LT r9 i1 1 18&lt;BR /&gt;
LT r0 i1 1 20&lt;BR /&gt;
;;;;&lt;BR /&gt;
   run;&lt;BR /&gt;
proc summary data=have nway;&lt;BR /&gt;
   class country;&lt;BR /&gt;
   output out=top3(keep=_obs_:) idgroup(max(sales) obs out[3](sales)=);&lt;BR /&gt;
   run;&lt;BR /&gt;
proc print;&lt;BR /&gt;
   run;&lt;BR /&gt;
data need;&lt;BR /&gt;
   set top3;&lt;BR /&gt;
   array obs&lt;LI&gt; _obs_:;&lt;BR /&gt;
   do j=1 to dim(obs);&lt;BR /&gt;
      point=obs&lt;J&gt;;&lt;BR /&gt;
      set have point=point;&lt;BR /&gt;
      output;&lt;BR /&gt;
      end;&lt;BR /&gt;
   drop _obs_: j;&lt;BR /&gt;
   run; &lt;BR /&gt;
proc print;&lt;BR /&gt;
   run;&lt;BR /&gt;
[/pre]&lt;BR /&gt;
&lt;BR /&gt;
[pre]&lt;BR /&gt;
Obs    Country    Region    Item    Week    Sales&lt;BR /&gt;
&lt;BR /&gt;
 1       LT         r0       i1       1       20&lt;BR /&gt;
 2       LT         r9       i1       1       18&lt;BR /&gt;
 3       LT         r6       i1       1       12&lt;BR /&gt;
 4       LV         r4       i1       1       15&lt;BR /&gt;
 5       LV         r5       i1       1       13&lt;BR /&gt;
 6       LV         r1       i1       1       12

Message was edited by: data _null_;&lt;/J&gt;&lt;/LI&gt;</description>
      <pubDate>Mon, 14 Mar 2011 18:41:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Is-it-possible-to-leave-only-largest-values-in-table/m-p/29051#M6798</guid>
      <dc:creator>data_null__</dc:creator>
      <dc:date>2011-03-14T18:41:40Z</dc:date>
    </item>
    <item>
      <title>Re: Is it possible to leave only largest values in table?</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Is-it-possible-to-leave-only-largest-values-in-table/m-p/29052#M6799</link>
      <description>It is essential for me that region names would be left in the table as I need to know in which region those sales were made.&lt;BR /&gt;
&lt;BR /&gt;
There's almost zero chance for ties as all sales(currency) in real data have 13 decimal places, for example: 29.6044367170295&lt;BR /&gt;
&lt;BR /&gt;
Message was edited by: efunk</description>
      <pubDate>Mon, 14 Mar 2011 18:42:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Is-it-possible-to-leave-only-largest-values-in-table/m-p/29052#M6799</guid>
      <dc:creator>efunk</dc:creator>
      <dc:date>2011-03-14T18:42:21Z</dc:date>
    </item>
    <item>
      <title>Re: Is it possible to leave only largest values in table?</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Is-it-possible-to-leave-only-largest-values-in-table/m-p/29053#M6800</link>
      <description>Hello efunk&lt;BR /&gt;
&lt;BR /&gt;
By sorting your data and using the first./last. programming functionality you can get what you want.  See the below code&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
data sales;&lt;BR /&gt;
length country region item $2 week sales 8;&lt;BR /&gt;
input country $ region $ item $ week sales;&lt;BR /&gt;
datalines;&lt;BR /&gt;
LV r1 i1 1 12&lt;BR /&gt;
LV r2 i1 1 11&lt;BR /&gt;
LV r3 i1 1 10&lt;BR /&gt;
LV r4 i1 1 15&lt;BR /&gt;
LV r5 i1 1 13&lt;BR /&gt;
LT r6 i1 1 12&lt;BR /&gt;
LT r7 i1 1 10&lt;BR /&gt;
LT r8 i1 1 11&lt;BR /&gt;
LT r9 i1 1 18&lt;BR /&gt;
LT r0 i1 1 20&lt;BR /&gt;
;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
proc sort data=sales ;&lt;BR /&gt;
  by country descending sales;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
data what_i_want;&lt;BR /&gt;
  set sales;&lt;BR /&gt;
  by country  descending sales;&lt;BR /&gt;
  if first.country then do;&lt;BR /&gt;
    counter=0;&lt;BR /&gt;
  end;&lt;BR /&gt;
  counter +1;&lt;BR /&gt;
&lt;BR /&gt;
  if counter&amp;lt;=3;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
D</description>
      <pubDate>Mon, 14 Mar 2011 18:50:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Is-it-possible-to-leave-only-largest-values-in-table/m-p/29053#M6800</guid>
      <dc:creator>darrylovia</dc:creator>
      <dc:date>2011-03-14T18:50:42Z</dc:date>
    </item>
    <item>
      <title>Re: Is it possible to leave only largest values in table?</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Is-it-possible-to-leave-only-largest-values-in-table/m-p/29054#M6801</link>
      <description>hello,&lt;BR /&gt;
&lt;BR /&gt;
this is not difficult. just merge "want" database with initial one. this merging will also solve the tiebreaking situation data _null_ refers at:&lt;BR /&gt;
&lt;BR /&gt;
[pre]&lt;BR /&gt;
proc sort data=have out=have_sort;&lt;BR /&gt;
by Country Item Week descending Sales;&lt;BR /&gt;
run; &lt;BR /&gt;
&lt;BR /&gt;
proc sort data=want(rename=(col1=sales)) out=want_sort;&lt;BR /&gt;
by Country Item Week descending Sales ;&lt;BR /&gt;
run; &lt;BR /&gt;
&lt;BR /&gt;
data final;&lt;BR /&gt;
merge  want_sort (in=a)  have_sort (in=b);&lt;BR /&gt;
by Country Item Week descending Sales ;&lt;BR /&gt;
&lt;BR /&gt;
if a and b;&lt;BR /&gt;
run; &lt;BR /&gt;
[/pre]&lt;BR /&gt;
&lt;BR /&gt;
Marius</description>
      <pubDate>Mon, 14 Mar 2011 18:51:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Is-it-possible-to-leave-only-largest-values-in-table/m-p/29054#M6801</guid>
      <dc:creator>deleted_user</dc:creator>
      <dc:date>2011-03-14T18:51:03Z</dc:date>
    </item>
    <item>
      <title>Re: Is it possible to leave only largest values in table?</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Is-it-possible-to-leave-only-largest-values-in-table/m-p/29055#M6802</link>
      <description>Works perfectly. Thank you all for lightning fast support.&lt;BR /&gt;
Special thanks to Marius.</description>
      <pubDate>Mon, 14 Mar 2011 19:03:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Is-it-possible-to-leave-only-largest-values-in-table/m-p/29055#M6802</guid>
      <dc:creator>efunk</dc:creator>
      <dc:date>2011-03-14T19:03:53Z</dc:date>
    </item>
  </channel>
</rss>

