title: SAS Macro to export to csv
body: <p>I'm trying to use code from here and not sure how to adapt it correctly.
  <a href="https://communities.sas.com/thread/33654?start=0&amp;tstart=0" rel="nofollow">https://communities.sas.com/thread/33654?start=0&amp;tstart=0</a></p>
  
  <p>I'm trying to export 51 SAS datasets to CSV files so that I can import them into R. Here's the macro code I have (that's not working):</p>
  
  <pre><code>libname g 'C:\mylibrary';
  %macro export(libname, numvars= &amp;numvars.);
    %do i= 1 %to &amp;numvars.;
      proc export data=&amp;libname.StateZip&amp;i
        DBMS=CSV REPLACE 
        outfile= "&amp;libname.StateZip&amp;i.csv"; 
        run;
    %end;
  %mend;
  
  %export(&amp;g, numvars= 51);
  </code></pre>
  
  <p>Here is the error message I'm getting:</p>
  
  <pre><code>NOTE: Writing TAGSETS.SASREPORT12(EGSR) Body file: EGSR
  13         
  14         GOPTIONS ACCESSIBLE;
  ERROR: Invalid macro name (.  It should be a valid SAS identifier no longer than 32 characters.
  ERROR: A dummy macro will be compiled.
  15         %macro export(libname, numvars= &amp;numvars.);
  16          %do i= 1 %to &amp;numvars.;
  17              proc export data=&amp;libname.StateZip&amp;i
  18                  DBMS=CSV REPLACE
  19               outfile= '&amp;libname.StateZip&amp;i.csv';
  20               run;
  21          %end;
  22         %mend;
  23         
  24         %export(g, numvars= 51);
  24        +(libname, numvars= &amp;numvars.);
             _
             10
  
  ERROR 10-205: Expecting the name of the procedure to be executed.
  
  24        +(libname, numvars= &amp;numvars.);
             _
             180
  ERROR 180-322: Statement is not valid or it is used out of proper order.
  2                                           The SAS System          11:38 Wednesday, September 5, 2012
  
  
  
  180: LINE and COLUMN cannot be determined.
  NOTE: NOSPOOL is on. Rerunning with OPTION SPOOL may allow recovery of the LINE and COLUMN where the 
        error has occurred.
  </code></pre>
  
  <p>Any help you can provide would be appreciated. </p>