Skip to content
Advertisement

how to pick the column with the highest value

I have a table with different possibilities of marital status (columns), how do I pick up the one with the highest value and keep the column name?

For example, below is my original data set:

data test;
infile datalines missover;
INPUT ID Gender $ pct_married pct_common_law pct_single; 
datalines;
1 male 0.5 0.3 0.2
2 female 0.6 0.4
3 male 0.3 0.3 0.4
;

and what i want to see is

data test2;
infile datalines missover;
INPUT ID Status $ pct_married pct_common_law pct_single; 
datalines;
1 pct_married
2 pct_married
3 pct_single
;

Advertisement

Answer

Make an array of the variables to search. Use the MAX() function to find the maximum value. Then using the WHICHN() function to find the index into the array where that value first occurs. Then use the VNAME() function to convert the array reference into the name of the variable.

data want ;
  set test;
  array pct pct_: ;
  name = vname(pct[whichn(max(of pct[*]),of pct[*])]);
run;
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement