<?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: Selecting minimum &amp;amp; maximum values of each group top-n in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Selecting-minimum-amp-maximum-values-of-each-group-top-n/m-p/392431#M94420</link>
    <description>&lt;P&gt;If you insist on doing your own programming, one simple way is like this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let n = 3;

proc sort data= sashelp.prdsal2 out=orig;
by country actual;
run;

data mins;
set orig; by country;
if first.country then order = 0;
order + 1;
if order &amp;lt;= &amp;amp;n;
actualMin = actual;
keep country actualMin;
run;

proc sort data=orig;
by country descending actual;
run;

data maxs;
set orig; by country;
if first.country then order = 0;
order + 1;
if order &amp;lt;= &amp;amp;n;
actualMax = actual;
keep country actualMax;
run;

data want;
merge mins maxs; by country;
run;

proc print noobs; run;

&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Fri, 01 Sep 2017 05:03:28 GMT</pubDate>
    <dc:creator>PGStats</dc:creator>
    <dc:date>2017-09-01T05:03:28Z</dc:date>
    <item>
      <title>Selecting minimum &amp; maximum values of each group top-n</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Selecting-minimum-amp-maximum-values-of-each-group-top-n/m-p/392427#M94417</link>
      <description>&lt;P&gt;Hi, I am trying to extract top 3 minimum and maximum actual sales&amp;nbsp;of each country (by group) &amp;nbsp;from the dataset&amp;nbsp;sashelp.prdsal2. My code is not dynamic, If any one could suggest how to incorporate changes within a single datastep, that would be nice. The program should be dynamic (if instead of top-3 we need top-n then it should work by rplacing 3 by n). the output should look like the attached screenshot. My code is -&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data= sashelp.prdsal2 out=orig;
by country actual;
run;

Data stage1;
set orig;
by country;
retain min ;
if first.country=1 then min=actual ;
if last.country =1 then max=actual ;
if last.country=1;
keep country min max;
run;

Data orig_6;
set orig;
by country;
if first.country=1 then delete;
if last.country =1 then delete; 
run;

data stage2;
set orig_6;
by country;
retain min ;
if first.country=1 then min=actual ;
if last.country =1 then max=actual ;
if last.country=1;
keep country min max;
run;

Data orig_12;
set orig_6;
by country;
if first.country=1 then delete;
if last.country =1 then delete; 
run;

data stage3;
set orig_12;
by country;
retain min ;
if first.country=1 then min=actual ;
if last.country =1 then max=actual ;
if last.country=1;
keep country min max;
run;

Data finalstage;
set stage1 stage2 stage3;
proc sort data=finalstage ;
by country min max;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="output.png" style="width: 306px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/14779i1BB380BE3FA41F51/image-size/large?v=v2&amp;amp;px=999" role="button" title="output.png" alt="output.png" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 01 Sep 2017 04:38:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Selecting-minimum-amp-maximum-values-of-each-group-top-n/m-p/392427#M94417</guid>
      <dc:creator>avnsas</dc:creator>
      <dc:date>2017-09-01T04:38:07Z</dc:date>
    </item>
    <item>
      <title>Re: Selecting minimum &amp; maximum values of each group top-n</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Selecting-minimum-amp-maximum-values-of-each-group-top-n/m-p/392430#M94419</link>
      <description>&lt;P&gt;Look at PROC UNIVARIATE. The second example on the documentation details how to extract these numbers and the NEXTRVAL &amp;amp; NEXTROBS control how many are displayed.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Heres a link to the documentation:&lt;/P&gt;
&lt;P&gt;&lt;A href="http://support.sas.com/documentation/cdl/en/procstat/66703/HTML/default/viewer.htm#procstat_univariate_syntax01.htm" target="_blank"&gt;http://support.sas.com/documentation/cdl/en/procstat/66703/HTML/default/viewer.htm#procstat_univariate_syntax01.htm&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/162202"&gt;@avnsas&lt;/a&gt; wrote:&lt;BR /&gt;
&lt;P&gt;Hi, I am trying to extract top 3 minimum and maximum actual sales&amp;nbsp;of each country (by group) &amp;nbsp;from the dataset&amp;nbsp;sashelp.prdsal2. My code is not dynamic, If any one could suggest how to incorporate changes within a single datastep, that would be nice. The program should be dynamic (if instead of top-3 we need top-n then it should work by rplacing 3 by n). the output should look like the attached screenshot. My code is -&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data= sashelp.prdsal2 out=orig;
by country actual;
run;

