DATA Step, Macro, Functions and more

Find variable name of maximium values in case of tie using array

Accepted Solution Solved
Reply
Senior User
Posts: 1
Accepted Solution

Find variable name of maximium values in case of tie using array

Hello Everyone

I need to find varaible name of maximium value even if there is a tie using array.

Sample dataset

Data test;

input TT TS TM JAG TL;

DATALINES;

450 789 850 789 745

145 523 854 125 452

356 896 785 452  896

523 589 846 475 145

632 521 452 785 452

;

RUN;

now i want to create a variable which will store the variable name of maxmium value

for eg

output dataset

TT  TS  TM  JAG  TL      max

850 789 850 789 745    TT/TM

145 523 254 125 452     TS

356 896 896 452  896    TS/TM/TL

523 589 846 475 145      TM

785  521 452 785 452     TT/JAG

 

Basically i wantname of variable for all the maximium values.

 

Please use concept of ARRAY only

 

Thanks~


Accepted Solutions
Solution
‎05-26-2016 10:44 AM
Super User
Posts: 11,343

Re: Find variable name of maximium values in case of tie using array

Here is one way. NOTESmiley Tongue the length assignment of the names variable is very important. Make it too short and your logic may work but the results would be truncated.

Data test;
   input TT TS TM JAG TL;
   array v   TT TS TM JAG TL;
   /* if you want one variable to hold ALL of the potential names
   it should be n*32 + (n-1) to hold the names and separaters*/
   length names $ 164 name $ 32;
   m = max(of v(*));
   /* what to do if all values are missing*/
   if m ne . then do i= 1 to dim(v);
      if m = v[i] then do;
         name= vname(v[i]);
         names = catx('/',names,name);
      end; 
   end;
   drop i m name;

DATALINES;
450 789 850 789 745
145 523 854 125 452
356 896 785 452  896
523 589 846 475 145
632 521 452 785 452
1   1   1   1   1
.   .   .   .   .
;
RUN;

View solution in original post


All Replies
Solution
‎05-26-2016 10:44 AM
Super User
Posts: 11,343

Re: Find variable name of maximium values in case of tie using array

Here is one way. NOTESmiley Tongue the length assignment of the names variable is very important. Make it too short and your logic may work but the results would be truncated.

Data test;
   input TT TS TM JAG TL;
   array v   TT TS TM JAG TL;
   /* if you want one variable to hold ALL of the potential names
   it should be n*32 + (n-1) to hold the names and separaters*/
   length names $ 164 name $ 32;
   m = max(of v(*));
   /* what to do if all values are missing*/
   if m ne . then do i= 1 to dim(v);
      if m = v[i] then do;
         name= vname(v[i]);
         names = catx('/',names,name);
      end; 
   end;
   drop i m name;

DATALINES;
450 789 850 789 745
145 523 854 125 452
356 896 785 452  896
523 589 846 475 145
632 521 452 785 452
1   1   1   1   1
.   .   .   .   .
;
RUN;
Super User
Super User
Posts: 7,997

Re: Find variable name of maximium values in case of tie using array

A small update to use the do over shrinks the code a bit.  Doesn't of course answer the all missing question:

data test;
  length names $200;
  input TT TS TM JAG TL;
  array v tt--tl;
  do over v;
    names=catx('/',names,ifc(v=max(of v{*}),vname(v),""));
  end; 
datalines;
450 789 850 789 745
145 523 854 125 452
356 896 785 452  896
523 589 846 475 145
632 521 452 785 452
1   1   1   1   1
.   .   .   .   .
;
run;

Smiley Happy

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 2 replies
  • 273 views
  • 3 likes
  • 3 in conversation