<?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 Do While Macro in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Do-While-Macro/m-p/721264#M223530</link>
    <description>&lt;P&gt;I am attempting to write a do while loop that will iterate over a list of values within a macro and am getting a strange result. I have based the structure off of other examples but can't find anything related to this error. I start with this below:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql noprint;
	select distinct Year_Month format=date9. into : month_list separated by ' ' 
	from string_all
	where Year_Month &amp;gt;= '01JAN2021'd
;
quit;

%macro historical_calcs();
%local i next_month;
%do %while (%scan(&amp;amp;month_list),&amp;amp;i) ne );
	%let next_month=%scan(&amp;amp;month_list, &amp;amp;i);

	proc sql;
	create table strings as
		select distinct string into : string_list separated by ' '
		from strings_all
		where Year_Month=&amp;amp;next_month
			  And
			  string is not missing
	;
	quit;

	%let i = %eval(&amp;amp;i + 1);
%end;
%mend;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I get an error saying "Expected semicolon not found after WHILE clause" and "A dummy macro will be compiled". What I am I missing about the structure of my do while part?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Edit: So I included the rest of the code and I've also included the log statement below.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;GOPTIONS ACCESSIBLE;
26         
27         %macro historical_calcs();
28         %local i next_month;
29         %do %while (%scan(&amp;amp;month_list),&amp;amp;i) ne );
ERROR: Expected semicolon not found after WHILE clause.
ERROR: A dummy macro will be compiled.
30         	%let next_month=%scan(&amp;amp;month_list, &amp;amp;i);
31         
32         	proc sql;
33         	create table strings as
34         		select distinct strings into : string_list separated by ' '
35         		from strings_all
36         		where Year_Month=&amp;amp;next_month
37         			  And
38         			  string is not missing
39         	;
40         	quit;
41         
42         	%let i = %eval(&amp;amp;i + 1);
43         %end;
44         %mend;
45         &lt;/PRE&gt;</description>
    <pubDate>Tue, 23 Feb 2021 14:20:36 GMT</pubDate>
    <dc:creator>A_SAS_Man</dc:creator>
    <dc:date>2021-02-23T14:20:36Z</dc:date>
    <item>
      <title>Do While Macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-While-Macro/m-p/721264#M223530</link>
      <description>&lt;P&gt;I am attempting to write a do while loop that will iterate over a list of values within a macro and am getting a strange result. I have based the structure off of other examples but can't find anything related to this error. I start with this below:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql noprint;
	select distinct Year_Month format=date9. into : month_list separated by ' ' 
	from string_all
	where Year_Month &amp;gt;= '01JAN2021'd
;
quit;

%macro historical_calcs();
%local i next_month;
%do %while (%scan(&amp;amp;month_list),&amp;amp;i) ne );
	%let next_month=%scan(&amp;amp;month_list, &amp;amp;i);

	proc sql;
	create table strings as
		select distinct string into : string_list separated by ' '
		from strings_all
		where Year_Month=&amp;amp;next_month
			  And
			  string is not missing
	;
	quit;

	%let i = %eval(&amp;amp;i + 1);
%end;
%mend;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I get an error saying "Expected semicolon not found after WHILE clause" and "A dummy macro will be compiled". What I am I missing about the structure of my do while part?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Edit: So I included the rest of the code and I've also included the log statement below.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;GOPTIONS ACCESSIBLE;
26         
27         %macro historical_calcs();
28         %local i next_month;
29         %do %while (%scan(&amp;amp;month_list),&amp;amp;i) ne );
ERROR: Expected semicolon not found after WHILE clause.
ERROR: A dummy macro will be compiled.
30         	%let next_month=%scan(&amp;amp;month_list, &amp;amp;i);
31         
32         	proc sql;
33         	create table strings as
34         		select distinct strings into : string_list separated by ' '
35         		from strings_all
36         		where Year_Month=&amp;amp;next_month
37         			  And
38         			  string is not missing
39         	;
40         	quit;
41         
42         	%let i = %eval(&amp;amp;i + 1);
43         %end;
44         %mend;
45         &lt;/PRE&gt;</description>
      <pubDate>Tue, 23 Feb 2021 14:20:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-While-Macro/m-p/721264#M223530</guid>
      <dc:creator>A_SAS_Man</dc:creator>
      <dc:date>2021-02-23T14:20:36Z</dc:date>
    </item>
    <item>
      <title>Re: Do While Macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-While-Macro/m-p/721273#M223536</link>
      <description>&lt;P&gt;Your parentheses are wrong. It should be&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%do %while (%scan(&amp;amp;month_list,&amp;amp;i) ne );&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 23 Feb 2021 14:41:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-While-Macro/m-p/721273#M223536</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2021-02-23T14:41:47Z</dc:date>
    </item>
    <item>
      <title>Re: Do While Macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-While-Macro/m-p/721274#M223537</link>
      <description>&lt;P&gt;Fix the first error.&lt;/P&gt;
