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

I am looking for a solution to the problem.

 

I have a number, let's say it 234534, and I would like to have this set of numbers in between  123423 and 345645.

234534 - 111111 = 123423

234534 + 111111 = 345645

but not in such a simple way:

 

data test;

%do i = 1 %to 3;
%do j = 2 %to 4;
%do k = 3 %to 5;
%do l = 4 %to 6;
%do m = 2 %to 4;
%do n = 3 %to 5;
i = &i;
j = &j;
k = &k;
l = &l;
m = &m;
n = &n;
output;
%end;
%end;
%end;
%end;
%end;
%end;
run;

 

because for me, numbers closer to the original are better or sorted like this:

234534 or 234534
334534   134534
244534   224534
235534   233534
and so far   and so far
134534   334534
224534   244534
233534   235534
and so far   and so far
344534   124534
and so far   and so far
144534   324534
and so far   and so far
345534   123534
and so far   and so far

Thank you for your help

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

Assuming I understand what you are aiming for, here's an approach:

 

data want;
original_i = 2;
original_j = 3;
original_k = 4;
original_l = 5;
original_m = 3;
original_n = 4;
do i = original_i - 1 to original_i + 1;
do j = original_j - 1 to original_j + 1;
do k = original_k - 1 to original_k + 1;
do l = original_l - 1 to original_l + 1;
do m = original_m - 1 to original_m + 1;
do n = original_n - 1 to original_n + 1;
   distance = abs(i - original_i) + abs(j - original_j) + abs(k - original_k) +
      abs(l - original_l) + abs(m - original_m) + abs(n - original_n);
   output;
end; end; end; end; end; end;
run;

Then you can sort the data set by DISTANCE.

 

Even if I didn't get the problem exactly right, this ought to be close enough that you can adjust it as needed.

View solution in original post

3 REPLIES 3
andreas_lds
Jade | Level 19

I don't understand what you are trying to achieve. Why are you using macro-loops? I don't think they are necessary. In general: start with working, macro-free code and use macro-statements only to make the code dynamic.

Astounding
PROC Star

Assuming I understand what you are aiming for, here's an approach:

 

data want;
original_i = 2;
original_j = 3;
original_k = 4;
original_l = 5;
original_m = 3;
original_n = 4;
do i = original_i - 1 to original_i + 1;
do j = original_j - 1 to original_j + 1;
do k = original_k - 1 to original_k + 1;
do l = original_l - 1 to original_l + 1;
do m = original_m - 1 to original_m + 1;
do n = original_n - 1 to original_n + 1;
   distance = abs(i - original_i) + abs(j - original_j) + abs(k - original_k) +
      abs(l - original_l) + abs(m - original_m) + abs(n - original_n);
   output;
end; end; end; end; end; end;
run;

Then you can sort the data set by DISTANCE.

 

Even if I didn't get the problem exactly right, this ought to be close enough that you can adjust it as needed.

makset
Obsidian | Level 7
Quick and effective code, thank you

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 16. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 572 views
  • 0 likes
  • 3 in conversation