BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
venkatard
Calcite | Level 5

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

1 ACCEPTED SOLUTION

Accepted Solutions
art297
Opal | Level 21

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;

View solution in original post

4 REPLIES 4
esjackso
Quartz | Level 8

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.

venkatard
Calcite | Level 5

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

art297
Opal | Level 21

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;

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 2730 views
  • 2 likes
  • 3 in conversation