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

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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