I want to calculate the maximum value in the year for each code and industry
So my Data is:
year code industry maximum value
2016 001 A 800
2017 001 A 900
2018 001 A 1700
2016 002 A 1200
2017 002 A 1500
2018 002 A 1100
2016 003 B 600
2017 003 B 1400
2018 003 B 950
2016 004 B 780
2017 004 B 1200
2018 004 B 3000
And I want to have:
year code industry maximum value
2016 002 A 1200
2017 002 A 1500
2018 001 A 1700
2016 004 B 780
2017 003 B 1400
2018 004 B 3000
I tried:
Thanks in advance
You can find some #SAStips here in computing maximum and minimum values https://blogs.sas.com/content/iml/2014/12/01/max-and-min-rows-and-cols.html You can also find other postings like this one that can provide some extra help https://communities.sas.com/t5/General-SAS-Programming/Fastest-way-to-calculate-the-max-of-a-variabl... Let us know if you still need assistance after looking at these resources.
I would use PROC SQL, like this:
data have;
infile datalines;
input year code:$3. industry:$1. Value;
datalines;
2016 001 A 800
2017 001 A 900
2018 001 A 1700
2016 002 A 1200
2017 002 A 1500
2018 002 A 1100
2016 003 B 600
2017 003 B 1400
2018 003 B 950
2016 004 B 780
2017 004 B 1200
2018 004 B 3000
;
proc sql;
create table want as
select Year, Code, Industry, max(Value) as MaxValue
from have
group by Year, Code, Industry
;
quit;
Yields:
year | code | industry | MaxValue |
---|---|---|---|
2016 | 001 | A | 800 |
2016 | 002 | A | 1200 |
2016 | 003 | B | 600 |
2016 | 004 | B | 780 |
2017 | 001 | A | 900 |
2017 | 002 | A | 1500 |
2017 | 003 | B | 1400 |
2017 | 004 | B | 1200 |
2018 | 001 | A | 1700 |
2018 | 002 | A | 1100 |
2018 | 003 | B | 950 |
2018 | 004 | B | 3000 |
Hope this helps!
Use PROC SUMMARY
proc summary data=have nway;
class industry code year;
var maximum_value;
output out=want max=;
run;
*1;
proc sql ;
create table want as
select year,code,industry,max(maximum_value) as max
from have
group by industry,year
having max(maximum_value)=maximum_value;
quit;
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.
Find more tutorials on the SAS Users YouTube channel.