Finding maximum value in a group using proc means in SAS? -
suppose have data in table
match_day name goals 1 higuain 4 1 messi 1 1 ozil 4 1 villa 3 1 xavi 4 2 benzema 4 2 messi 4 2 ronaldo 3 2 villa 4 2 xavi 4
now want find out player scored maximum goals in each match. tried using doing-
proc means data=b nway max; class match_day name; var goals; output out=c(drop=_type_ _freq_) max=goals; run;
but not work. correct way of doing this?
this isn't can in proc means. it's easier in sql or data step. direct solution:
proc sort data=b; match_day descending goals; *so highest goal number @ top; run; data c; set b; match_day; if first.match_day; *the first record per match_day; run;
that give record largest number of goals. if there tie, not more 1 record, instead arbitrarily first.
if want keep records number, can do:
data c; set b; retain keep; match_day descending goals; if first.match_day keep=1; *the first record per match_day, flag keep; if keep=1 output; *output records kept; if last.goals keep=0; *clear keep flag @ end of first goals set; drop keep; run;
Comments
Post a Comment