DATA Step, Macro, Functions and more

Creating a column based on max values of row

Accepted Solution Solved
Reply
Contributor
Posts: 57
Accepted Solution

Creating a column based on max values of row

Hi All,

 

I am stuck in a problem of creating a single column data (B) which depends on the max value in each row of original data (A). Data A is -

Data A:

v1    v2    v3    v4
1      2     3       4
2      3     4       1
6      7     2       4

 

Data B should take the max from each row of Data A and then create the corresponding column as value in Data B:

Col

v4

v3

v2

 

Is this possible to do?

 

KR

SK

 


Accepted Solutions
Solution
‎07-24-2016 03:08 AM
Super Contributor
Posts: 298

Re: Creating a column based on max values of row

Posted in reply to Siddharth123

Here is a way. 

data want;
   length Col $32;
   set have;
   array k[*] V1 - V4;
   Max = Max(of k[*]);
   Pos = whichN(Max, of k[*]);
   Col = vname(k[Pos]);
keep Col;
run; 

Hope this is an acceptable answer.

View solution in original post


All Replies
Solution
‎07-24-2016 03:08 AM
Super Contributor
Posts: 298

Re: Creating a column based on max values of row

Posted in reply to Siddharth123

Here is a way. 

data want;
   length Col $32;
   set have;
   array k[*] V1 - V4;
   Max = Max(of k[*]);
   Pos = whichN(Max, of k[*]);
   Col = vname(k[Pos]);
keep Col;
run; 

Hope this is an acceptable answer.

Super User
Posts: 19,851

Re: Creating a column based on max values of row

Posted in reply to Siddharth123

What if you have ties?

 

Data A:

v1    v2    v3    v4
1      2     3       4
2      3     4       1
6      7     4       4

Super User
Posts: 10,041

Re: Creating a column based on max values of row

Posted in reply to Siddharth123
It is IML thing:


data DataA;
input v1    v2    v3    v4;
cards;
1      2     3       4
2      3     4       1
6      7     2       4
;
run;
proc iml;
use DataA;
read all var _num_ into x[c=vnames];
close;
col=vnames[x[,<:>]];
create max from col;
append from col;
close;
quit;
data want;
 merge DataA Max;
run;



☑ This topic is solved.

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

Discussion stats
  • 3 replies
  • 321 views
  • 2 likes
  • 4 in conversation