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~
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;
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;
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;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.