<?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: Use data step to get the second highest salary in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Use-data-step-to-get-the-second-highest-salary/m-p/493935#M130071</link>
    <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/174522"&gt;@rajeshalwayswel&lt;/a&gt;&amp;nbsp; Your code is incorrect. Notice the below change in data, I request you to intuitively think why your logic is incorrect before you get help just to help solve&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data have;&lt;BR /&gt;input Id Salary ;&lt;BR /&gt;cards;&lt;BR /&gt;1 100&lt;BR /&gt;2 50&lt;BR /&gt;3 200 &lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;proc sort data=have ;&lt;BR /&gt;by id descending salary;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;data want;&lt;BR /&gt;set have;&lt;BR /&gt;if _n_=2;&lt;BR /&gt;run;&lt;/P&gt;</description>
    <pubDate>Sun, 09 Sep 2018 17:38:58 GMT</pubDate>
    <dc:creator>novinosrin</dc:creator>
    <dc:date>2018-09-09T17:38:58Z</dc:date>
    <item>
      <title>Use data step to get the second highest salary</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Use-data-step-to-get-the-second-highest-salary/m-p/493898#M130047</link>
      <description>&lt;P&gt;Use data step to get the second highest salary from the&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;CODE&gt;Employee&lt;/CODE&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;table.&lt;/P&gt;&lt;PRE&gt;+----+--------+
| Id | Salary |
+----+--------+
| 1  | 100    |
| 2  | 200    |
| 3  | 300    |
+----+--------+&lt;/PRE&gt;&lt;P&gt;For example, given the above Employee table, the query should return&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;CODE&gt;200&lt;/CODE&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;as the second highest salary. If there is no second highest salary, then the query should return&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;CODE&gt;null&lt;/CODE&gt;.&lt;/P&gt;&lt;PRE&gt;+---------------------+
| SecondHighestSalary |
+---------------------+
| 200                 |
+---------------------+&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 09 Sep 2018 14:06:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Use-data-step-to-get-the-second-highest-salary/m-p/493898#M130047</guid>
      <dc:creator>ccnky123</dc:creator>
      <dc:date>2018-09-09T14:06:18Z</dc:date>
    </item>
    <item>
      <title>Re: Use data step to get the second highest salary</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Use-data-step-to-get-the-second-highest-salary/m-p/493907#M130052</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data have;input Id  salary ;
cards;
 1  100 
2  200
3  100
;

proc sql;
select max(salary) from have;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 09 Sep 2018 14:35:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Use-data-step-to-get-the-second-highest-salary/m-p/493907#M130052</guid>
      <dc:creator>Jagadishkatam</dc:creator>
      <dc:date>2018-09-09T14:35:59Z</dc:date>
    </item>
    <item>
      <title>Re: Use data step to get the second highest salary</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Use-data-step-to-get-the-second-highest-salary/m-p/493908#M130053</link>
      <description>&lt;P&gt;In the following code I am:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;1. Creating two test tables, one with multiple rows, the other with one.&lt;/P&gt;
&lt;P&gt;2. Sorting both tables&lt;/P&gt;
&lt;P&gt;3. Using a data step to get the second highest salary and store it in a macro variable&lt;/P&gt;
&lt;P&gt;4. Not sure exactly what you want if there is only one row, but in this program it should create a data set with no rows and a note in the log. You can change it to whatever.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/*Create the table with multiple rows*/
data test;
	infile datalines;
	input ID $ Salary;
	datalines;
1 100
2 200
3 1111
4 500
5 700
6 1000
;

/*Create the table with only one row*/
data testoneobs;
	infile datalines;
	input ID $ Salary;
	datalines;
1 100
;

/*Sort by multiple rows table descending salary*/
proc sort data=test;
	by descending salary;
run;
/*Sort single row table descending salary*/
proc sort data=testoneobs;
	by descending salary;
run;


/*You can test this program with both tables from above*/
data secondhighest;
	set test end=lastobs;
	if lastobs and _n_=1 then putlog 'Only one salary';/*or whatever else you want*/
	else if _n_ =2 then do;
		call symputx('SecondSalary',salary);/*Store in macro variable, remove if you don't want*/
		output;
		stop;
	end;