Data stage1;
set orig;
by country;
retain min ;
if first.country=1 then min=actual ;
if last.country =1 then max=actual ;
if last.country=1;
keep country min max;
run;

Data orig_6;
set orig;
by country;
if first.country=1 then delete;
if last.country =1 then delete; 
run;

data stage2;
set orig_6;
by country;
retain min ;
if first.country=1 then min=actual ;
if last.country =1 then max=actual ;
if last.country=1;
keep country min max;
run;

Data orig_12;
set orig_6;
by country;
if first.country=1 then delete;
if last.country =1 then delete; 
run;

data stage3;
set orig_12;
by country;
retain min ;
if first.country=1 then min=actual ;
if last.country =1 then max=actual ;
if last.country=1;
keep country min max;
run;

Data finalstage;
set stage1 stage2 stage3;
proc sort data=finalstage ;
by country min max;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="output.png" style="width: 306px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/14779i1BB380BE3FA41F51/image-size/large?v=v2&amp;amp;px=999" role="button" title="output.png" alt="output.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 01 Sep 2017 04:51:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Selecting-minimum-amp-maximum-values-of-each-group-top-n/m-p/392430#M94419</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2017-09-01T04:51:00Z</dc:date>
    </item>
    <item>
      <title>Re: Selecting minimum &amp; maximum values of each group top-n</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Selecting-minimum-amp-maximum-values-of-each-group-top-n/m-p/392431#M94420</link>
      <description>&lt;P&gt;If you insist on doing your own programming, one simple way is like this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let n = 3;

proc sort data= sashelp.prdsal2 out=orig;
by country actual;
run;

data mins;
set orig; by country;
if first.country then order = 0;
order + 1;
if order &amp;lt;= &amp;amp;n;
actualMin = actual;
keep country actualMin;
run;

proc sort data=orig;
by country descending actual;
run;

data maxs;
set orig; by country;
if first.country then order = 0;
order + 1;
if order &amp;lt;= &amp;amp;n;
actualMax = actual;
keep country actualMax;
run;

data want;
merge mins maxs; by country;
run;

proc print noobs; run;

&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 01 Sep 2017 05:03:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Selecting-minimum-amp-maximum-values-of-each-group-top-n/m-p/392431#M94420</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2017-09-01T05:03:28Z</dc:date>
    </item>
    <item>
      <title>Re: Selecting minimum &amp; maximum values of each group top-n</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Selecting-minimum-amp-maximum-values-of-each-group-top-n/m-p/392433#M94422</link>
      <description>&lt;P&gt;Hi, thanks for the reply but I am looking for a solution via datastep only, no PROC FREQ / PROC SUMMARY / PROC MEANS etc. Can something be done using DO-Loop &amp;nbsp;or point= &amp;nbsp;?&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 01 Sep 2017 05:10:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Selecting-minimum-amp-maximum-values-of-each-group-top-n/m-p/392433#M94422</guid>
      <dc:creator>avnsas</dc:creator>
      <dc:date>2017-09-01T05:10:07Z</dc:date>
    </item>
    <item>
      <title>Re: Selecting minimum &amp; maximum values of each group top-n</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Selecting-minimum-amp-maximum-values-of-each-group-top-n/m-p/392564#M94490</link>
      <description>&lt;P&gt;Why do you want to repeatedly use a hammer (datastep) when Proc Means will do this with one pass through the data (the tool that fits the job)?&lt;/P&gt;</description>
      <pubDate>Fri, 01 Sep 2017 14:50:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Selecting-minimum-amp-maximum-values-of-each-group-top-n/m-p/392564#M94490</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2017-09-01T14:50:39Z</dc:date>
    </item>
    <item>
      <title>Re: Selecting minimum &amp; maximum values of each group top-n</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Selecting-minimum-amp-maximum-values-of-each-group-top-n/m-p/392601#M94503</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13884"&gt;@ballardw&lt;/a&gt; wrote:&lt;BR /&gt;
