BookmarkSubscribeRSS Feed
Tom
Super User Tom
Super User

There appears to be some interaction between defining the macro and parsing the %DO statement.  Because the %SYSFUNC() call works fine outside of a macro. 

 

And it works fine inside the macro if used in a simple %LET statement instead.  

So you could add a separate macro variable to store the upper bound for the %DO loop.

%macro split_terms;
%local terms n i ;
%let terms = %str(This is a term; This is another term, with a comma; And this is a third term);
%let n=%sysfunc(countw(&terms,%str(;)));
%do i = 1 %to &n;
  %put &=i %scan(&terms, &i, %str(;));
%end;
%mend;

Or you could store the delimiter into a macro variable which has the advantage of needed to set the delimiter in only one place.

%macro split_terms;
%local terms dlm i ;
%let terms = %str(This is a term; This is another term, with a comma; And this is a third term);
%let dlm=%str(;);
%do i = 1 %to %sysfunc(countw(&terms,&dlm));
  %put &=i %scan(&terms,&i,&dlm);
%end;
%mend;

Note that adding single quotes or double quotes to the list of delimiter characters will only work properly if the string being scanned/parsed does not include that character.  If it does then using both quote and semicolon as the delimiter will produce different results than using only semicolon.

 

 

Saxi63
SAS Employee

Dear SAS Macro Guru's

This is a known issue at our R&D.
It is unclear when this will be fixed.
Until then, you need to use a new Macro-Variable for the %to value.
regards
Karl-Heinz

 


%macro split_terms;
%let terms = %str(This is a term; This is another term, with a comma; And this is a third term) ;
%let NUMBER=%sysfunc(countw(&terms, %str(%;))) ;
%do i = 1 %to &Number;
%put %scan(&terms, &i, %str(%;));
%end;
%mend;
%split_terms;
yabwon
Amethyst | Level 16

"It is unclear when this will be fixed." -  question is: is there anyone who still knows how to fix it for macro processor 😉

 

Bart

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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
  • 17 replies
  • 4878 views
  • 16 likes
  • 5 in conversation