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;
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.