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;

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

New Learning Events in April

 

Join us for two new fee-based courses: Administrative Healthcare Data and SAS via Live Web Monday-Thursday, April 24-27 from 1:00 to 4:30 PM ET each day. And Administrative Healthcare Data and SAS: Hands-On Programming Workshop via Live Web on Friday, April 28 from 9:00 AM to 5:00 PM ET.

LEARN MORE

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