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

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