BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
ConsAth
Fluorite | Level 6

 

Hi all,

 

What i am trying to accomplish is the following , i have an array with 4 variables containing values lest say 1, 2 ,5, 6 etc.

 

I am able to get the max value of the array and have the column name returned but I am stuck on how I can do the same procedure for the next largest value, any ideas?

 

Kind regards and thank you in advance

 

This is my script up till now

 

DATA WANT;
SET HAVE;

ARRAY COL(*) col1 --col4;

INDEX_OF_MAX=WHICHN(MAX(OF COL(*)),OF COL(*));
VALUE_1=VNAME(COL(INDEX_OF_MAX));
RUN;

1 ACCEPTED SOLUTION

Accepted Solutions
data_null__
Jade | Level 19
data want;
   array col(5) (1 3 5 10 4);
   index_of_2nd=whichn(largest(2,of col(*)),of col(*));
   value_1=vname(col(index_of_2nd));
   run;

View solution in original post

6 REPLIES 6
data_null__
Jade | Level 19

Use function LARGEST

 

ConsAth
Fluorite | Level 6
Thank you for your input but it doesnt cover my query on how to get the column name of that second largest value, thanks again
data_null__
Jade | Level 19

You know how to do that as demonstrated in the code you posted.  You just need to know how to find the second largest value.

ConsAth
Fluorite | Level 6
Yes, but i have been stuck in using the same method for second max value, i know i could use largest function but that would give me only which are the largest values not the column name they come from , for example here in this part of script INDEX_OF_MAX=WHICHN(MAX(OF COL(*)),OF COL(*)); i am trying to do the same for lets say MAX -1 and then use the Vname to get it, dont know have been workind around on this too much time and maybe i am missing the obvious
data_null__
Jade | Level 19
data want;
   array col(5) (1 3 5 10 4);
   index_of_2nd=whichn(largest(2,of col(*)),of col(*));
   value_1=vname(col(index_of_2nd));
   run;
ConsAth
Fluorite | Level 6
Have to say you opened my eyes, i was thinking whichn and largest functions separetely and not combined, thank you very much

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 6 replies
  • 3649 views
  • 4 likes
  • 2 in conversation