<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: How does %sysfunc(countw(... get resolved in a %do loop in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-does-sysfunc-countw-get-resolved-in-a-do-loop/m-p/896244#M354123</link>
    <description>&lt;P&gt;Here's an interesting one:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro try2();
%do i =1 %to %length(%str(abc;
  %put &amp;amp;=i ;
%end;
%mend;

%try2();&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;That macro happily compiles, so that semicolon is seen by the macro compiler and ends the %do statement.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And then when it executes, you do get an error because the %length function is missing the closing ) , but SAS adds it in, and the macro 'works':&lt;/P&gt;
&lt;PRE&gt;1    %macro try2();
2    %do i =1 %to %length(%str(abc;
3      %put &amp;amp;=i ;
4    %end;
5    %mend;
NOTE: The macro TRY2 completed compilation without errors.
      9 instructions 144 bytes.
6
7    %try2();
ERROR: Expected close parenthesis after macro function invocation not found.
NOTE: One or more missing close parentheses have been supplied for the %LENGTH function.
I=1
I=2
I=3
&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 28 Sep 2023 13:16:31 GMT</pubDate>
    <dc:creator>Quentin</dc:creator>
    <dc:date>2023-09-28T13:16:31Z</dc:date>
    <item>
      <title>How does %sysfunc(countw(... get resolved in a %do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-does-sysfunc-countw-get-resolved-in-a-do-loop/m-p/896208#M354106</link>
      <description>&lt;P&gt;Dear Sas Community,&lt;BR /&gt;&lt;BR /&gt;I'm having troubles understanding why this code, with %str(;) inside of a sysfunc call in a loop is not working:&lt;/P&gt;&lt;PRE&gt;%macro split_terms;
%let terms = %str(This is a term; This is another term, with a comma; And this is a third term);
%do i = 1 %to &lt;U&gt;%sysfunc(countw(&amp;amp;terms, %str(;)));&lt;/U&gt;
%put %scan(&amp;amp;terms, &amp;amp;i, %str(;));
%end;
%mend;
%split_terms;&lt;/PRE&gt;&lt;P&gt;&lt;BR /&gt;But when in sysfunc, I put quotes around semicolon and remove %str like below, it works correctly:&lt;/P&gt;&lt;PRE&gt;%macro split_terms;
%let terms = %str(This is a term; This is another term, with a comma; And this is a third term);
%do i = 1 %to &lt;U&gt;%sysfunc(countw(&amp;amp;terms, ';'));&lt;/U&gt;
%put %scan(&amp;amp;terms, &amp;amp;i, %str(;));
%end;
%mend;
%split_terms;&lt;/PRE&gt;&lt;P&gt;&lt;BR /&gt;The other way around it is to put the resolution of&amp;nbsp;%sysfunc(countw(&amp;amp;terms, %str(;))) into macro variable like:&lt;/P&gt;&lt;PRE&gt;%macro split_terms;
    %let terms = %str(This is a term; This is another term, with a comma; And this is a third term);
    &lt;U&gt;%let nterms = %sysfunc(countw(&amp;amp;terms, %str(;)));&lt;/U&gt;
    %do i = 1 %to &lt;U&gt;&amp;amp;nterms.;&lt;/U&gt;
        %put %scan(&amp;amp;terms, &amp;amp;i, %str(;));
    %end;
%mend;
%split_terms;&lt;/PRE&gt;&lt;P&gt;&lt;BR /&gt;In other statements, like %if, there seems to be no difference in behavior:&lt;/P&gt;&lt;PRE&gt;%let terms = %str(This is a term; This is another term, with a comma; And this is a third term);
%if %sysfunc(countw(&amp;amp;terms, %str(;))) eq %sysfunc(countw(&amp;amp;terms, ';')) %then %do;
    %put ---&amp;gt; EQUAL &amp;lt;---;
%END;&lt;/PRE&gt;&lt;P&gt;&lt;EM&gt;24 %put ---&amp;gt; EQUAL &amp;lt;---;&lt;BR /&gt;---&amp;gt; EQUAL &amp;lt;---&lt;BR /&gt;25 %END;&lt;/EM&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;Is there a difference of behavior when such %sysfunc(countw... statement is used in a loop?&lt;/P&gt;</description>
      <pubDate>Thu, 28 Sep 2023 08:19:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-does-sysfunc-countw-get-resolved-in-a-do-loop/m-p/896208#M354106</guid>
      <dc:creator>Lukkul</dc:creator>
      <dc:date>2023-09-28T08:19:17Z</dc:date>
    </item>
    <item>
      <title>Re: How does %sysfunc(countw(... get resolved in a %do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-does-sysfunc-countw-get-resolved-in-a-do-loop/m-p/896224#M354110</link>
      <description>&lt;P&gt;It's look like a but when handling the ";" character by the %sysfunc(countw(...))&amp;nbsp; combo in the "%to" part of the %do-loop.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;When you put the separator as a macrovariable:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro split_terms();
%let terms = %str(A; B,C; D);

%let sep=%str(;);

%do i = 1 %to %sysfunc(countw( &amp;amp;terms., &amp;amp;sep. ));
  %put &amp;amp;=i.;
  %put %scan(&amp;amp;terms, &amp;amp;i, %str(;) );
%end;
%mend;
%split_terms()&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;you will get what you want:&lt;/P&gt;
&lt;PRE&gt;[...]
10   %mend;
11   %split_terms()
I=1
A
I=2
B,C
I=3
D
&lt;/PRE&gt;
&lt;P&gt;Also when you ignore the the third argument it will run ok:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro iterm(lst);
 %let finish=%sysfunc(countw(&amp;amp;lst));
  %do i = 1 %to &amp;amp;finish;
   %put %scan(&amp;amp;lst,&amp;amp;i);
  %end;
%mend iterm;
%iterm(a; c; e)&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;log is:&lt;/P&gt;
&lt;PRE&gt;[...]
6    %mend iterm;
7    %iterm(a; c; e)
a
c
e
&lt;/PRE&gt;
&lt;P&gt;BTW.&amp;nbsp; that example is copy-pasted from the SAS doc, I only added ";" to the list.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I would report that to the SAS Tech support and ask for investigation.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Bart&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 28 Sep 2023 10:12:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-does-sysfunc-countw-get-resolved-in-a-do-loop/m-p/896224#M354110</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2023-09-28T10:12:22Z</dc:date>
    </item>
    <item>
      <title>Re: How does %sysfunc(countw(... get resolved in a %do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-does-sysfunc-countw-get-resolved-in-a-do-loop/m-p/896226#M354111</link>
      <description>&lt;P&gt;And it's not a matter of comma, since the "&amp;amp;sep." trick cuts the data ok.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And adding execution time quoting functions wont help too:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro split_terms();
%let terms = %str(A; B,C; D);

%do i = 1 %to %sysfunc(countw( %superq(terms), %str(;) ));
  %put &amp;amp;=i.;
  %put %scan(%superq(terms), &amp;amp;i, %str(;) );
%end;
%mend;
%split_terms()&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;log says:&lt;/P&gt;
&lt;PRE&gt;1    %macro split_terms();
2    %let terms = %str(A; B,C; D);
3
4    %do i = 1 %to %sysfunc(countw( %superq(terms), %str(;) ));
5      %put &amp;amp;=i.;
6      %put %scan(%superq(terms), &amp;amp;i, %str(;) );
7    %end;
8    %mend;
9    %split_terms()
ERROR: Expected close parenthesis after macro function invocation not found.
NOTE: One or more missing close parentheses have been supplied for the %COUNTW function.
ERROR: Expected close parenthesis after macro function invocation not found.
ERROR: %EVAL function has no expression to evaluate, or %IF statement has no condition.
ERROR: The %TO value of the %DO I loop is invalid.
ERROR: The macro SPLIT_TERMS will stop executing.
&lt;/PRE&gt;
&lt;P&gt;it looks like somewhere under the hood that semicolon is breaking something...&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Bart&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 28 Sep 2023 10:18:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-does-sysfunc-countw-get-resolved-in-a-do-loop/m-p/896226#M354111</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2023-09-28T10:18:25Z</dc:date>
    </item>
    <item>
      <title>Re: How does %sysfunc(countw(... get resolved in a %do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-does-sysfunc-countw-get-resolved-in-a-do-loop/m-p/896231#M354114</link>
      <description>&lt;P&gt;BTW the approach with quotes around semicolon is not good idea too. If by chance your text would contain commas they would be used as separators:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro split_terms();
%let terms = %str(A; B,'C',D; E);

%do i = 1 %to %sysfunc(countw( &amp;amp;terms., ';' ));
  %put &amp;amp;=i.;
  %put %scan(&amp;amp;terms., &amp;amp;i, ';' );
%end;
%mend;
%split_terms()&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Log is:&lt;/P&gt;
&lt;PRE&gt;[...]
8    %mend;
9    %split_terms()
I=1
A
I=2
B,
I=3
C
I=4
,D
I=5
E
&lt;/PRE&gt;
&lt;P&gt;Bart&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 28 Sep 2023 10:57:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-does-sysfunc-countw-get-resolved-in-a-do-loop/m-p/896231#M354114</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2023-09-28T10:57:21Z</dc:date>
    </item>
    <item>
      <title>Re: How does %sysfunc(countw(... get resolved in a %do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-does-sysfunc-countw-get-resolved-in-a-do-loop/m-p/896233#M354116</link>
      <description>&lt;P&gt;Thanks for your inputs Bart &lt;span class="lia-unicode-emoji" title=":heart_suit:"&gt;♥️&lt;/span&gt;&lt;BR /&gt;The approach with single quotes does the trick, but only if semicolon is quoted in sysfunc call, it still has to be wrapped in %str in scan function like:&lt;/P&gt;&lt;PRE&gt;%macro split_terms;
%let terms = %str(A; B,'C',D; E);
%do i = 1 %to %sysfunc(countw(&amp;amp;terms, ';'));
%put &lt;U&gt;%scan(&amp;amp;terms, &amp;amp;i, %str(;));&lt;/U&gt;
%end;
%mend;
%split_terms;&lt;/PRE&gt;&lt;P&gt;Then the output is&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;A&lt;BR /&gt;B,'C',D&lt;BR /&gt;E&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;Which is just bananas... &lt;span class="lia-unicode-emoji" title=":grinning_face_with_big_eyes:"&gt;😃&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 28 Sep 2023 12:03:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-does-sysfunc-countw-get-resolved-in-a-do-loop/m-p/896233#M354116</guid>
      <dc:creator>Lukkul</dc:creator>
      <dc:date>2023-09-28T12:03:51Z</dc:date>
    </item>
    <item>
      <title>Re: How does %sysfunc(countw(... get resolved in a %do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-does-sysfunc-countw-get-resolved-in-a-do-loop/m-p/896235#M354118</link>
      <description>&lt;P&gt;Yes, please report to tech support.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I thought this was about %SYSFUNC doing something weird.&amp;nbsp; But after testing, it happens even without %SYFUNC, so I think it's something happening in the %DO statement itself.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;When I submit:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro try();
%do i =1 %to %length(%str(abc;def));
  %put &amp;amp;=i ;
%end;
%mend;
%try();&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I get a macro execution-time error:&lt;/P&gt;
&lt;PRE&gt;718  %macro try();
719  %do i =1 %to %length(%str(abc;def));
720    %put &amp;amp;=i ;
721  %end;
722  %mend;
NOTE: The macro TRY completed compilation without errors.
      11 instructions 180 bytes.
723  %try();
ERROR: Expected close parenthesis after macro function invocation not found.
NOTE: One or more missing close parentheses have been supplied for the %LENGTH function.
NOTE: Line generated by the invoked macro "TRY".
1     def));
      ---
      180
MPRINT(TRY):   def));

ERROR 180-322: Statement is not valid or it is used out of proper order.

I=1
NOTE: Line generated by the invoked macro "TRY".
3    def));
     ---
     180
MPRINT(TRY):  def));

ERROR 180-322: Statement is not valid or it is used out of proper order.

I=2
NOTE: Line generated by the invoked macro "TRY".
5    def));
     ---
     180
MPRINT(TRY):  def));

ERROR 180-322: Statement is not valid or it is used out of proper order.

I=3
&lt;/PRE&gt;
&lt;P&gt;And it's interesting that the macro does actually execute 3 times.&amp;nbsp; So clearly that semicolon was unmasked somehow.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And if you take your original code and move the %sysfunc(countw()) before the %TO, you get an error during macro compilation, indicating that the semicolon was seen and broke compilation.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I think in both cases, it's&amp;nbsp; an error that happens during macro compilation.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro split_terms;
%do i = %sysfunc(countw(foo, %str(;))) %to 1 ; 
%end;
%mend;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;724  %macro split_terms;
725  %do i = %sysfunc(countw(foo, %str(;))) %to 1 ;
ERROR: Expected %TO not found in %DO statement.
ERROR: A dummy macro will be compiled.
ERROR: Improper use of macro reserved word TO.
726  %end;
727  %mend;
NOTE: The macro SPLIT_TERMS completed compilation with errors.
      0 instructions 0 bytes.
&lt;/PRE&gt;
&lt;P&gt;The %DO statement is doing stuff behind the scenes, like it has an implied %EVAL.&amp;nbsp; But I couldn't replicate the problem with an %IF statement which also has an implied eval.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 28 Sep 2023 12:30:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-does-sysfunc-countw-get-resolved-in-a-do-loop/m-p/896235#M354118</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2023-09-28T12:30:16Z</dc:date>
    </item>
    <item>
      <title>Re: How does %sysfunc(countw(... get resolved in a %do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-does-sysfunc-countw-get-resolved-in-a-do-loop/m-p/896239#M354121</link>
      <description>&lt;P&gt;Add:&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%put &amp;amp;=i.;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;inside the loop you will see it works wrong.&lt;/P&gt;
&lt;P&gt;Bart&lt;/P&gt;</description>
      <pubDate>Thu, 28 Sep 2023 12:42:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-does-sysfunc-countw-get-resolved-in-a-do-loop/m-p/896239#M354121</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2023-09-28T12:42:47Z</dc:date>
    </item>
    <item>
      <title>Re: How does %sysfunc(countw(... get resolved in a %do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-does-sysfunc-countw-get-resolved-in-a-do-loop/m-p/896243#M354122</link>
      <description>&lt;P&gt;Also the following "blows up":&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;resetline;
%macro test(a);
  %scan(%superq(a),1)
%mend;


%macro split_terms();
%do i = 1 %to %test(%str(123;456));
  %put &amp;amp;=i.;
%end;
%mend;
%split_terms()&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;but this (with an extra macrovariable) works like a charm:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;resetline;
%macro test(a);
  %scan(%superq(a),1)
%mend;


%macro split_terms();
%let x = %test(%str(123;456));
%do i = 1 %to &amp;amp;x.;
  %put &amp;amp;=i.;
%end;
%mend;
%split_terms()&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I agree with&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/19879"&gt;@Quentin&lt;/a&gt;&amp;nbsp;you should report it.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If SAS does not decide to fix that, they can at least update the definition in the documentation:&amp;nbsp;&lt;A href="https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/mcrolref/p0ri72c3ud2fdtn1qzs2q9vvdiwk.htm" target="_blank"&gt;https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/mcrolref/p0ri72c3ud2fdtn1qzs2q9vvdiwk.htm&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;because now it states:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;DIV id="p1h69tgzuz894vn1s61vq0ucjbo5" class="xisDoc-argDescriptionPair"&gt;
&lt;H4 class="xisDoc-argument"&gt;&lt;FONT color="#0000FF"&gt;&lt;EM&gt;start&lt;/EM&gt;&lt;/FONT&gt;&lt;/H4&gt;
&lt;DIV class="xisDoc-argumentDescription"&gt;
&lt;P class="xisDoc-paraSimpleFirst"&gt;&lt;FONT color="#0000FF"&gt;&lt;EM&gt;specifies an integer (or a macro expression that generates an integer) to use as the initial value of&amp;nbsp;macro-variable.&amp;nbsp;Start&amp;nbsp;and&amp;nbsp;stop&amp;nbsp;control the number of times the statements between the iterative %DO statement and the %END statement are processed. For the first iteration,&amp;nbsp;macro-variable&amp;nbsp;is equal to&amp;nbsp;start. As processing continues, the value of&amp;nbsp;macro-variable&amp;nbsp;changes by the value of&amp;nbsp;increment&amp;nbsp;until the value of&amp;nbsp;macro-variable&amp;nbsp;is outside the range of integers specified by&amp;nbsp;start&amp;nbsp;and&amp;nbsp;stop.&lt;/EM&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;DIV id="n11hq2iagatkwsn1uiq44wbxsm34" class="xisDoc-argDescriptionPair"&gt;
&lt;H4 class="xisDoc-argument"&gt;&lt;FONT color="#0000FF"&gt;&lt;EM&gt;%TO&amp;nbsp;stop&lt;/EM&gt;&lt;/FONT&gt;&lt;/H4&gt;
&lt;DIV class="xisDoc-argumentDescription"&gt;
&lt;P class="xisDoc-paraSimpleFirst"&gt;&lt;FONT color="#0000FF"&gt;&lt;EM&gt;specifies an integer (or a macro expression that generates an integer) to use as the final&amp;nbsp;macro-variable&amp;nbsp;iteration value. Iteration stops if the value of&amp;nbsp;macro-variable&amp;nbsp;exceeds this value.&lt;/EM&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Bart&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 28 Sep 2023 13:11:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-does-sysfunc-countw-get-resolved-in-a-do-loop/m-p/896243#M354122</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2023-09-28T13:11:19Z</dc:date>
    </item>
    <item>
      <title>Re: How does %sysfunc(countw(... get resolved in a %do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-does-sysfunc-countw-get-resolved-in-a-do-loop/m-p/896244#M354123</link>
      <description>&lt;P&gt;Here's an interesting one:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro try2();
%do i =1 %to %length(%str(abc;
  %put &amp;amp;=i ;
%end;
%mend;

%try2();&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;That macro happily compiles, so that semicolon is seen by the macro compiler and ends the %do statement.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And then when it executes, you do get an error because the %length function is missing the closing ) , but SAS adds it in, and the macro 'works':&lt;/P&gt;
&lt;PRE&gt;1    %macro try2();
2    %do i =1 %to %length(%str(abc;
3      %put &amp;amp;=i ;
4    %end;
5    %mend;
NOTE: The macro TRY2 completed compilation without errors.
      9 instructions 144 bytes.
6
7    %try2();
ERROR: Expected close parenthesis after macro function invocation not found.
NOTE: One or more missing close parentheses have been supplied for the %LENGTH function.
I=1
I=2
I=3
&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 28 Sep 2023 13:16:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-does-sysfunc-countw-get-resolved-in-a-do-loop/m-p/896244#M354123</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2023-09-28T13:16:31Z</dc:date>
    </item>
    <item>
      <title>Re: How does %sysfunc(countw(... get resolved in a %do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-does-sysfunc-countw-get-resolved-in-a-do-loop/m-p/896246#M354125</link>
      <description>&lt;P&gt;Reading again through all these examples (thanks&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/35763"&gt;@yabwon&lt;/a&gt; for adding a bunch!).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I think it's a problem that happens during macro compile time.&amp;nbsp; Where when the %DO statement compiles it is either unquoting the semicolon or, for some reason, %str(;) is never actually quoting the semicolon.&amp;nbsp; So the semicolon prematurely ends the %DO statement, and it compiles incorrectly.&amp;nbsp; Then the everything else, i.e. ))); or whatever, becomes just text.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But I can't think of an explanation.&lt;/P&gt;</description>
      <pubDate>Thu, 28 Sep 2023 13:46:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-does-sysfunc-countw-get-resolved-in-a-do-loop/m-p/896246#M354125</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2023-09-28T13:46:09Z</dc:date>
    </item>
    <item>
      <title>Re: How does %sysfunc(countw(... get resolved in a %do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-does-sysfunc-countw-get-resolved-in-a-do-loop/m-p/896247#M354126</link>
      <description>&lt;P&gt;I think compilation of macro without closing bracket is ok. The same way this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro test(a);
  %length(%str(&amp;amp;a.
%mend;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;compiles (aka stores macro code in a catalog), or this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro test(a);
  %noSuchMacro(()
%mend;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;because the process "only" stores the macro code. And both cases it is treated just as a "text to be generated during execution".&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And only in the execution phase they both blow up.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Bart&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 28 Sep 2023 13:34:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-does-sysfunc-countw-get-resolved-in-a-do-loop/m-p/896247#M354126</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2023-09-28T13:34:45Z</dc:date>
    </item>
    <item>
      <title>Re: How does %sysfunc(countw(... get resolved in a %do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-does-sysfunc-countw-get-resolved-in-a-do-loop/m-p/896249#M354128</link>
      <description>&lt;P&gt;But I think for the %DO statement it's different.&amp;nbsp; I think it really does compile during macro compilation time.&amp;nbsp; Differently than a %length statement "compiles".&amp;nbsp; I think. : )&lt;/P&gt;</description>
      <pubDate>Thu, 28 Sep 2023 13:43:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-does-sysfunc-countw-get-resolved-in-a-do-loop/m-p/896249#M354128</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2023-09-28T13:43:29Z</dc:date>
    </item>
    <item>
      <title>Re: How does %sysfunc(countw(... get resolved in a %do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-does-sysfunc-countw-get-resolved-in-a-do-loop/m-p/896255#M354131</link>
      <description>&lt;P&gt;Thank you Bart and Quentin for the discussion &lt;span class="lia-unicode-emoji" title=":smiling_face_with_smiling_eyes:"&gt;😊&lt;/span&gt;&lt;/P&gt;&lt;P&gt;Luckily, the bit with semicolon in a macro variable works correctly, so there is a workaround, but I'll be reporting the issue for sure&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 28 Sep 2023 14:14:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-does-sysfunc-countw-get-resolved-in-a-do-loop/m-p/896255#M354131</guid>
      <dc:creator>Lukkul</dc:creator>
      <dc:date>2023-09-28T14:14:53Z</dc:date>
    </item>
    <item>
      <title>Re: How does %sysfunc(countw(... get resolved in a %do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-does-sysfunc-countw-get-resolved-in-a-do-loop/m-p/896258#M354133</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/455236"&gt;@Lukkul&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Thank you Bart and Quentin for the discussion &lt;span class="lia-unicode-emoji" title=":smiling_face_with_smiling_eyes:"&gt;😊&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;Luckily, the bit with semicolon in a macro variable works correctly, so there is a workaround, but I'll be reporting the issue for sure&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Yes, &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/35763"&gt;@yabwon&lt;/a&gt;'s first example was I think not only workaround but also explanation.&amp;nbsp; The fact that:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%do i = 1 %to %sysfunc(countw( &amp;amp;terms., &amp;amp;sep. ));&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;works suggests it's a problem that happens at compile time.&amp;nbsp; Essentially, instead of using %str() to mask the semicolon that appears on the %DO statement, Bart avoids having a semicolon in the %DO statement.&amp;nbsp; So this %DO statement can correctly compile without being ended prematurely by the semicolon.&amp;nbsp; And it also executes correctly. : )&lt;/P&gt;</description>
      <pubDate>Thu, 28 Sep 2023 14:26:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-does-sysfunc-countw-get-resolved-in-a-do-loop/m-p/896258#M354133</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2023-09-28T14:26:19Z</dc:date>
    </item>
    <item>
      <title>Re: How does %sysfunc(countw(... get resolved in a %do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-does-sysfunc-countw-get-resolved-in-a-do-loop/m-p/896262#M354134</link>
      <description>&lt;P&gt;There appears to be some interaction between defining the macro and parsing the %DO statement.&amp;nbsp; Because the %SYSFUNC() call works fine outside of a macro.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And it works fine inside the macro if used in a simple %LET statement instead.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So you could add a separate macro variable to store the upper bound for the %DO loop.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%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(&amp;amp;terms,%str(;)));
%do i = 1 %to &amp;amp;n;
  %put &amp;amp;=i %scan(&amp;amp;terms, &amp;amp;i, %str(;));
%end;
%mend;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Or you could store the delimiter into a macro variable which has the advantage of needed to set the delimiter in only one place.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%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(&amp;amp;terms,&amp;amp;dlm));
  %put &amp;amp;=i %scan(&amp;amp;terms,&amp;amp;i,&amp;amp;dlm);
%end;
%mend;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;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.&amp;nbsp; If it does then using both quote and semicolon as the delimiter will produce different results than using only semicolon.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 28 Sep 2023 14:42:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-does-sysfunc-countw-get-resolved-in-a-do-loop/m-p/896262#M354134</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-09-28T14:42:18Z</dc:date>
    </item>
    <item>
      <title>Re: How does %sysfunc(countw(... get resolved in a %do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-does-sysfunc-countw-get-resolved-in-a-do-loop/m-p/898090#M354972</link>
      <description>&lt;P&gt;Dear SAS Macro Guru's&lt;BR /&gt;&lt;BR /&gt;This is a known issue at our R&amp;amp;D.&lt;BR /&gt;It is unclear when this will be fixed.&lt;BR /&gt;Until then, you need to use a new Macro-Variable for the %to value.&lt;BR /&gt;regards&lt;BR /&gt;Karl-Heinz&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
%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(&amp;amp;terms, %str(%;))) ;
%do i = 1 %to &amp;amp;Number;
%put %scan(&amp;amp;terms, &amp;amp;i, %str(%;));
%end;
%mend;
%split_terms;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 11 Oct 2023 09:15:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-does-sysfunc-countw-get-resolved-in-a-do-loop/m-p/898090#M354972</guid>
      <dc:creator>Saxi63</dc:creator>
      <dc:date>2023-10-11T09:15:31Z</dc:date>
    </item>
    <item>
      <title>Re: How does %sysfunc(countw(... get resolved in a %do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-does-sysfunc-countw-get-resolved-in-a-do-loop/m-p/898092#M354973</link>
      <description>&lt;P&gt;"&lt;SPAN&gt;It is unclear when this will be fixed.&lt;/SPAN&gt;" -&amp;nbsp; question is: is there anyone who still knows how to fix it for macro processor &lt;span class="lia-unicode-emoji" title=":winking_face:"&gt;😉&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Bart&lt;/P&gt;</description>
      <pubDate>Wed, 11 Oct 2023 09:23:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-does-sysfunc-countw-get-resolved-in-a-do-loop/m-p/898092#M354973</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2023-10-11T09:23:20Z</dc:date>
    </item>
    <item>
      <title>Re: How does %sysfunc(countw(... get resolved in a %do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-does-sysfunc-countw-get-resolved-in-a-do-loop/m-p/898202#M355007</link>
      <description>&lt;P&gt;This bug has existed since at least version 9.2&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;   8? %macro try;%do i=1 %to %quote(2;3);%put i=&amp;amp;i;%end;%mend;%try;

NOTE: Line generated by the invoked macro "TRY".
8     3);
      -
      180
ERROR 180-322: Statement is not valid or it is used out of proper order.

i=1
NOTE: Line generated by the invoked macro "TRY".
8     3);
      -
      180
ERROR 180-322: Statement is not valid or it is used out of proper order.

i=2

   9?
&lt;/PRE&gt;</description>
      <pubDate>Wed, 11 Oct 2023 18:42:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-does-sysfunc-countw-get-resolved-in-a-do-loop/m-p/898202#M355007</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-10-11T18:42:53Z</dc:date>
    </item>
  </channel>
</rss>