run;
/*Check value of new macro variable*/
%put &amp;amp;=secondsalary;


/*Print report and use the macro variable in the title, or whatever else you need of it*/
title "Second highest salary is: &amp;amp;SecondSalary";
proc print data=secondhighest;
run;
title;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 09 Sep 2018 14:40:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Use-data-step-to-get-the-second-highest-salary/m-p/493908#M130053</guid>
      <dc:creator>Panagiotis</dc:creator>
      <dc:date>2018-09-09T14:40:09Z</dc:date>
    </item>
    <item>
      <title>Re: Use data step to get the second highest salary</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Use-data-step-to-get-the-second-highest-salary/m-p/493911#M130056</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input Id Salary ;
cards;
1 100
2 200
3 30 
run;

proc sort data=have ;
by id descending salary;
run;

data want;
set have;
if _n_=2;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 09 Sep 2018 15:08:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Use-data-step-to-get-the-second-highest-salary/m-p/493911#M130056</guid>
      <dc:creator>rajeshalwayswel</dc:creator>
      <dc:date>2018-09-09T15:08:38Z</dc:date>
    </item>
    <item>
      <title>Re: Use data step to get the second highest salary</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Use-data-step-to-get-the-second-highest-salary/m-p/493935#M130071</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/174522"&gt;@rajeshalwayswel&lt;/a&gt;&amp;nbsp; Your code is incorrect. Notice the below change in data, I request you to intuitively think why your logic is incorrect before you get help just to help solve&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data have;&lt;BR /&gt;input Id Salary ;&lt;BR /&gt;cards;&lt;BR /&gt;1 100&lt;BR /&gt;2 50&lt;BR /&gt;3 200 &lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;proc sort data=have ;&lt;BR /&gt;by id descending salary;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;data want;&lt;BR /&gt;set have;&lt;BR /&gt;if _n_=2;&lt;BR /&gt;run;&lt;/P&gt;</description>
      <pubDate>Sun, 09 Sep 2018 17:38:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Use-data-step-to-get-the-second-highest-salary/m-p/493935#M130071</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2018-09-09T17:38:58Z</dc:date>
    </item>
    <item>
      <title>Re: Use data step to get the second highest salary</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Use-data-step-to-get-the-second-highest-salary/m-p/493936#M130072</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data have;
input Id  salary ;
cards;
1  100 
2  200
3  300
;
proc rank data=have out=want(where=(ranksalary=2))  ties=low;

   var salary;
   ranks ranksalary;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 09 Sep 2018 17:43:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Use-data-step-to-get-the-second-highest-salary/m-p/493936#M130072</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2018-09-09T17:43:45Z</dc:date>
    </item>
    <item>
      <title>Re: Use data step to get the second highest salary</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Use-data-step-to-get-the-second-highest-salary/m-p/598469#M172587</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;input Id  salary ;
cards;
1  100 
2  200
3  300
4 200
;
run;


Then output should be
2 200
4 200

How to solve it&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 22 Oct 2019 17:50:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Use-data-step-to-get-the-second-highest-salary/m-p/598469#M172587</guid>
      <dc:creator>BrahmanandaRao</dc:creator>
      <dc:date>2019-10-22T17:50:59Z</dc:date>
    </item>
    <item>
      <title>Re: Use data step to get the second highest salary</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Use-data-step-to-get-the-second-highest-salary/m-p/598470#M172588</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/265860"&gt;@BrahmanandaRao&lt;/a&gt;&amp;nbsp; &amp;nbsp;It's the same code for your sample too. Test it plz&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input Id  salary ;
cards;
1  100 
2  200
3  300
4 200
;
run;

proc rank data=have out=want(where=(ranksalary=2))  ties=low;
   var salary;
   ranks ranksalary;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 22 Oct 2019 17:55:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Use-data-step-to-get-the-second-highest-salary/m-p/598470#M172588</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-10-22T17:55:58Z</dc:date>
    </item>
  </channel>
</rss>

