Not applicable
Posts: 0

# maximum value

Hi,

Here's my problem: I have 10 variables (columns) and I would like to create an other variable which would consist of the highest value from the preceding 10 variables. The thing is, I don't actually want the value, but the variable's name corresponding to that maximum value. Could you help me?

Many thanks!
Frequent Contributor
Posts: 81

## Re: maximum value

A suggestion:
1. Find the maximum value of all 10 variables using MAX() function.
2. Create an array of all 10 variables.
3. Compare the maximum value to each variable in the array in a DO..END loop, and note which variable has the same value as the maximum.

A question:
If 2 or more variables have the same maximum value, do you need to know all their names?

.......Phil
Not applicable
Posts: 0

## Re: maximum value

thanks!

To your question: no, I am deleting those data (if 2 have the same and the largest value)

another question: is there a command to find the 2nd largest value?
Frequent Contributor
Posts: 81

## Re: maximum value

I'm not aware of a "2nd largest value" function.

As a workaround you could repeat my previous code, but, knowing which variable held the maximum value, exclude that one from the array and find the maximum of the remaining 9. Alternatively assign a very small, or large negative, value to the "maximum" variable, then repeat the code asis.

.......Phil
Super Contributor
Posts: 260

## Re: maximum value

Hi,

there is a LARGEST(k, variable_list) beginning with SAS9 ; k equals 1 if you want the maximum, k=2 will give you the second greatest value, and so on.

For your problem of recording the variables names according to their order, is the following example of any help ?

DATA work.test ;
INPUT x1-x5 ;
CARDS ;
45 454 484 21 2
2121 5156 5645 451 5451
5151 2121 21 313 26
55 64 65 1 231
;
RUN ;
DATA work.whos_who (DROP = nth: j) ;
SET work.test ;
ARRAY myXs _NUMERIC_ ;
ARRAY theirNames \$ name1-name5 ;
DO nth = 1 TO DIM(myXs) ;
nth_biggest = LARGEST(nth, OF x1-x5) ;
DO j = 1 TO DIM(myXs) ;
IF nth_biggest = myXs(j) THEN theirNames(nth) = VNAME(myXs(j)) ;
END ;
END ;
RUN ;
Not applicable
Posts: 0

## Re: maximum value

Olivier!

Merci! thank you so much!
Not applicable
Posts: 0

## Re: maximum value

Hi,

Why not putting your variables in an array, sort the array and automatically you will have an easy access to the highest value, second highest, lowest, etc ...
There is an experimental array sorting function in SAS 9, try the example below.

Good luck,
Otto.

data t;
infile cards;
input var bar nar kar lar ; * just some variable names ;
cards;
1 8 2 77 13
5 3 1 4 6
;
run;

data tt;
set t;
array v
• var bar nar kar lar ;
call sortn (of v
• );
run;
• Discussion stats
• 6 replies
• 232 views
• 0 likes
• 3 in conversation