title: SAS: Do while loop not iterating
body: <p>I have a saved <code>csv_files</code> SAS Dataset with the following entries:</p>
  
  <pre><code>name path
  aapl F:\Data\aapl.csv
  msft F:\Data\msft.csv
  ibm F:\Data\ibm.csv
  goog F:\Data\goog.csv
  </code></pre>
  
  <p>I then use this SAS dataset to locate the csv files and import them into SAS using the following Macro which has a <code>while</code> loop:</p>
  
  <pre><code>options symbolgen mlogic nomprint;  
  /*Leave options on as SAS will issue a message when Do While Loop is false*/
  
  %let dsname = csv_files;                               
  %let libto = l;
  
  
  %Macro get_data(n,index);  
  
       /*n: is a counting macro variable representing the lower limit of the number of CSV files
   to be read into SAS. Index: is a variable that can be thought of as 
  observation number( row number) representing the upper limit 
  of the number  of CSV files to be read into SAS.*/
  
  %DO %WHILE (&amp;n &lt;=&amp;index);
  data _NULL_;
  set &amp;libto..&amp;dsname end=last;
  if _N_ =&amp;n then Do;
  call symput('path',path);      /*  Get file path from CSV_files*/
  call symput('dsn',name);       /* Get dataset name from CSV_files*/                     
  if not last  then index+1;     /* Create macro variable Index */
  else if last then call symput ('index',index);  
  End;
  run;
  
  proc import datafile= "&amp;path"             
       out= &amp;libto..&amp;dsn
       dbms=dlm
       replace;
       delimiter=","  ;            
       getnames=yes;
  run;
  %End;
  
  %Mend get_data;
  
  /* Macro Call*/
  %get_data(1,4);
  </code></pre>
  
  <p>When I execute this Macro I always iterate over the first observation (in this case AAPL). It simply imports the aapl.csv file over and over....what's wrong?</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>Following the suggestion of Dmitry Shopin I changed my <code>while</code> for a <code>do</code> loop:</p>
  
  <pre><code>%let dsname = csv_files;                                                   
  %let libto = l;
  
  %Macro get_data(n,index);       
  %let dsname = csv_files;                                                   
  %let libto = l;
  
  %do i=&amp;n %to &amp;index;
  data _NULL_;
  set &amp;libto..&amp;dsname end=last;
  if _N_ =&amp;i then Do;
  call symput('path',path);      /*  Get file path from CSV_files*/
  call symput('dsn',name);       /* Get dataset name from CSV_files*/                     
  if not last  then index+1;     /* Create macro variable Index */
  else if last then call symput ('index',index);  
  End;
  run;
  
  proc import datafile= "&amp;path"             
       out= &amp;libto..&amp;dsn
       dbms=dlm
       replace;
       delimiter=","  ;            
       getnames=yes;
  run;
  %End;
  %End;
  
  %Mend get_data;
  
  /* Macro Call*/
  %get_data(1,4);
  </code></pre>
  
  <p>The errors that I get is the following:</p>
  
  <pre><code>ERROR 180-322: Statement is not valid or it is used out of proper order.
  </code></pre>
  
  <p>I get this for multiple lines in the code but generates the last 3 out of 4 SAS datasets from my CSV files.</p>
  
  <p>I also get the error for the <code>&amp;dsn</code> in the code:</p>
  
  <pre><code>ERROR 22-322: Syntax error, expecting one of the following: ;, (, DATAFILE, DATATABLE, DBMS,
            DEBUG, FILE, OUT, REPLACE, TABLE, _DEBUG_.
  </code></pre>