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

I am trying to write some code that would go by group and row by row and determine if at each row if the current value is the lowest in the group. If so it would keep that value until it either reaches a lower value or gets to the next group. I have tried various retain and lags with if statements but I can't seem to come to the correct way of doing this. Any help is greatly appreciated.

 

An example of data is below where running_min is the outcome I am looking for:

groupamountrunning_min
a1010
a99
a99
a88
a77
a66
a55
a95
a105
b100100
b2020
b3520
b4520
b1010
b5010

 

Below is the test data I am working with

/* Code for test data */
data test;
input group $ amount;
cards;
a 10
a 9
a 9
a 8
a 7
a 6
a 5
a 9
a 10
b 100
b 20
b 35
b 45
b 10
b 50
;
run;
1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20

data have;
input group	$ amount;*	running_min;
cards;
a	10	10
a	9	9
a	9	9
a	8	8
a	7	7
a	6	6
a	5	5
a	9	5
a	10	5
b	100	100
b	20	20
b	35	20
b	45	20
b	10	10
b	50	10
;

data want;
 do until(last.group);
  set have;
  by group;
  running_min=min(running_min,amount);
  output;
 end;
run;

View solution in original post

5 REPLIES 5
novinosrin
Tourmaline | Level 20

data have;
input group	$ amount;*	running_min;
cards;
a	10	10
a	9	9
a	9	9
a	8	8
a	7	7
a	6	6
a	5	5
a	9	5
a	10	5
b	100	100
b	20	20
b	35	20
b	45	20
b	10	10
b	50	10
;

data want;
 do until(last.group);
  set have;
  by group;
  running_min=min(running_min,amount);
  output;
 end;
run;
lukesirakos
Fluorite | Level 6

Well that was certainly easier than I was making it and a very fast reply, thank you so much!

PeterClemmensen
Tourmaline | Level 20
data want;
   set test;
   by group;
   if first.group then running_min=.;
   running_min=min(running_min,amount);
   retain running_min;
run;
lukesirakos
Fluorite | Level 6
Ah this is brilliant and the perfect solution for what I wanted to do after this step was figured out. I can't believe how close I was and how simple it ended up being. Thank you so much!
RichardDeVen
Barite | Level 11

Try

data want;
  set have;
  by group;
  current_min = ifn ( first.group, amount, min(amount,current_min) );
  retain current_min;  
run;

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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
  • 5 replies
  • 2372 views
  • 4 likes
  • 4 in conversation