Help using Base SAS procedures

How do I identify a fieldname from its value?

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 6
Accepted Solution

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: 17,737

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;

View solution in original post


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

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: 9,662

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.

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

Discussion stats
  • 3 replies
  • 132 views
  • 2 likes
  • 3 in conversation