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 using a loop in datastep to load files together and analyze.
Is there any other simpler and faster way ??

%macro test();

%let C_k_09_3 = 3;
%let C_k_09_4 = 6;
%let C_k_09_5 = 6;

%let C_k_09_3_H = %sysevalf(&C_k_09_3 + 2);	%let C_k_09_3_L = %sysevalf(&C_k_09_3 - 2);
%let C_k_09_4_H = %sysevalf(&C_k_09_4 + 2);	%let C_k_09_4_L = %sysevalf(&C_k_09_4 - 2);
%let C_k_09_5_H = %sysevalf(&C_k_09_5 + 2);	%let C_k_09_5_L = %sysevalf(&C_k_09_5 - 2);

data test;
set  
	%do i = &C_k_09_3_L %to  &C_k_09_3_H ;
		%do j = &C_k_09_4_L %to  &C_k_09_4_H ;
			%do k = &C_k_09_5_L %to  &C_k_09_5_H;

				test__&i&j&k


			%end;
		%end;
	%end;
;

run;

%mend test;

%test;
best regards
thank you in advance for your help
1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

So you have three input parameters, each specifying the center point of a loop that runs for 5 elements.

I would build the list in a data step.

Why?

  • no extra macro is needed
  • no %eval to do the calculations for the loop
  • code is easier to read and think through
%let l1=3;
%let l2=6;
%let l3=6;

data _null_;
length setlist $32767;
do i = &l1. - 2 to &l1. + 2;
  do j = &l2. - 2 to &l2. + 2;
    do k = &l3. - 2 to &l3. + 2;
      setlist = catx(" ",setlist,cats("test__",i,j,k));
    end;
  end;
end;
call symputx('setlist',setlist);
run;

data test;
set &setlist.;
run;

View solution in original post

3 REPLIES 3
ErikLund_Jensen
Rhodochrosite | Level 12

Hi @makset 

 

There is nothing wrong with the way you use the macro to generate the set items. I cant' se that could be done simpler or more effecient.

 

Your calculation is very complicated. As there are no decimal values here, you could use %eval instead of %sysevalf and get the same. But it might be easier to specify the sets of low and high values for each position:

%macro test();

%let ilow = 1; %let ihigh = 5;
%let jlow = 4; %let jhigh = 8;
%let klow = 4; %let khigh = 8;

data test;
set  
	%do i = &ilow %to &ihigh;
		%do j = &jlow %to &jhigh;
			%do k = &klow %to &khigh;
				test__&i&j&k;
			%end;
		%end;
	%end;
;
run;
%mend test;
%test;

 

 

 

Kurt_Bremser
Super User

So you have three input parameters, each specifying the center point of a loop that runs for 5 elements.

I would build the list in a data step.

Why?

  • no extra macro is needed
  • no %eval to do the calculations for the loop
  • code is easier to read and think through
%let l1=3;
%let l2=6;
%let l3=6;

data _null_;
length setlist $32767;
do i = &l1. - 2 to &l1. + 2;
  do j = &l2. - 2 to &l2. + 2;
    do k = &l3. - 2 to &l3. + 2;
      setlist = catx(" ",setlist,cats("test__",i,j,k));
    end;
  end;
end;
call symputx('setlist',setlist);
run;

data test;
set &setlist.;
run;
RichardDeVen
Barite | Level 11

If I, J, or K exceed 9 you will have the possibility of the SET list have the same data set listed twice. This is because concatenation &I&J&K has no delimiters between the resolved values, likely due to naming convention of the TEMP__ tables.

Example:
TEMP__1111 would be listed three times in SET if the I,J,K loops were 1 to 11
- 11, 1, 1
- 1, 11, 1
- 1, 1, 11

sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


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
  • 3 replies
  • 864 views
  • 1 like
  • 4 in conversation