<?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: Do While Loop in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Do-While-Loop/m-p/68201#M14778</link>
    <description>It seems that you are mixing up data step statements and macro statements in multiple places.  Is this what you were trying do?&lt;BR /&gt;
&lt;BR /&gt;
[pre]&lt;BR /&gt;
let groupvalues=Lot_Id,Facility;  /* Removed quotation marks.  These would cause problems when ***/&lt;BR /&gt;
                                   /* you reference the macro variable as "&amp;amp;GroupValues" later.  ***/&lt;BR /&gt;
&lt;BR /&gt;
%let nrGroupValues = %sysfunc(countw(&amp;amp;groupvalues,)); /*** The second argument to the countw function ***/&lt;BR /&gt;
                                                      /*** was set to missing.  The single quote was ***/&lt;BR /&gt;
                                                      /*** treated as a delimiter in the previous code ***/&lt;BR /&gt;
%put &amp;amp;nrGroupValues;&lt;BR /&gt;
&lt;BR /&gt;
%macro test;&lt;BR /&gt;
&lt;BR /&gt;
%local CounterVariable GroupValue; /*** Initialize variables that are local to this macro ***/&lt;BR /&gt;
%if &amp;amp;groupvalues ne  %then %do;  /*** Removed unneeded quotation marks in this statement. ***/&lt;BR /&gt;
&lt;BR /&gt;
/*nrGroupValues = %sysfunc(countw(&amp;amp;groupvalues,','));*/ /*** Removed redundant assignment statement. The ***/&lt;BR /&gt;
                                                        /*** macro variable nrGroupValues has already ***/&lt;BR /&gt;
                                                        /*** been created.  This form of assignment statement ***/&lt;BR /&gt;
                                                        /*** is only valid inside a data step. ***/&lt;BR /&gt;
&lt;BR /&gt;
	%let CounterVariable=1; /*** Initialized macro variable CounterVariable. ***/&lt;BR /&gt;
	%do %while (%eval(&amp;amp;CounterVariable le &amp;amp;nrGroupValues));  /*** CounterVariable and nrGroupValues are macro ***/&lt;BR /&gt;
	                                                         /*** variables and are therefore referenced with the ***/&lt;BR /&gt;
	                                                         /*** preceding &amp;amp;. ***/&lt;BR /&gt;
&lt;BR /&gt;
	%let GroupValue = %qscan(%bquote(&amp;amp;groupvalues), &amp;amp;CounterVariable); /*** %let is used to create a macro variable. ***/&lt;BR /&gt;
	                                                                   /*** The macro function %qscan should be used ***/&lt;BR /&gt;
	                                                                   /*** instead of the datastep scan function. The ***/&lt;BR /&gt;
	                                                                   /*** %bquote function performs macro quoting so that ***/&lt;BR /&gt;
	                                                                   /*** the comma present in the GroupValues value is ***/&lt;BR /&gt;
	                                                                   /*** not interpreted as a delimiter to %qscan  ***/&lt;BR /&gt;
	                                                                   /*** function.  CounterVariable is a macro variable ***/&lt;BR /&gt;
	                                                                   /*** and is therefore referenced with the preceding &amp;amp; ***/&lt;BR /&gt;
     %put nrGroupValues = &amp;amp;nrGroupValues;&lt;BR /&gt;
	 %put CounterVariable = &amp;amp;CounterVariable;&lt;BR /&gt;
     %put GroupValue = &amp;amp;GroupValue;&lt;BR /&gt;
&lt;BR /&gt;
	%let CounterVariable = %eval(&amp;amp;CounterVariable + 1); /*** The %eval function is used to increment the macro variable ***/&lt;BR /&gt;
	                                                    /*** CounterVariable. ***/&lt;BR /&gt;
	%end;&lt;BR /&gt;
%end;&lt;BR /&gt;
%mend;&lt;BR /&gt;
%test;&lt;BR /&gt;
[/pre]</description>
    <pubDate>Wed, 02 Feb 2011 12:58:17 GMT</pubDate>
    <dc:creator>polingjw</dc:creator>
    <dc:date>2011-02-02T12:58:17Z</dc:date>
    <item>
      <title>Do While Loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-While-Loop/m-p/68200#M14777</link>
      <description>[pre]&lt;BR /&gt;
options symbolgen;&lt;BR /&gt;
%let groupvalues="Lot_Id,Facility";&lt;BR /&gt;
%let nrGroupValues = %sysfunc(countw(&amp;amp;groupvalues,' '));&lt;BR /&gt;
%put &amp;amp;nrGroupValues;&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
%macro test;&lt;BR /&gt;
%if "&amp;amp;groupvalues" ne ' ' %then %do;&lt;BR /&gt;
nrGroupValues = %sysfunc(countw(&amp;amp;groupvalues,','));&lt;BR /&gt;
	%do %while(CounterVariable le nrGroupValues); &lt;BR /&gt;
	GroupValue = scan(&amp;amp;groupvalues,CounterVariable,' ');&lt;BR /&gt;
     %put &amp;amp;nrGroupValues;&lt;BR /&gt;
	 %put CounterVariable;&lt;BR /&gt;
     %put GroupValue;&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
		CounterVariable = CounterVariable + 1;&lt;BR /&gt;
	%end;&lt;BR /&gt;
