Solved
Contributor
Posts: 63

# Can any one help me out in calculating moving(running) minimum and maximum in sas datastep

Can any one help me out in calculating moving(running) minimum and maximum in sas datastep

Accepted Solutions
Solution
‎03-04-2013 08:32 AM
PROC Star
Posts: 8,164

## Re: Can any one help me out in calculating moving(running) minimum and maximum in sas datastep

You could just use something like:

data have;

input user_id    week    score;

cards;

101          1          143

101          2          156

101          3          102

102          1           19

102          2           23

;

data want;

set have;

retain running_min running_max;

by user_id;

running_min=ifn(first.user_id,score,min(score,running_min));

running_max=ifn(first.user_id,score,max(score,running_max));

run;

All Replies
Super Contributor
Posts: 334

## Re: Can any one help me out in calculating moving(running) minimum and maximum in sas datastep

Can you provide more details about what you are trying to do and what you have tried? Without context it will be hard to help.

Contributor
Posts: 63

## Re: Can any one help me out in calculating moving(running) minimum and maximum in sas datastep

I am sorry.

i have a data set for example

user_id    week    score

101          1          143

101          2          156

101          3          102

102          1           19

102          2           23

i want running minimum and running maximum for scores by user_id.

please see the below table which gives running min and running max

user_id    week    score    running_ min    running_max

101          1          143      143                    143

101          2          156       143                    156

101          3          102       102                    156

102          1           19         19                      19

102          2           23          19                     23

Solution
‎03-04-2013 08:32 AM
PROC Star
Posts: 8,164

## Re: Can any one help me out in calculating moving(running) minimum and maximum in sas datastep

You could just use something like:

data have;

input user_id    week    score;

cards;

101          1          143

101          2          156

101          3          102

102          1           19

102          2           23

;

data want;

set have;

retain running_min running_max;

by user_id;

running_min=ifn(first.user_id,score,min(score,running_min));

running_max=ifn(first.user_id,score,max(score,running_max));

run;

Contributor
Posts: 63

## Re: Can any one help me out in calculating moving(running) minimum and maximum in sas datastep

Thanks a lot for the quick reply.

🔒 This topic is solved and locked.