<?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: Third highest salary as per Department in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Third-highest-salary-as-per-Department/m-p/340222#M77735</link>
    <description>&lt;P&gt;Here is a solution for Q1, including a tie breaker&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA Salary2;
INPUT EmpName $ Salary Department $;
DATALINES;
Steve 150 Finance
James 120 IT
Andrew 120 Finance
Mark 100 Back
Adam 110 IT
Niti 140 IT
Aman 110 HR
Lewis 130 Finance
Smith 125 Back
PG 140 IT   &amp;lt;-- tied with Niti
;

proc sql;
/* Break ties */
create table S as 
select *, salary + 0.0001*rand("uniform") as s 
from Salary2;

create table thirdSalary as
select EmpName, Salary, Department
from 
    (select * 
     from 
        (select * 
         from S 
         group by Department
         having s &amp;lt; max(s))
     group by Department
     having s &amp;lt; max(s))
group by department
having s = max(s);

drop table S;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you don't need to break ties then replace dataset S with Salary2 and variable s with Salary.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Q2: There is no such thing as the&lt;EM&gt; last&lt;/EM&gt; observation in SQL. You would need an observation number to solve this problem in SQL.&lt;/P&gt;</description>
    <pubDate>Sun, 12 Mar 2017 06:35:41 GMT</pubDate>
    <dc:creator>PGStats</dc:creator>
    <dc:date>2017-03-12T06:35:41Z</dc:date>
    <item>
      <title>Third highest salary as per Department</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Third-highest-salary-as-per-Department/m-p/340220#M77733</link>
      <description>&lt;P&gt;Hi Team&lt;/P&gt;&lt;P&gt;Please help me with this scenario.I want to solve it by Proc Sql and not base SAS as i was able to solve it by first.variable and Proc rank so i need it to solve using Proc Sql.&lt;/P&gt;&lt;P&gt;Dataset:-&lt;/P&gt;&lt;P&gt;DATA SALARY1;&lt;BR /&gt;INPUT EmpName $ Salary Department $;&lt;BR /&gt;DATALINES;&lt;BR /&gt;Steve 150 Finance&lt;BR /&gt;James 120 IT&lt;BR /&gt;Andrew 120 Finance&lt;BR /&gt;Mark 100 Back&lt;BR /&gt;Adam 110 IT&lt;BR /&gt;Niti 140 IT&lt;BR /&gt;Aman 110 HR&lt;BR /&gt;Lewis 130 Finance&lt;BR /&gt;Smith 125 Back&lt;BR /&gt;;&lt;BR /&gt;RUN;&lt;/P&gt;&lt;P&gt;Output:-&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Andrew 120 Finance&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Adam 110 IT&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Q1.If there is no third observartion for particular department i don`t need that department salary to show up.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Q2.If there is no third observation for particular department i need the last salary for that department.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Thanks in Advance.Please help.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 12 Mar 2017 05:47:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Third-highest-salary-as-per-Department/m-p/340220#M77733</guid>
      <dc:creator>Nitish1003</dc:creator>
      <dc:date>2017-03-12T05:47:01Z</dc:date>
    </item>
    <item>
      <title>Re: Third highest salary as per Department</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Third-highest-salary-as-per-Department/m-p/340222#M77735</link>
      <description>&lt;P&gt;Here is a solution for Q1, including a tie breaker&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA Salary2;
INPUT EmpName $ Salary Department $;
DATALINES;
Steve 150 Finance
James 120 IT
Andrew 120 Finance
Mark 100 Back
Adam 110 IT
Niti 140 IT
Aman 110 HR
Lewis 130 Finance
Smith 125 Back
PG 140 IT   &amp;lt;-- tied with Niti
;

proc sql;
/* Break ties */
create table S as 
select *, salary + 0.0001*rand("uniform") as s 
from Salary2;

create table thirdSalary as
select EmpName, Salary, Department
from 
    (select * 
     from 
        (select * 
         from S 
         group by Department
         having s &amp;lt; max(s))
     group by Department
     having s &amp;lt; max(s))
group by department
having s = max(s);

drop table S;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you don't need to break ties then replace dataset S with Salary2 and variable s with Salary.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Q2: There is no such thing as the&lt;EM&gt; last&lt;/EM&gt; observation in SQL. You would need an observation number to solve this problem in SQL.&lt;/P&gt;</description>
      <pubDate>Sun, 12 Mar 2017 06:35:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Third-highest-salary-as-per-Department/m-p/340222#M77735</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2017-03-12T06:35:41Z</dc:date>
    </item>
    <item>
      <title>Re: Third highest salary as per Department</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Third-highest-salary-as-per-Department/m-p/340226#M77737</link>
      <description>&lt;PRE&gt;
Here is for Q1.

DATA SALARY1;
INPUT EmpName $ Salary Department $;
DATALINES;
Steve 150 Finance
James 120 IT
Andrew 120 Finance
Mark 100 Back
Adam 110 IT
Niti 140 IT
Aman 110 HR
Lewis 130 Finance
Smith 125 Back
;
RUN;
proc sql;
select *,(select count(distinct salary) from salary1 
where department=a.department and salary ge a.salary) as n
 from salary1 as a
  where calculated n=3;
quit;



If you want Q2, you need make an index variable like n+1, or you only want salary's max or min ?

&lt;/PRE&gt;</description>
      <pubDate>Sun, 12 Mar 2017 10:31:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Third-highest-salary-as-per-Department/m-p/340226#M77737</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2017-03-12T10:31:05Z</dc:date>
    </item>
    <item>
      <title>Re: Third highest salary as per Department</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Third-highest-salary-as-per-Department/m-p/340248#M77743</link>
      <description>&lt;P&gt;Hi Ksharp&lt;/P&gt;&lt;P&gt;Thank's for the help can you please describe the logic a bit it will be helpful&lt;/P&gt;</description>
      <pubDate>Sun, 12 Mar 2017 18:21:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Third-highest-salary-as-per-Department/m-p/340248#M77743</guid>
      <dc:creator>Nitish1003</dc:creator>
      <dc:date>2017-03-12T18:21:21Z</dc:date>
    </item>
    <item>
      <title>Re: Third highest salary as per Department</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Third-highest-salary-as-per-Department/m-p/340249#M77744</link>
      <description>&lt;P&gt;Thanks for the help PG Stats&lt;/P&gt;</description>
      <pubDate>Sun, 12 Mar 2017 18:22:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Third-highest-salary-as-per-Department/m-p/340249#M77744</guid>
      <dc:creator>Nitish1003</dc:creator>
      <dc:date>2017-03-12T18:22:37Z</dc:date>
    </item>
    <item>
      <title>Re: Third highest salary as per Department</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Third-highest-salary-as-per-Department/m-p/340264#M77748</link>
      <description>&lt;P&gt;If you want to explore all the ways SAS can skew a cat, the best way to solve this&amp;nbsp;question imho is to use proc summary and then a data a step to implement Q1 or Q2 or any other scenario. Compact and (somewhat) legible syntax, and no prior sorting required.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc summary nway;
  class DEPARTMENT;
  var SALARY;
  output out=STATS
         max(SALARY)=SALARY 
         idgroup(max(SALARY)out[3](EMPNAME)=EMPNAME);           
run; &lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 12 Mar 2017 21:20:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Third-highest-salary-as-per-Department/m-p/340264#M77748</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2017-03-12T21:20:18Z</dc:date>
    </item>
    <item>
      <title>Re: Third highest salary as per Department</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Third-highest-salary-as-per-Department/m-p/340292#M77752</link>
      <description>&lt;PRE&gt;
Get each obs 's rank within each department .
In other words, the max has rank(n) 1,the second has rank 2 ........
and pick up the obs has the rank 3 (the third largest) .

&lt;/PRE&gt;</description>
      <pubDate>Mon, 13 Mar 2017 02:41:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Third-highest-salary-as-per-Department/m-p/340292#M77752</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2017-03-13T02:41:19Z</dc:date>
    </item>
    <item>
      <title>Re: Third highest salary as per Department</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Third-highest-salary-as-per-Department/m-p/340300#M77755</link>
      <description>&lt;P&gt;Another version of&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/18408"&gt;@Ksharp&lt;/a&gt;'s idea, maybe simpler because it doesn't involve a correlated subquery:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA Salary2;
INPUT EmpName $ Salary Department $;
DATALINES;
Steve 150 Finance
James 120 IT
Andrew 120 Finance
Mark 100 Back
Adam 110 IT
Niti 140 IT
Aman 110 HR
Lewis 130 Finance
Smith 125 Back
PG 140 IT   &amp;lt;-- tied with Niti
;

proc sql;
/* Break ties */
create table S as 
select *, salary + 0.0001*rand("uniform") as s 
from Salary2;

create table thirdSalary as
select a.EmpName, a.Salary, a.Department
from
    S as a left join
    S as b on a.Department=b.Department
group by a.EmpName, a.Salary, a.Department
having sum(b.s&amp;gt;a.s) = 2;

drop tabel S;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 13 Mar 2017 03:40:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Third-highest-salary-as-per-Department/m-p/340300#M77755</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2017-03-13T03:40:33Z</dc:date>
    </item>
    <item>
      <title>Re: Third highest salary as per Department</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Third-highest-salary-as-per-Department/m-p/340308#M77759</link>
      <description>&lt;P&gt;Hi KSharp&lt;/P&gt;&lt;P&gt;I already did it with help of Proc rank, but I wanted to understand the logic of your first solution. If possible can you please explain me the logic running behind the nested Query.&lt;/P&gt;&lt;P&gt;Thanks in advance.&lt;/P&gt;</description>
      <pubDate>Mon, 13 Mar 2017 05:22:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Third-highest-salary-as-per-Department/m-p/340308#M77759</guid>
      <dc:creator>Nitish1003</dc:creator>
      <dc:date>2017-03-13T05:22:39Z</dc:date>
    </item>
    <item>
      <title>Re: Third highest salary as per Department</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Third-highest-salary-as-per-Department/m-p/340315#M77760</link>
      <description>&lt;P&gt;Thanks every one for the help. I have got my answer and i have also gone through the details of using subquery.&lt;/P&gt;</description>
      <pubDate>Mon, 13 Mar 2017 07:42:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Third-highest-salary-as-per-Department/m-p/340315#M77760</guid>
      <dc:creator>Nitish1003</dc:creator>
      <dc:date>2017-03-13T07:42:55Z</dc:date>
    </item>
    <item>
      <title>Re: Third highest salary as per Department</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Third-highest-salary-as-per-Department/m-p/482173#M124845</link>
      <description>&lt;P&gt;Works perfectly and it is actually a very good Logic took time to understand&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 28 Jul 2018 12:53:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Third-highest-salary-as-per-Department/m-p/482173#M124845</guid>
      <dc:creator>Raj03</dc:creator>
      <dc:date>2018-07-28T12:53:10Z</dc:date>
    </item>
  </channel>
</rss>

