BookmarkSubscribeRSS Feed
deleted_user
Not applicable
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!
6 REPLIES 6
prholland
Fluorite | Level 6
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
deleted_user
Not applicable
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?
prholland
Fluorite | Level 6
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
Olivier
Pyrite | Level 9
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 ;
deleted_user
Not applicable
Olivier!

Merci! thank you so much!
deleted_user
Not applicable
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;
  • 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
    • 944 views
    • 0 likes
    • 3 in conversation