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
Onyx | Level 15

"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



sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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.

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