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

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~

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

Here is one way. NOTE:P 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

2 REPLIES 2
ballardw
Super User

Here is one way. NOTE:P 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;
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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

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
  • 2 replies
  • 1392 views
  • 3 likes
  • 3 in conversation