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

For each observation, I want to find the variable that contains the maximum value

here is the File structure

i/p

Obs        CompanyID              Year       Col1       Col2       col3       col4       col5

1             AAAA                    1991           2             3             6             7             2

2             BBBB                    1992           6             9             3             2             0

Intended o/p

Obs        CompanyID         Year       Col1       Col2       col3       col4       col5                Max

1             AAAA                    1991      2             3             6             7             2             col4

2             BBBB                    1992      6             9             3             2             0             col2

I used the following code but it just creates column names in the o/p file.

proc iml;

use FileA;

read all var _NUM_ INTO X [colname=VarNames];

close FileA;

idxMax = X[,<:>]; /* find columns for max of each row */

varMax = varNames[idxMax]; /* corresponding var names */

print idxMax varNames;

run;

Any thoughts? Is there a simple way in SQL?

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

Using a data step works easily enough, not sure why you went into IML, unless you really need to, but then SQL wouldn't be an option either.

data want;

set have;

array col(5) col1-col5;

max_value=max(of col(*));

var_max=vname(col(whichn(max_value, of col(*))));

run;

View solution in original post

7 REPLIES 7
Reeza
Super User

Using a data step works easily enough, not sure why you went into IML, unless you really need to, but then SQL wouldn't be an option either.

data want;

set have;

array col(5) col1-col5;

max_value=max(of col(*));

var_max=vname(col(whichn(max_value, of col(*))));

run;

jmdecu
Fluorite | Level 6

Hi Reeza,

How does this solution handle ties?  In other words, if two variables contain the maximum value for the row, will the names of both variables be flagged?  Thanks!

Reeza
Super User

It doesn't.  If you want to handle ties you should probably use a SQL approach.

stat_sas
Ammonite | Level 13

If you need sql then you can try this.

proc transpose data=have(keep=obs col:) out=want;
by obs;
run;

proc sql;
select a.*,b._name_ from have a
inner join (
select obs, _name_  from want
group by obs
having col1=max(col1)) b
on a.obs=b.obs;
quit;

vinayemmadi
Calcite | Level 5

use arrays and do loop to check min value

buckeyefisher
Obsidian | Level 7

Reeza,

simple SAS logic worked. thanks. stat@sas  - i didn't try SQL logic but will keep it for future reference.

Thanks

buckeyefisher
Obsidian | Level 7

jmdecu,

good catch..I am also exploring of a fix...

thanks

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 7 replies
  • 3586 views
  • 3 likes
  • 5 in conversation