%end;&lt;BR /&gt;
%mend;&lt;BR /&gt;
%test;&lt;BR /&gt;
&lt;BR /&gt;
[/pre]&lt;BR /&gt;
what is wrong with this code?&lt;BR /&gt;
it crashes on multiple places

Message was edited by: Filipvdr</description>
      <pubDate>Wed, 02 Feb 2011 11:59:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-While-Loop/m-p/68200#M14777</guid>
      <dc:creator>Filipvdr</dc:creator>
      <dc:date>2011-02-02T11:59:08Z</dc:date>
    </item>
    <item>
      <title>Re: Do While Loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-While-Loop/m-p/68201#M14778</link>
      <description>It seems that you are mixing up data step statements and macro statements in multiple places.  Is this what you were trying do?&lt;BR /&gt;
&lt;BR /&gt;
[pre]&lt;BR /&gt;
let groupvalues=Lot_Id,Facility;  /* Removed quotation marks.  These would cause problems when ***/&lt;BR /&gt;
                                   /* you reference the macro variable as "&amp;amp;GroupValues" later.  ***/&lt;BR /&gt;
&lt;BR /&gt;
%let nrGroupValues = %sysfunc(countw(&amp;amp;groupvalues,)); /*** The second argument to the countw function ***/&lt;BR /&gt;
                                                      /*** was set to missing.  The single quote was ***/&lt;BR /&gt;
                                                      /*** treated as a delimiter in the previous code ***/&lt;BR /&gt;
%put &amp;amp;nrGroupValues;&lt;BR /&gt;
&lt;BR /&gt;
%macro test;&lt;BR /&gt;
&lt;BR /&gt;
%local CounterVariable GroupValue; /*** Initialize variables that are local to this macro ***/&lt;BR /&gt;
%if &amp;amp;groupvalues ne  %then %do;  /*** Removed unneeded quotation marks in this statement. ***/&lt;BR /&gt;
&lt;BR /&gt;
/*nrGroupValues = %sysfunc(countw(&amp;amp;groupvalues,','));*/ /*** Removed redundant assignment statement. The ***/&lt;BR /&gt;
                                                        /*** macro variable nrGroupValues has already ***/&lt;BR /&gt;
                                                        /*** been created.  This form of assignment statement ***/&lt;BR /&gt;
                                                        /*** is only valid inside a data step. ***/&lt;BR /&gt;
&lt;BR /&gt;
	%let CounterVariable=1; /*** Initialized macro variable CounterVariable. ***/&lt;BR /&gt;
	%do %while (%eval(&amp;amp;CounterVariable le &amp;amp;nrGroupValues));  /*** CounterVariable and nrGroupValues are macro ***/&lt;BR /&gt;
	                                                         /*** variables and are therefore referenced with the ***/&lt;BR /&gt;
	                                                         /*** preceding &amp;amp;. ***/&lt;BR /&gt;
&lt;BR /&gt;
	%let GroupValue = %qscan(%bquote(&amp;amp;groupvalues), &amp;amp;CounterVariable); /*** %let is used to create a macro variable. ***/&lt;BR /&gt;
	                                                                   /*** The macro function %qscan should be used ***/&lt;BR /&gt;
	                                                                   /*** instead of the datastep scan function. The ***/&lt;BR /&gt;
	                                                                   /*** %bquote function performs macro quoting so that ***/&lt;BR /&gt;
	                                                                   /*** the comma present in the GroupValues value is ***/&lt;BR /&gt;
	                                                                   /*** not interpreted as a delimiter to %qscan  ***/&lt;BR /&gt;
	                                                                   /*** function.  CounterVariable is a macro variable ***/&lt;BR /&gt;
	                                                                   /*** and is therefore referenced with the preceding &amp;amp; ***/&lt;BR /&gt;
     %put nrGroupValues = &amp;amp;nrGroupValues;&lt;BR /&gt;
	 %put CounterVariable = &amp;amp;CounterVariable;&lt;BR /&gt;
     %put GroupValue = &amp;amp;GroupValue;&lt;BR /&gt;
&lt;BR /&gt;
	%let CounterVariable = %eval(&amp;amp;CounterVariable + 1); /*** The %eval function is used to increment the macro variable ***/&lt;BR /&gt;
	                                                    /*** CounterVariable. ***/&lt;BR /&gt;
	%end;&lt;BR /&gt;
%end;&lt;BR /&gt;
%mend;&lt;BR /&gt;
%test;&lt;BR /&gt;
[/pre]</description>
      <pubDate>Wed, 02 Feb 2011 12:58:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-While-Loop/m-p/68201#M14778</guid>
      <dc:creator>polingjw</dc:creator>
      <dc:date>2011-02-02T12:58:17Z</dc:date>
    </item>
    <item>
      <title>Re: Do While Loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-While-Loop/m-p/68202#M14779</link>
      <description>&amp;gt; [pre]&lt;BR /&gt;