&lt;PRE&gt;27         %macro historical_calcs();
28         %local i next_month;
29         %do %while (%scan(&amp;amp;month_list),&amp;amp;i) ne );
ERROR: Expected semicolon not found after WHILE clause.
ERROR: A dummy macro will be compiled.
30         	%let next_month=%scan(&amp;amp;month_list, &amp;amp;i);&lt;/PRE&gt;
&lt;P&gt;At line 29 you have unbalanced parentheses. you have two open parens and three closing parens.&lt;/P&gt;
&lt;P&gt;Also there is a logic error.&amp;nbsp; When line 29 runs there is no way that the macro variable I can have a value yet.&lt;/P&gt;
&lt;P&gt;Perhaps you need to set it to 1 before the %DO statement?&lt;/P&gt;</description>
      <pubDate>Tue, 23 Feb 2021 14:43:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-While-Macro/m-p/721274#M223537</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-02-23T14:43:36Z</dc:date>
    </item>
    <item>
      <title>Re: Do While Macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-While-Macro/m-p/721281#M223542</link>
      <description>&lt;P&gt;Why are you trying to use DO WHILE for simple iterative DO loop?&lt;/P&gt;
&lt;P&gt;Other issues:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;Use the automatic macro SQLOBS to capture the number of months found.&lt;/LI&gt;
&lt;LI&gt;Do not format the date values. Or if you do then make sure to reference the value appropriately when using the strings pulled from the macro variable with the list of values.&lt;/LI&gt;
&lt;LI&gt;Unless you are positive your STRING variable never contains spaces you should quote the values before generating a space delimited list of the values.&lt;/LI&gt;
&lt;/UL&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql noprint;
select distinct Year_Month into :month_list separated by ' ' 
  from string_all
  where Year_Month &amp;gt;= '01JAN2021'd
;
%let n_months=&amp;amp;sqlobs;
quit;

%macro historical_calcs();
%local i next_month;
%do i=1 %to &amp;amp;n_months;
  %let next_month=%scan(&amp;amp;month_list, &amp;amp;i);

proc sql;
  create table strings as
    select distinct quote(trim(string)) into :string_list separated by ' '
    from strings_all
    where Year_Month=&amp;amp;next_month
      and string is not missing
  ;
  %let n_strings = &amp;amp;sqlobs;
quit;
  %put &amp;amp;=i &amp;amp;=next_month &amp;amp;=n_strings &amp;amp;=string_list ;
%end;
%mend;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you did want to mimic an iterative DO loop like:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%do i=&amp;lt;start value&amp;gt; %to &amp;lt;end value&amp;gt; %by &amp;lt;step value&amp;gt;;
...
%end;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;with a DO WHILE loop then the pattern should be something like this.&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let i=&amp;lt;start value&amp;gt;;
%do %while (&amp;amp;i &amp;lt;= &amp;lt;end value&amp;gt;);
  ....
  %let i=%eval(&amp;amp;i + &amp;lt;step value&amp;gt;);
%end;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 23 Feb 2021 15:19:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-While-Macro/m-p/721281#M223542</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-02-23T15:19:45Z</dc:date>
    </item>
  </channel>
</rss>

