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