Selecting MAX VALUE by Visit Number - REVISED

Reply
N/A
Posts: 0

Selecting MAX VALUE by Visit Number - REVISED

I have the following data:

id score00 score02 score 04
1 2 4 1
2 4 3 7
...and so on.

I would like to get the maximum values for each score, going back to all the previous scores. It should look something like this:

id score00 score02 score04 maxscore00 maxscore02 maxscore04
1 2 4 1 2 4 4
2 4 3 7 4 4 7
...and so on.

What is the SAS code I need to get the table above?

Thank you very much!
Frequent Contributor
Posts: 95

Re: Selecting MAX VALUE by Visit Number - REVISED

A simple solution might look like this

data scores;
set scores;
retain maxscore00 maxscore02 maxscore04;
maxscore00 = max(score00, maxscore00);
maxscore02 = max(score02, maxscore02);
maxscore04 = max(score04, maxscore04);
run;

But if you have a lot more variables, you might be better off with this version. You would have to expand the retain and array lines with your additional variables, but you wouldn't have to write max functions over and over.

data scores;
set scores;
retain maxscore00 maxscore02 maxscore04;
array scorein (3) score00 score02 score04;
array scoremx (3) maxscore00 maxscore02 maxscore04;
do i = 1 to 3;
scoremx = max(scorein, scoremx);
end;
drop i;
run;
Ask a Question
Discussion stats
  • 1 reply
  • 278 views
  • 0 likes
  • 2 in conversation