&amp;gt; options symbolgen;&lt;BR /&gt;
&amp;gt; %let groupvalues="Lot_Id,Facility";&lt;BR /&gt;
&amp;gt; %let nrGroupValues = %sysfunc(countw(&amp;amp;groupvalues,'&lt;BR /&gt;
&amp;gt; '));&lt;BR /&gt;
&amp;gt; %put &amp;amp;nrGroupValues;&lt;BR /&gt;
&amp;gt; &lt;BR /&gt;
&amp;gt; &lt;BR /&gt;
&amp;gt; %macro test;&lt;BR /&gt;
&amp;gt; %if "&amp;amp;groupvalues" ne ' ' %then %do;&lt;BR /&gt;
&amp;gt; nrGroupValues = %sysfunc(countw(&amp;amp;groupvalues,','));&lt;BR /&gt;
&amp;gt; 	%do %while(CounterVariable le nrGroupValues); &lt;BR /&gt;
&amp;gt; GroupValue = scan(&amp;amp;groupvalues,CounterVariable,'&lt;BR /&gt;
&amp;gt; ' ');&lt;BR /&gt;
&amp;gt;      %put &amp;amp;nrGroupValues;&lt;BR /&gt;
&amp;gt; t CounterVariable;&lt;BR /&gt;
&amp;gt;      %put GroupValue;&lt;BR /&gt;
&amp;gt; 	CounterVariable = CounterVariable + 1;&lt;BR /&gt;
&amp;gt; 	%end;&lt;BR /&gt;
&amp;gt; %end;&lt;BR /&gt;
&amp;gt; %mend;&lt;BR /&gt;
&amp;gt; %test;&lt;BR /&gt;
&amp;gt; &lt;BR /&gt;
&amp;gt; [/pre]&lt;BR /&gt;
&amp;gt; what is wrong with this code?&lt;BR /&gt;
&amp;gt; it crashes on multiple places&lt;BR /&gt;
&amp;gt; &lt;BR /&gt;
&amp;gt; Message was edited by: Filipvdr&lt;BR /&gt;
&lt;BR /&gt;
I think the problem is that with the Variable CounterVariable, nrGroupValues and GroupValue. The variables are being referenced without '&amp;amp;' and are being initialized without %let.&lt;BR /&gt;
&lt;BR /&gt;
It looks like you are using Datastep Variables and Functions (SCAN) within the macro that does not have a datastep at all.&lt;BR /&gt;
&lt;BR /&gt;
Try this:&lt;BR /&gt;
options nosymbolgen;&lt;BR /&gt;
%let groupvalues="Lot_Id,Facility";&lt;BR /&gt;
%let nrGroupValues = %sysfunc(countw(&amp;amp;groupvalues,' '));&lt;BR /&gt;
%put nrGroupValues_1: &amp;amp;nrGroupValues;&lt;BR /&gt;
&lt;BR /&gt;
%let CounterVariable=1;&lt;BR /&gt;
&lt;BR /&gt;
%macro test;&lt;BR /&gt;
	%if &amp;amp;groupvalues ne ' ' %then %do;&lt;BR /&gt;
		%let nrGroupValues = %sysfunc(countw(&amp;amp;groupvalues,','));&lt;BR /&gt;
		%do %while(&amp;amp;CounterVariable le &amp;amp;nrGroupValues); &lt;BR /&gt;
			%let GroupValue = %scan(&amp;amp;groupvalues,&amp;amp;CounterVariable,' ');&lt;BR /&gt;
	    		%put nrGroupValues_2 : &amp;amp;nrGroupValues;&lt;BR /&gt;
			%put CounterVariable: &amp;amp;CounterVariable;&lt;BR /&gt;
	    		%put GroupValue: &amp;amp;GroupValue;&lt;BR /&gt;
			%let CounterVariable = %eval(&amp;amp;CounterVariable + 1);&lt;BR /&gt;
		%end;&lt;BR /&gt;
	%end;&lt;BR /&gt;
%mend;&lt;BR /&gt;
&lt;BR /&gt;
%test;&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
Log:&lt;BR /&gt;
nrGroupValues_1: 1&lt;BR /&gt;
&lt;BR /&gt;
nrGroupValues_2 : 2&lt;BR /&gt;
CounterVariable: 1&lt;BR /&gt;
GroupValue: "Lot_Id,Facility"&lt;BR /&gt;
nrGroupValues_2 : 2&lt;BR /&gt;
CounterVariable: 2&lt;BR /&gt;
GroupValue:&lt;BR /&gt;
&lt;BR /&gt;
This runs without any error. But I did not understand what you are trying to achieve.&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
Message was edited by: bhavani&lt;BR /&gt;
&lt;BR /&gt;
Message was edited by: bhavani

Message was edited by: bhavani</description>
      <pubDate>Wed, 02 Feb 2011 13:04:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-While-Loop/m-p/68202#M14779</guid>
      <dc:creator>bhavani</dc:creator>
      <dc:date>2011-02-02T13:04:38Z</dc:date>
    </item>
  </channel>
</rss>

