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

Hi SAS Experts,

 

I just have a question on specifying series for do loops. Instead of the usual %do n=1 %to..., I would like to specify a series such that it will activate a certain set of numbers (in this case 1, 7,8,10...). But it does not work. Any ways out? 🙂

 

Thanks.

 

Best,
David

 

%let n_MSA=48;

%macro combineMSA(MSA);
%do n=1,7,8,10,11,17,19,23,27,28,29,35,36,38,40,44,46;

data data.diction_MSA&n.; set data.diction;
if MSA ne &n. then delete;
run;

%end;
%mend;
%combineMSA;
1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

Do this in your macro:

%local list i n;
%let list=1 7 8 10 11 17 19 23 27 28 29 35 36 38 40 44 46;
%do i = 1 %to %sysfunc(countw(&list.));
  %let n = %scan(&list.,&i.);
  /* your loop body */
%end;

View solution in original post

3 REPLIES 3
Kurt_Bremser
Super User

Do this in your macro:

%local list i n;
%let list=1 7 8 10 11 17 19 23 27 28 29 35 36 38 40 44 46;
%do i = 1 %to %sysfunc(countw(&list.));
  %let n = %scan(&list.,&i.);
  /* your loop body */
%end;
Jagadishkatam
Amethyst | Level 16

Please try the call execute alternate to macro do loop

 

data have;
do n=1,7,8,10,11,17,19,23,27,28,29,35,36,38,40,44,46;
output;
end;
run;


data _null_;
call execute("data data.diction_MSA"||strip(put(n,best.))||"; set data.diction;if MSA ne "||strip(put(n,best.))||" then delete;run;");
run;
Thanks,
Jag
Reeza
Super User


The Macro Appendix has examples of common macro usage, including one with a macro loop.

https://communities.sas.com/t5/SAS-Communities-Library/SAS-9-4-Macro-Language-Reference-Has-a-New-Ap...

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
  • 850 views
  • 4 likes
  • 4 in conversation