You need to use a by
statement for SAS to create first.
and last.
variables. But they do NOT take on values of the by variable, instead they are just boolean flags to indicate if you are on the first (or the last) observation of this particular value of the BY variable.
If you just want to find the minimum and maximum and your values are already sorted then it is pretty simple.
data want ;
set scores;
by player score ;
if first.player then lowscore=score ;
retain lowscore ;
if last.player ;
highscore=score ;
keep player lowscore highscore ;
run;
Note that you need to RETAIN the lowscore variable so that the value set on the first observation is not cleared when the data step moves to the next observation. You can avoid the need for RETAIN by using a DO loop around the SET statement.
data want ;
do until (last.player);
set scores;
by player score ;
if first.player then lowscore=score ;
highscore=score ;
end;
keep player lowscore highscore ;
run;
If the data is sorted by player, but not by player and score then you will need to add more logic to find the min and max.
data want ;
do until (last.player);
set scores;
by player ;
lowscore=min(lowscore,score);
highscore=max(highscore,score);
end;
keep player lowscore highscore ;
run;
If you want to keep all of the original observations also then add another DO loop to re-read the data and output the detail rows.
data want ;
do until (last.player);
set scores;
by player ;
lowscore=min(lowscore,score);
highscore=max(highscore,score);
end;
do until (last.player);
set scores;
by player ;
output;
end;
run;