<?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: First_Smallest,Second_Smallest,Third_Smallest in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/First-Smallest-Second-Smallest-Third-Smallest/m-p/933753#M367241</link>
    <description>&lt;P&gt;If&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;Your long dataset is already grouped (but not sorted) by CUSTID/VAR.&lt;/LI&gt;
&lt;LI&gt;You want to keep the dataset in original order&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;Then: there is a single DATA step solution:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want2 (drop=v);
  array tmpval {6} _temporary_ (6*%sysfunc(constant(big)));

  /*First pass */
  do v=1 by 1 until (last.var); 
    set have2 ;
    by custid var notsorted;
    tmpval{v}=value;
  end;

  call sortn(of tmpval{*});

  /*Second pass */
  do until (last.var);
    set have2 ;
    by custid var notsorted;
    rank=whichn(value,of tmpval{*});
    output;
    tmpval{rank}=constant('big');
  end;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;This code accommodates up to 6 obs per custid/var.&amp;nbsp; The initialization of the TMPVAL array to a set of %sysfunc(CONSTANT(BIG)) values helps with underpopulated (i.e. N&amp;lt;6) groups, because the sorting of values (between the first pass and second pass) keeps all observed values as the leftmost array elements (unlike initializing the array to missing values).&amp;nbsp; That permits the WHICHN function to correctly determine the rank.&amp;nbsp; That is also the reason why, once a rank is determined, the corresponding array element is reset to CONSTANT('BIG'), in preparation for the next group.&lt;/P&gt;</description>
    <pubDate>Wed, 26 Jun 2024 03:26:05 GMT</pubDate>
    <dc:creator>mkeintz</dc:creator>
    <dc:date>2024-06-26T03:26:05Z</dc:date>
    <item>
      <title>First_Smallest,Second_Smallest,Third_Smallest</title>
      <link>https://communities.sas.com/t5/SAS-Programming/First-Smallest-Second-Smallest-Third-Smallest/m-p/933666#M367199</link>
      <description>&lt;P&gt;Hello&lt;/P&gt;
&lt;P&gt;Lets say that have data set has wide structure than I know how to calculate -First_Smallest,Second_Smallest,Third_Smallest..&lt;/P&gt;
&lt;P&gt;Let's say that have data set has ling structure.&lt;/P&gt;
&lt;P&gt;What is the way to calculate-First_Smallest,Second_Smallest,Third_Smallest from this data set?&lt;/P&gt;
&lt;P&gt;(Without change the structure from long to wide)?&lt;/P&gt;
&lt;P&gt;Note-&lt;/P&gt;
&lt;P&gt;First_Smallest is the value in smallest observation&lt;/P&gt;
&lt;P&gt;Second_Smallest is the value in second smallest observation&lt;/P&gt;
&lt;P&gt;Third_Smallest is the value in third smallest observation&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data have1;
input CustID VAR $ mon1 mon2 mon3 mon4 mon5 mon6;
cards;
1 x 10 20 30 40 50 60
1 W 60 50 40 30 20 10
1 Z 30 30 30 0 0 0
2 x 30 20 10 10 40 0
;
Run;

data want1;
set have1;
First_Smallest=smallest(1,mon1,mon2,mon3,mon4,mon5,mon6) ;
Second_Smallest=smallest(2,mon1,mon2,mon3,mon4,mon5,mon6) ;
Third_Smallest=smallest(3,mon1,mon2,mon3,mon4,mon5,mon6) ;
Run;


data have2;
input CustID VAR $ month $ value;
cards;
1 x mon1 10
1 x mon2 20
1 x mon3 30
1 x mon4 40
1 x mon5 50
1 x mon6 60
1 w mon1 60
1 w mon2 50
1 w mon3 40
1 w mon4 30
1 w mon5 20
1 w mon6 10
1 z mon1 30
1 z mon2 30
1 z mon3 30
1 z mon4 0
1 z mon5 0
1 z mon6 0
2 x mon1 30
2 x mon2 20
2 x mon3 10
2 x mon4 10
2 x mon5 40
2 x mon6 0
;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 25 Jun 2024 11:37:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/First-Smallest-Second-Smallest-Third-Smallest/m-p/933666#M367199</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2024-06-25T11:37:16Z</dc:date>
    </item>
    <item>
      <title>Re: First_Smallest,Second_Smallest,Third_Smallest</title>
      <link>https://communities.sas.com/t5/SAS-Programming/First-Smallest-Second-Smallest-Third-Smallest/m-p/933667#M367200</link>
      <description>&lt;P&gt;Answered in your earlier thread&amp;nbsp;&lt;A href="https://communities.sas.com/t5/SAS-Programming/calculate-second-smallest-via-proc-summary/m-p/932430#M366806" target="_blank" rel="noopener"&gt;https://communities.sas.com/t5/SAS-Programming/calculate-second-smallest-via-proc-summary/m-p/932430#M366806&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also PROC RANK (which you also know about)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Terminology: "&lt;SPAN&gt;First_Smallest is the value in smallest observation" is wrong usage; it is the value of a particular variable that is smallest. There is no such thing as a "smallest observation".&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 25 Jun 2024 11:45:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/First-Smallest-Second-Smallest-Third-Smallest/m-p/933667#M367200</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2024-06-25T11:45:46Z</dc:date>
    </item>
    <item>
      <title>Re: First_Smallest,Second_Smallest,Third_Smallest</title>
      <link>https://communities.sas.com/t5/SAS-Programming/First-Smallest-Second-Smallest-Third-Smallest/m-p/933682#M367210</link>
      <description>&lt;P&gt;How about this?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=have2;
