BookmarkSubscribeRSS Feed
deleted_user
Not applicable
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!
1 REPLY 1
1162
Calcite | Level 5
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;

Catch up on SAS Innovate 2026

Dive into keynotes, announcements and breakthroughs on demand.

Explore Now →

Health and Life Sciences Learning

 

Need courses to help you with SAS Life Sciences Analytics Framework, SAS Health Cohort Builder, or other topics? Check out the Health and Life Sciences learning path for all of the offerings.

LEARN MORE

Discussion stats
  • 1 reply
  • 1351 views
  • 0 likes
  • 2 in conversation