<?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: Select all observations that their weight=Second largest weight in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Select-all-observations-that-their-weight-Second-largest-weight/m-p/900887#M356030</link>
    <description>&lt;P&gt;Yes, "mission creep" can affect the programming that you do, its often a good thing to plan for it. On the other hand, for programmers who just want to learn how to do some task in SAS, that's not an issue.&lt;/P&gt;</description>
    <pubDate>Tue, 31 Oct 2023 11:43:50 GMT</pubDate>
    <dc:creator>PaigeMiller</dc:creator>
    <dc:date>2023-10-31T11:43:50Z</dc:date>
    <item>
      <title>Select all observations that their weight=Second largest weight</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Select-all-observations-that-their-weight-Second-largest-weight/m-p/900429#M355853</link>
      <description>&lt;P&gt;Hello&lt;/P&gt;
&lt;P&gt;The following query select one observation that its weight value is second highest.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data class;
input Cust_ID  weight age;
1 59 20
2 61 25
3 95 40
4 92 25
5 51 18
6 92 21
7 50 43
8 60 23
9 70 71
10 92 62
;
Run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data class;
set sashelp.class;
Run;

proc sql outobs = 1;
    select name, weight 
    from class
    where weight not in (select max(weight) from class)
    order by weight desc
;quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;My question-&lt;/P&gt;
&lt;P&gt;Let's say that there are multiple observations&amp;nbsp; with weight=second highest value and I want to see these observations. What is the way to do it in one step? (So in this example I will see 3 observations since there are 3 observations with weight=92)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 28 Oct 2023 14:35:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Select-all-observations-that-their-weight-Second-largest-weight/m-p/900429#M355853</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2023-10-28T14:35:31Z</dc:date>
    </item>
    <item>
      <title>Re: Select all observations that their weight=Second largest weight</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Select-all-observations-that-their-weight-Second-largest-weight/m-p/900430#M355854</link>
      <description>&lt;P&gt;Use &lt;A href="https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.4/proc/p0le3p5ngj1zlbn1mh3tistq9t76.htm" target="_self"&gt;PROC RANK&lt;/A&gt;.&lt;/P&gt;</description>
      <pubDate>Sat, 28 Oct 2023 14:56:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Select-all-observations-that-their-weight-Second-largest-weight/m-p/900430#M355854</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2023-10-28T14:56:41Z</dc:date>
    </item>
    <item>
      <title>Re: Select all observations that their weight=Second largest weight</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Select-all-observations-that-their-weight-Second-largest-weight/m-p/900432#M355855</link>
      <description>May you please show the full code</description>
      <pubDate>Sat, 28 Oct 2023 15:50:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Select-all-observations-that-their-weight-Second-largest-weight/m-p/900432#M355855</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2023-10-28T15:50:12Z</dc:date>
    </item>
    <item>
      <title>Re: Select all observations that their weight=Second largest weight</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Select-all-observations-that-their-weight-Second-largest-weight/m-p/900435#M355858</link>
      <description>&lt;P&gt;Please look at the examples at the link I gave. One of the examples is analogous to your problem, only trivial changes are needed to use that example in your situation.&lt;/P&gt;</description>
      <pubDate>Sat, 28 Oct 2023 16:33:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Select-all-observations-that-their-weight-Second-largest-weight/m-p/900435#M355858</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2023-10-28T16:33:40Z</dc:date>
    </item>
    <item>
      <title>Re: Select all observations that their weight=Second largest weight</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Select-all-observations-that-their-weight-Second-largest-weight/m-p/900474#M355877</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/*
It is not right tool by using SQL.
Why not try data step ?
*/
Data class;
input Cust_ID  weight age;
cards;
1 59 20
2 61 25
3 95 40
4 92 25
5 51 18
6 92 21
7 50 43
8 60 23
9 70 71
10 92 62
;
Run;

proc sql;
select * 
 from (select * from class except select * from class having weight= max(weight))
   having weight= max(weight)
;quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 29 Oct 2023 08:59:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Select-all-observations-that-their-weight-Second-largest-weight/m-p/900474#M355877</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2023-10-29T08:59:34Z</dc:date>
    </item>
    <item>
      <title>Re: Select all observations that their weight=Second largest weight</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Select-all-observations-that-their-weight-Second-largest-weight/m-p/900540#M355905</link>
      <description>&lt;P&gt;A data step can pass through the data once to establish the MAX and 2ND highest values.&amp;nbsp; It can then re-read the data and keep those obs that match the second.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want  (drop=_:);
  set have (in=firstpass)  have (in=secondpass);

  retain _2ND _MAX;
  array  w{2} _2ND _MAX; 
  
  if firstpass then do;      /*Update array W, if needed*/
    if weight^=_MAX and weight&amp;gt;_2ND then do;
      _2ND=weight;
      call sortn(of w{*});
    end;
  end;
  if secondpass and weight=_2ND;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 29 Oct 2023 22:32:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Select-all-observations-that-their-weight-Second-largest-weight/m-p/900540#M355905</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2023-10-29T22:32:52Z</dc:date>
    </item>
    <item>
      <title>Re: Select all observations that their weight=Second largest weight</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Select-all-observations-that-their-weight-Second-largest-weight/m-p/900553#M355908</link>
      <description>&lt;P&gt;As proposed by&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10892"&gt;@PaigeMiller&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input Cust_ID  weight age;
  datalines;
1 59 20
2 61 25
3 95 40
4 92 25
5 51 18
6 92 21
7 50 43
8 60 23
9 70 71
10 92 62
;

proc rank data=have ties=dense descending out=want(where=(rank=2));
  ranks rank;
  var weight;
run;

proc print data=want;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 29 Oct 2023 23:46:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Select-all-observations-that-their-weight-Second-largest-weight/m-p/900553#M355908</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2023-10-29T23:46:20Z</dc:date>
    </item>
    <item>
      <title>Re: Select all observations that their weight=Second largest weight</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Select-all-observations-that-their-weight-Second-largest-weight/m-p/900612#M355922</link>
      <description>&lt;P&gt;In the long run, I think SAS users are better off using built-in SAS capabilities (like PROC RANK) rather than writing their own DATA step code or SQL code to achieve the same thing. SAS has done the hard job of writing this code, making it robust to different possible problems including handling of missing values, debugging their code and testing it, and then SAS's code is proved to work in many real-world applications. In addition, if the problem changes to keeping the 5th largest ... trivial changes are needed to the PROC RANK code; I'm not sure the changes are trivial to DATA step or SQL solutions.&lt;/P&gt;</description>
      <pubDate>Mon, 30 Oct 2023 09:51:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Select-all-observations-that-their-weight-Second-largest-weight/m-p/900612#M355922</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2023-10-30T09:51:13Z</dc:date>
    </item>
    <item>
      <title>Re: Select all observations that their weight=Second largest weight</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Select-all-observations-that-their-weight-Second-largest-weight/m-p/900830#M356006</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10892"&gt;@PaigeMiller&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;In the long run, I think SAS users are better off using built-in SAS capabilities (like PROC RANK) rather than writing their own DATA step code or SQL code&lt;/P&gt;
&lt;P&gt;...&lt;/P&gt;
&lt;P&gt;In addition, if the problem changes to keeping the 5th largest ... trivial changes are needed to the PROC RANK code; I'm not sure the changes are trivial to DATA step or SQL solutions.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In general (and in this case), I agree with this sentiment.&amp;nbsp; But with my clients/customers, I often find it's a good idea to anticipate a sort of mission creep, where the initial simple request (as in this topic) is expanded to include other objectives that won't be satisfied by the purpose-built SAS PROC.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In this particular case, changing the problem to the N'th largest would need only a trivial modification of the DATA step solution, but (I believe) a real headache for the PROC SQL approach.&lt;/P&gt;</description>
      <pubDate>Mon, 30 Oct 2023 22:27:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Select-all-observations-that-their-weight-Second-largest-weight/m-p/900830#M356006</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2023-10-30T22:27:02Z</dc:date>
    </item>
    <item>
      <title>Re: Select all observations that their weight=Second largest weight</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Select-all-observations-that-their-weight-Second-largest-weight/m-p/900887#M356030</link>
      <description>&lt;P&gt;Yes, "mission creep" can affect the programming that you do, its often a good thing to plan for it. On the other hand, for programmers who just want to learn how to do some task in SAS, that's not an issue.&lt;/P&gt;</description>
      <pubDate>Tue, 31 Oct 2023 11:43:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Select-all-observations-that-their-weight-Second-largest-weight/m-p/900887#M356030</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2023-10-31T11:43:50Z</dc:date>
    </item>
  </channel>
</rss>