&lt;P&gt;Why do you want to repeatedly use a hammer (datastep) when Proc Means will do this with one pass through the data (the tool that fits the job)?&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Because it's homework.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 01 Sep 2017 17:23:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Selecting-minimum-amp-maximum-values-of-each-group-top-n/m-p/392601#M94503</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2017-09-01T17:23:10Z</dc:date>
    </item>
    <item>
      <title>Re: Selecting minimum &amp; maximum values of each group top-n</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Selecting-minimum-amp-maximum-values-of-each-group-top-n/m-p/392611#M94507</link>
      <description>&lt;P&gt;I agree it is much easier to do via any of the proc steps mentioned, but I was curious to know how it can be done via datastep only for the purpose of learning [and yes it is homework, you caught me].&lt;/P&gt;</description>
      <pubDate>Fri, 01 Sep 2017 17:49:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Selecting-minimum-amp-maximum-values-of-each-group-top-n/m-p/392611#M94507</guid>
      <dc:creator>avnsas</dc:creator>
      <dc:date>2017-09-01T17:49:38Z</dc:date>
    </item>
    <item>
      <title>Re: Selecting minimum &amp; maximum values of each group top-n</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Selecting-minimum-amp-maximum-values-of-each-group-top-n/m-p/392637#M94516</link>
      <description>&lt;P&gt;Note: This task can be done in two steps: a sort and a data step. For extra bonus points.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Hint: you will need arrays.&lt;/P&gt;
&lt;P&gt;Hint:&amp;nbsp;consider using&amp;nbsp;the remainder (MOD) operation.&lt;/P&gt;</description>
      <pubDate>Fri, 01 Sep 2017 20:36:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Selecting-minimum-amp-maximum-values-of-each-group-top-n/m-p/392637#M94516</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2017-09-01T20:36:35Z</dc:date>
    </item>
    <item>
      <title>Re: Selecting minimum &amp; maximum values of each group top-n</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Selecting-minimum-amp-maximum-values-of-each-group-top-n/m-p/392776#M94554</link>
      <description>&lt;P&gt;Here is a data step solution - a proc and two data steps. No other Procs. I feel that the solution is useful for learning to code. The steps are:&lt;/P&gt;
&lt;P&gt;[1] Sort the data set by Country. This step can be avoided if we are prepared to store the ACTUAL into a _temporary_ array for each&lt;/P&gt;
&lt;P&gt;country and then use SORTN().&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;[2] Pre-process the sorted data set, to get the number of Countries and the number of observations per Country.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;[3] In the last data step, find the starting number of _N_ for each group, save them in an array (LOW[*]) and the ending _N_ s&lt;/P&gt;
&lt;P&gt;in another array (K[*]).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;[4] Then get the 3 (dynamic number -- &amp;amp;topbot) values from the bottom side and top side.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;[5] You get the output as you have wanted.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Better way is to say the Community what you found instead of keeping silent.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data= sashelp.prdsal2(keep = country actual) out=prdsal;
by country actual;
run;


data _null_;
   length group $100;
   retain group;
   do num = 1 by 1 until(last.country);
      set prdsal end = eof;
      by country;
   end;
   if last.country then  call catx(', ', group, num);
   if eof then do;
      call symputx('group', group);
      ngroup = countw(group, ',');
      call symputx('ngroup', ngroup);
   end;
run;

%put &amp;amp;group;

%put &amp;amp;ngroup;

%let topbot = 3;

data bottop;
   array k[&amp;amp;ngroup] _temporary_ (&amp;amp;group);
   array low[&amp;amp;ngroup] _temporary_ (&amp;amp;group);
   low[1] = 1;
   do i = 2 to &amp;amp;ngroup;
      low[i] = k[i - 1] + 1;
      k[i] = k[i - 1] + k[i];
   end;

   do g = 1 to &amp;amp;ngroup ;
      do i = 1 to &amp;amp;topbot;
         p = low[g] + i - 1;
         set prdsal point = p;
         Bot = actual;
         p = k[g] - &amp;amp;topbot + i;
         set prdsal point = p;
         Top = actual;
         output ;
      end;
   end;
stop;
drop i g actual;
run;      
      
proc print data = Bottop;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 02 Sep 2017 17:06:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Selecting-minimum-amp-maximum-values-of-each-group-top-n/m-p/392776#M94554</guid>
      <dc:creator>KachiM</dc:creator>
      <dc:date>2017-09-02T17:06:33Z</dc:date>
    </item>
    <item>
      <title>Re: Selecting minimum &amp; maximum values of each group top-n</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Selecting-minimum-amp-maximum-values-of-each-group-top-n/m-p/392847#M94573</link>
      <description>&lt;P&gt;thanks for the great code, really helpful explaination !!&lt;/P&gt;</description>
      <pubDate>Sun, 03 Sep 2017 05:50:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Selecting-minimum-amp-maximum-values-of-each-group-top-n/m-p/392847#M94573</guid>
      <dc:creator>avnsas</dc:creator>
      <dc:date>2017-09-03T05:50:53Z</dc:date>
    </item>
  </channel>
</rss>

