<?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 Find min and max salary for each dept_id using proc sql and datastep in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Find-min-and-max-salary-for-each-dept-id-using-proc-sql-and/m-p/913756#M360126</link>
    <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data ds;
length emp_name $20;
input emp_name $ dep_id salary;
datalines;
Siva 1 30000
Ravi 2 40000
Prasad 1 50000
Arun 1  30000
Sai 2 20000
;
run;

proc sql;
create table _temp as 
select dep_id,max(salary) as max_sal ,min(salary) as min_sal

from ds
group by dep_id ; 

create table _temp as 
select a.* ,b.emp_name as emp_name_min,c.emp_name as emp_name_max
from _temp as a
left join ds as b
on a.min_sal=b.salary
left join  ds as  c
on a.max_sal=c.salary;
quit;

proc sort data=_temp nodupkey ;
by dep_id;
run;
proc print;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;How to get min and max salary for each dept_id using proc sql and datastep in the above code result I got only one minsalary from dep_id 1 but given data two min salary are same how to fix this&lt;/P&gt;</description>
    <pubDate>Wed, 31 Jan 2024 11:29:09 GMT</pubDate>
    <dc:creator>BrahmanandaRao</dc:creator>
    <dc:date>2024-01-31T11:29:09Z</dc:date>
    <item>
      <title>Find min and max salary for each dept_id using proc sql and datastep</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-min-and-max-salary-for-each-dept-id-using-proc-sql-and/m-p/913756#M360126</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data ds;
length emp_name $20;
input emp_name $ dep_id salary;
datalines;
Siva 1 30000
Ravi 2 40000
Prasad 1 50000
Arun 1  30000
Sai 2 20000
;
run;

proc sql;
create table _temp as 
select dep_id,max(salary) as max_sal ,min(salary) as min_sal

from ds
group by dep_id ; 

create table _temp as 
select a.* ,b.emp_name as emp_name_min,c.emp_name as emp_name_max
from _temp as a
left join ds as b
on a.min_sal=b.salary
left join  ds as  c
on a.max_sal=c.salary;
quit;

proc sort data=_temp nodupkey ;
by dep_id;
run;
proc print;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;How to get min and max salary for each dept_id using proc sql and datastep in the above code result I got only one minsalary from dep_id 1 but given data two min salary are same how to fix this&lt;/P&gt;</description>
      <pubDate>Wed, 31 Jan 2024 11:29:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-min-and-max-salary-for-each-dept-id-using-proc-sql-and/m-p/913756#M360126</guid>
      <dc:creator>BrahmanandaRao</dc:creator>
      <dc:date>2024-01-31T11:29:09Z</dc:date>
    </item>
    <item>
      <title>Re: Find min and max salary for each dept_id using proc sql and datastep</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-min-and-max-salary-for-each-dept-id-using-proc-sql-and/m-p/913757#M360127</link>
      <description>&lt;P&gt;The sort step with NODUPKEY is removing duplicates.&amp;nbsp; If you remove the NODUPKEY option, you will have two records for department 1 in your output.&lt;/P&gt;</description>
      <pubDate>Wed, 31 Jan 2024 11:35:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-min-and-max-salary-for-each-dept-id-using-proc-sql-and/m-p/913757#M360127</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2024-01-31T11:35:03Z</dc:date>
    </item>
    <item>
      <title>Re: Find min and max salary for each dept_id using proc sql and datastep</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-min-and-max-salary-for-each-dept-id-using-proc-sql-and/m-p/913758#M360128</link>
      <description>Nope even if not used nodupkey same result</description>
      <pubDate>Wed, 31 Jan 2024 11:38:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-min-and-max-salary-for-each-dept-id-using-proc-sql-and/m-p/913758#M360128</guid>
      <dc:creator>BrahmanandaRao</dc:creator>
      <dc:date>2024-01-31T11:38:47Z</dc:date>
    </item>
    <item>
      <title>Re: Find min and max salary for each dept_id using proc sql and datastep</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-min-and-max-salary-for-each-dept-id-using-proc-sql-and/m-p/913759#M360129</link>
      <description>Did you rerun the full code, starting with the step that creates the data?&lt;BR /&gt;</description>
      <pubDate>Wed, 31 Jan 2024 11:42:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-min-and-max-salary-for-each-dept-id-using-proc-sql-and/m-p/913759#M360129</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2024-01-31T11:42:06Z</dc:date>
    </item>
    <item>
      <title>Re: Find min and max salary for each dept_id using proc sql and datastep</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-min-and-max-salary-for-each-dept-id-using-proc-sql-and/m-p/913849#M360139</link>
      <description>&lt;P&gt;For testing purposes name each dataset differently (eg. temp1, temp2, final), then you will see the difference with and without nodupkey.&lt;/P&gt;</description>
      <pubDate>Wed, 31 Jan 2024 17:04:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-min-and-max-salary-for-each-dept-id-using-proc-sql-and/m-p/913849#M360139</guid>
      <dc:creator>A_Kh</dc:creator>
      <dc:date>2024-01-31T17:04:35Z</dc:date>
    </item>
    <item>
      <title>Re: Find min and max salary for each dept_id using proc sql and datastep</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-min-and-max-salary-for-each-dept-id-using-proc-sql-and/m-p/913877#M360149</link>
      <description>&lt;P&gt;Please explain in words what you are trying to do.&amp;nbsp; And explain what you think the steps are doing.&lt;/P&gt;
&lt;P&gt;Especially that last step that is trying to "join" by matching&amp;nbsp; SALARY to MAX_SAL.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;NOTE that PROC SQL will happily re-merge the aggregate statistics for you automatically.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data ds;
  input emp_name :$20. dep_id salary;
datalines;
Siva 1 30000
Ravi 2 40000
Prasad 1 50000
Arun 1  30000
Sai 2 20000
;

proc sql;
create table ds_sal_range as
  select *
    , max(salary) as dept_max_salary
    , min(salary) as dept_min_salary 
  from ds
  group by dep_id
  order by emp_name
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result&lt;/P&gt;
&lt;PRE&gt;                                       dept_max_    dept_min_
Obs    emp_name    dep_id    salary      salary       salary

 1      Arun          1       30000      50000        30000
 2      Prasad        1       50000      50000        30000
 3      Ravi          2       40000      40000        20000
 4      Sai           2       20000      40000        20000
 5      Siva          1       30000      50000        30000
&lt;/PRE&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;</description>
      <pubDate>Wed, 31 Jan 2024 19:32:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-min-and-max-salary-for-each-dept-id-using-proc-sql-and/m-p/913877#M360149</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2024-01-31T19:32:44Z</dc:date>
    </item>
  </channel>
</rss>

