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

I need find dynamic minimum value depend on _n_ , some like below,

yj111_0-1633984488858.png

 

Column K is the field I want, but the min range is changed by N. I did have some marco set up but it did not work since it did not take _N_ as numeric 


%LET T=_N_;
want =MIN(OF X(&T)-X(&T+3));

 

anyone can help?  many Appreciate here!

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
yj111
Fluorite | Level 6

It works perfectly!! 

 

Thank you!

View solution in original post

6 REPLIES 6
FreelanceReinh
Jade | Level 19

Hello @yj111 and welcome to the SAS Support Communities!

 

Do you have only six variables x1-x6 and six observations?

 

Then use an array, no macro variables needed:

data want;
set have;
array a[8] x1-x6 x6 x6;
if n(a[_n_],a[_n_+1],a[_n_+2]) then m=min(a[_n_],a[_n_+1],a[_n_+2]);
run;

If you don't have missing values, you can omit the IF condition.

yj111
Fluorite | Level 6

It works perfectly!! 

 

Thank you!

PaigeMiller
Diamond | Level 26

Hello @yj111 , you need to mark the reply from @FreelanceReinh as correct and not your reply that says it works perfectly as the correct answer.

--
Paige Miller
PaigeMiller
Diamond | Level 26

No macros needed, nor would they be a good idea here. This is a place where an ARRAY works perfectly.

 

data want;
    set have;
    array x x1-x6;
    if _n_>1 and _n_<6 then min_for_row=min(x(_n_+1),x(_n_));
    else if _n_=1 then min_for_row=min(of x1-x3);
    else if _n_=6 then min_for_row=x6;
run;
--
Paige Miller
yj111
Fluorite | Level 6
thank you Paige!
small changes I made from your suggestion:

data want;
set have;

array X X1-X6;
if _n_>=1 and _n_<5 then min_for_row=min(X(_n_+1),X(_n_),X(_n_+2));

else if _n_=5 then min_for_row=min(X(_N_-1),X(_N_));
ELSE IF _N_=6 THEN MIN_FOR_ROW=X(_N_);

run;
PaigeMiller
Diamond | Level 26

@yj111 wrote:
thank you Paige!


Repeating my earlier request: 

"Hello @yj111 , you need to mark the reply from @FreelanceReinhard as correct and not your reply that says it works perfectly as the correct answer."

--
Paige Miller

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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
  • 6 replies
  • 1542 views
  • 4 likes
  • 3 in conversation