by CustID Var;
run;

proc rank data=have2 out=want(where=(rank &amp;lt;= 3)) /* keep top 3 ranks */
          ties=low;
   by CustID Var;
   var value;
   ranks Rank;
run;
proc sort data=want;
   by CustID Var Rank;
run;

proc print data=want;run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 25 Jun 2024 14:47:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/First-Smallest-Second-Smallest-Third-Smallest/m-p/933682#M367210</guid>
      <dc:creator>Rick_SAS</dc:creator>
      <dc:date>2024-06-25T14:47:16Z</dc:date>
    </item>
    <item>
      <title>Re: First_Smallest,Second_Smallest,Third_Smallest</title>
      <link>https://communities.sas.com/t5/SAS-Programming/First-Smallest-Second-Smallest-Third-Smallest/m-p/933753#M367241</link>
      <description>&lt;P&gt;If&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;Your long dataset is already grouped (but not sorted) by CUSTID/VAR.&lt;/LI&gt;
&lt;LI&gt;You want to keep the dataset in original order&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;Then: there is a single DATA step solution:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want2 (drop=v);
  array tmpval {6} _temporary_ (6*%sysfunc(constant(big)));

  /*First pass */
  do v=1 by 1 until (last.var); 
    set have2 ;
    by custid var notsorted;
    tmpval{v}=value;
  end;

  call sortn(of tmpval{*});

  /*Second pass */
  do until (last.var);
    set have2 ;
    by custid var notsorted;
    rank=whichn(value,of tmpval{*});
    output;
    tmpval{rank}=constant('big');
  end;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;This code accommodates up to 6 obs per custid/var.&amp;nbsp; The initialization of the TMPVAL array to a set of %sysfunc(CONSTANT(BIG)) values helps with underpopulated (i.e. N&amp;lt;6) groups, because the sorting of values (between the first pass and second pass) keeps all observed values as the leftmost array elements (unlike initializing the array to missing values).&amp;nbsp; That permits the WHICHN function to correctly determine the rank.&amp;nbsp; That is also the reason why, once a rank is determined, the corresponding array element is reset to CONSTANT('BIG'), in preparation for the next group.&lt;/P&gt;</description>
      <pubDate>Wed, 26 Jun 2024 03:26:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/First-Smallest-Second-Smallest-Third-Smallest/m-p/933753#M367241</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2024-06-26T03:26:05Z</dc:date>
    </item>
    <item>
      <title>Re: First_Smallest,Second_Smallest,Third_Smallest</title>
      <link>https://communities.sas.com/t5/SAS-Programming/First-Smallest-Second-Smallest-Third-Smallest/m-p/935288#M367735</link>
      <description>&lt;P&gt;For the sake of future readers who come to this thread, you can read about three different methods for finding the largest (or smallest) values in each group:&amp;nbsp;&lt;A href="https://blogs.sas.com/content/iml/2024/07/10/largest-values-for-group.html" target="_blank"&gt;Display the largest values for each group - The DO Loop (sas.com)&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;The DATA step with BY-group processing&amp;nbsp;&lt;/LI&gt;
&lt;LI&gt;PROC RANK&lt;/LI&gt;
&lt;LI&gt;PROC MEANS with the IDGROUP option on the OUTPUT statement&lt;/LI&gt;
&lt;/OL&gt;</description>
      <pubDate>Wed, 10 Jul 2024 09:50:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/First-Smallest-Second-Smallest-Third-Smallest/m-p/935288#M367735</guid>
      <dc:creator>Rick_SAS</dc:creator>
      <dc:date>2024-07-10T09:50:26Z</dc:date>
    </item>
  </channel>
</rss>

