BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
hhchenfx
Barite | Level 11

Hi,

My data has ID, date, Value. For each Id, at a given date, I want to find the Max value up to that date.

My code below works but I wonder if there is any shorter/faster way, such as using proc expand.

Thanks,

HHC

data have;
input id date value;
datalines;
1 1 2
1 2 15
1 3 6
1 4 17
1 5 8
1 6 9
2 1 60
2 2 50
2 3 600
;run;
proc sort data=have; by id descending date; run;

data want;
set have nobs=nobs;
drop i j _: exit;
i+1;
max_value=0;
exit=0;
do j=i to nobs until (exit=1);
	set have (keep=id date value rename=(id=_id date = _date value=_value)) point=j;
	if id^=_id then do;exit=1;leave; end;
	if max_value<_value then max_value=_value;

end;
run;

 

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

@hhchenfx wrote:

 

My data has ID, date, Value. For each Id, at a given date, I want to find the Max value up to that date.

My code below works but I wonder if there is any shorter/faster way, such as using proc expand.


What an odd question. The first thing you should do is look at the documentation for PROC EXPAND.

 

However, this much simpler DATA step will work:

 

data want;
    set have;
    by id;
    retain maxvalue;
    if first.id then maxvalue=value;
    else if value>maxvalue then maxvalue=value;
run;

 

--
Paige Miller

View solution in original post

1 REPLY 1
PaigeMiller
Diamond | Level 26

@hhchenfx wrote:

 

My data has ID, date, Value. For each Id, at a given date, I want to find the Max value up to that date.

My code below works but I wonder if there is any shorter/faster way, such as using proc expand.


What an odd question. The first thing you should do is look at the documentation for PROC EXPAND.

 

However, this much simpler DATA step will work:

 

data want;
    set have;
    by id;
    retain maxvalue;
    if first.id then maxvalue=value;
    else if value>maxvalue then maxvalue=value;
run;

 

--
Paige Miller

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!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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