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

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

 

1 ACCEPTED SOLUTION

Accepted Solutions
KachiM
Rhodochrosite | Level 12

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

3 REPLIES 3
KachiM
Rhodochrosite | Level 12

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.

Reeza
Super User

What if you have ties?

 

Data A:

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

Ksharp
Super User
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;



sas-innovate-2024.png

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.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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