Solved
Contributor
Posts: 25

# aggreagte function for each row going backwards in date

Hi all,

Can any one help me out with the following?

i want to get max(cnt) by date upto that particular date. For example i want my result dataset to be like below.

datalines;

date               cnt

23march2013 10

24march2013 25

14april2013 15

15april2013 30

18april2013 17

result dataset:

date max(cnt)

23march2013 10

24march2013 25

14april2013 25

15april2013 30

18april2013 30

RK

Accepted Solutions
Solution
‎11-12-2013 11:29 AM
Super User
Posts: 23,749

## Re: aggreagte function for each row going backwards in date

Look up the retain statement.

You want to retain the variable and if the CNT is larger then change it to the new maximum.

data want;

set have;

retain max_cnt 0;

if cnt>max_cnt then max_cnt=cnt;

run;

All Replies
Solution
‎11-12-2013 11:29 AM
Super User
Posts: 23,749

## Re: aggreagte function for each row going backwards in date

Look up the retain statement.

You want to retain the variable and if the CNT is larger then change it to the new maximum.

data want;

set have;

retain max_cnt 0;

if cnt>max_cnt then max_cnt=cnt;

run;

Super User
Posts: 5,881

## Re: aggreagte function for each row going backwards in date

Just retain a new variable max_cnt, and do max_cnt = max(max_cnt,cnt).

Data never sleeps
🔒 This topic is solved and locked.