## How do I identify a fieldname from its value?

Solved
Occasional Contributor
Posts: 6

# How do I identify a fieldname from its value?

Guys, I am unsure how to proceed at this point.  So I have thousands of rows of data with a hundred different fields.  The field values are all integer values.  I would like to identify which field contains the highest value in each row.

I can use the LARGEST() function to identify the 3 highest values in each row, but what I really need is the names of those 3 fields (added as 3 extra fields).  Any suggestions?

Accepted Solutions
Solution
‎06-28-2011 05:15 PM
Super User
Posts: 23,683

## How do I identify a fieldname from its value?

What happens if you have multiples? Which variable would you take? or would you take 3 of the same?

Use an array of sorts with the whichn and vname function.

data have;

set sashelp.pricedata;

array pr{*} price1--price17;

firstvar=vname(pr[whichn(largest(1, of pr{*}), of pr{*})]);

run;

All Replies
Solution
‎06-28-2011 05:15 PM
Super User
Posts: 23,683

## How do I identify a fieldname from its value?

What happens if you have multiples? Which variable would you take? or would you take 3 of the same?

Use an array of sorts with the whichn and vname function.

data have;

set sashelp.pricedata;

array pr{*} price1--price17;

firstvar=vname(pr[whichn(largest(1, of pr{*}), of pr{*})]);

run;

Occasional Contributor
Posts: 6

## How do I identify a fieldname from its value?

Thanks Reeza -- VNAME was what I was looking for.  So I have the above working, the issue now is that some of the rows have all zero values in the fields, and the output in these cases is the first fieldname repeated all 3 times.  I would like simply not to display any fieldname if values are 0.  But how to do this with the above code?

Super User
Posts: 10,766

## How do I identify a fieldname from its value?

Then using array will give you more control.

Since not seeing your data ,it is hard to code exactly.

Ksharp

🔒 This topic is solved and locked.