BookmarkSubscribeRSS Feed
EmadNoureldeen
Calcite | Level 5

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

4 REPLIES 4
SAS_Cares
SAS Employee

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. 

SASJedi
Ammonite | Level 13

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:

Want
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!

Check out my Jedi SAS Tricks for SAS Users
PaigeMiller
Diamond | Level 26

Use PROC SUMMARY

 

proc summary data=have nway;
    class industry code year;
    var maximum_value;
    output out=want max=;
run;
--
Paige Miller
hhinohar
Quartz | Level 8
*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;