<?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: Macro to export each file in a library to an Excel file in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-export-each-file-in-a-library-to-an-Excel-file/m-p/430588#M281612</link>
    <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro SASToExcel(ExportLibrary=);

%* obtain list of datasets in the library;
    ods output members = _Members;
    proc datasets lib = &amp;amp;ExportLibrary; run; quit;

    proc sql;
*get the name of datasets into the list using Dataset1, Dataset2 etc;
        select Name into :Dataset1-
from _Members;
    quit;
*store the number of records to control the loop;
%let NumOfDatasets=&amp;amp;sqlobs;

%*loop to export all your data sets;
    %do index = 1 %to &amp;amp;NumOfDatasets;

		proc export data=&amp;amp;ExportLibrary..&amp;amp;&amp;amp;Dataset&amp;amp;index.
        outfile="K:\MS_PROGS\cip_codes\&amp;amp;&amp;amp;dataset&amp;amp;index..xlsx"
        dbms=excel replace;
        run;
    %end;

%*delete dataset of list of data sets in the library;
    proc datasets;
        delete _Members;
    quit;

%mend;

%SASToExcel(ExportLibrary=work);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Try the following, simplifed&lt;/P&gt;</description>
    <pubDate>Wed, 24 Jan 2018 18:49:31 GMT</pubDate>
    <dc:creator>Reeza</dc:creator>
    <dc:date>2018-01-24T18:49:31Z</dc:date>
    <item>
      <title>Macro to export each file in a library to an Excel file</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-export-each-file-in-a-library-to-an-Excel-file/m-p/430575#M281606</link>
      <description>&lt;P&gt;I am trying to modify a macro to export each file in a target library to a separate Excel file.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have this but don't understand it enough to successfully modify it:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro SASToExcel(ExportLibrary=);

    ods output members = _Members;
    proc datasets lib = &amp;amp;ExportLibrary; run; quit;

    proc sql;
        select count(Name) into :NumOfDatasets from _Members;
		select Name into :Name from _Members;
        select Name into :Dataset1-:Dataset%trim(%left(&amp;amp;NumOfDatasets)) from _Members;
    quit;

    %do index = 1 %to &amp;amp;NumOfDatasets;
        %let myoutfile = %str(&amp;amp;Name);
		proc export data=&amp;amp;ExportLibrary..&amp;amp;&amp;amp;Dataset&amp;amp;index.
        outfile="K:\MS_PROGS\cip_codes\&amp;amp;myoutfile..xlsx"
        dbms=excel replace;
        run;
    %end;

    proc datasets;
        delete _Members;
    quit;

%mend;

%SASToExcel(ExportLibrary=work);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;This runs, but it creates one Excel file named after the first dataset in the library with each SAS dataset in the library as a sheet in that file.&amp;nbsp; I want each dataset as a file named by its SAS dataset name.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I think I am close, but don't know how to proceed.&amp;nbsp; Help, oh Great Macro Gurus??&lt;/P&gt;</description>
      <pubDate>Wed, 24 Jan 2018 18:30:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-to-export-each-file-in-a-library-to-an-Excel-file/m-p/430575#M281606</guid>
      <dc:creator>HB</dc:creator>
      <dc:date>2018-01-24T18:30:42Z</dc:date>
    </item>
    <item>
      <title>Re: Macro to export each file in a library to an Excel file</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-export-each-file-in-a-library-to-an-Excel-file/m-p/430579#M281607</link>
      <description>&lt;PRE class=" language-sas"&gt;&lt;CODE class="  language-sas"&gt;        outfile&lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt;&lt;SPAN class="token string"&gt;"K:\MS_PROGS\cip_codes\&amp;amp;myoutfile..xlsx"&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You need to make sure that is a unique file name for each, but myoutfile isn't defined anywhere in your code.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So you need to modify that line, with the name you want for the file. That should be all the changes needed that I see.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 24 Jan 2018 18:33:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-to-export-each-file-in-a-library-to-an-Excel-file/m-p/430579#M281607</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2018-01-24T18:33:59Z</dc:date>
    </item>
    <item>
      <title>Re: Macro to export each file in a library to an Excel file</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-export-each-file-in-a-library-to-an-Excel-file/m-p/430580#M281608</link>
      <description>PS. If you want to understand it, add comments to the code. Future you will thank you.</description>
      <pubDate>Wed, 24 Jan 2018 18:34:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-to-export-each-file-in-a-library-to-an-Excel-file/m-p/430580#M281608</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2018-01-24T18:34:56Z</dc:date>
    </item>
    <item>
      <title>Re: Macro to export each file in a library to an Excel file</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-export-each-file-in-a-library-to-an-Excel-file/m-p/430581#M281609</link>
      <description>&lt;P&gt;I was trying to define &amp;amp;myoutfile.&amp;nbsp; That's where&amp;nbsp; I fall down, i think.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; %let myoutfile = %str(&amp;amp;Name);&lt;/P&gt;</description>
      <pubDate>Wed, 24 Jan 2018 18:36:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-to-export-each-file-in-a-library-to-an-Excel-file/m-p/430581#M281609</guid>
      <dc:creator>HB</dc:creator>
      <dc:date>2018-01-24T18:36:13Z</dc:date>
    </item>
    <item>
      <title>Re: Macro to export each file in a library to an Excel file</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-export-each-file-in-a-library-to-an-Excel-file/m-p/430583#M281610</link>
      <description>&lt;BLOCKQUOTE&gt;
&lt;P&gt;&lt;SPAN&gt;&amp;nbsp;I want each dataset as a file named by its SAS dataset name.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;You already have a macro variable that has the value you need to meet the requirement above, you don't need to create a new one.&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;I have no idea where &amp;amp;name is coming from either so that's just as vague as outfile &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 24 Jan 2018 18:40:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-to-export-each-file-in-a-library-to-an-Excel-file/m-p/430583#M281610</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2018-01-24T18:40:03Z</dc:date>
    </item>
    <item>
      <title>Re: Macro to export each file in a library to an Excel file</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-export-each-file-in-a-library-to-an-Excel-file/m-p/430585#M281611</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;&lt;SPAN&gt;You already have a macro variable that has the value you need to meet the requirement above, &lt;/SPAN&gt;&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I do? Which one?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 24 Jan 2018 18:43:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-to-export-each-file-in-a-library-to-an-Excel-file/m-p/430585#M281611</guid>
      <dc:creator>HB</dc:creator>
      <dc:date>2018-01-24T18:43:02Z</dc:date>
    </item>
    <item>
      <title>Re: Macro to export each file in a library to an Excel file</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-export-each-file-in-a-library-to-an-Excel-file/m-p/430588#M281612</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro SASToExcel(ExportLibrary=);

%* obtain list of datasets in the library;
    ods output members = _Members;
    proc datasets lib = &amp;amp;ExportLibrary; run; quit;

    proc sql;
*get the name of datasets into the list using Dataset1, Dataset2 etc;
        select Name into :Dataset1-
from _Members;
    quit;
*store the number of records to control the loop;
%let NumOfDatasets=&amp;amp;sqlobs;

%*loop to export all your data sets;
    %do index = 1 %to &amp;amp;NumOfDatasets;

		proc export data=&amp;amp;ExportLibrary..&amp;amp;&amp;amp;Dataset&amp;amp;index.
        outfile="K:\MS_PROGS\cip_codes\&amp;amp;&amp;amp;dataset&amp;amp;index..xlsx"
        dbms=excel replace;
        run;
    %end;

%*delete dataset of list of data sets in the library;
    proc datasets;
        delete _Members;
    quit;

%mend;

%SASToExcel(ExportLibrary=work);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Try the following, simplifed&lt;/P&gt;</description>
      <pubDate>Wed, 24 Jan 2018 18:49:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-to-export-each-file-in-a-library-to-an-Excel-file/m-p/430588#M281612</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2018-01-24T18:49:31Z</dc:date>
    </item>
    <item>
      <title>Re: Macro to export each file in a library to an Excel file</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-export-each-file-in-a-library-to-an-Excel-file/m-p/430592#M281613</link>
      <description>&lt;P&gt;79: LINE and COLUMN cannot be determined.&lt;BR /&gt;NOTE: NOSPOOL is on. Rerunning with OPTION SPOOL may allow recovery of the LINE and COLUMN&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; where the error has occurred.&lt;BR /&gt;ERROR 79-322: Expecting a :.&lt;BR /&gt;76: LINE and COLUMN cannot be determined.&lt;BR /&gt;NOTE: NOSPOOL is on. Rerunning with OPTION SPOOL may allow recovery of the LINE and COLUMN&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; where the error has occurred.&lt;BR /&gt;ERROR 76-322: Syntax error, statement will be ignored.&lt;/P&gt;</description>
      <pubDate>Wed, 24 Jan 2018 18:58:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-to-export-each-file-in-a-library-to-an-Excel-file/m-p/430592#M281613</guid>
      <dc:creator>HB</dc:creator>
      <dc:date>2018-01-24T18:58:23Z</dc:date>
    </item>
    <item>
      <title>Re: Macro to export each file in a library to an Excel file</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-export-each-file-in-a-library-to-an-Excel-file/m-p/430597#M281614</link>
      <description>&lt;P&gt;You can replace:&lt;/P&gt;
&lt;PRE&gt;    ods output members = _Members;
    proc datasets lib = &amp;amp;ExportLibrary; run; quit;

    proc sql;
        select count(Name) into :NumOfDatasets from _Members;
		select Name into :Name from _Members;
        select Name into :Dataset1-:Dataset%trim(%left(&amp;amp;NumOfDatasets)) from _Members;
    quit;
&lt;/PRE&gt;
&lt;P&gt;with&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;proc sql noprint;
    select memName into :Dataset1-:Dataset9999
    from dictionary.tables
    where libname=upcase("&amp;amp;Exportlibrary");
quit;
%let NumOfDatasets=&amp;amp;sqlobs;
%put data sets: &amp;amp;NumOfDatasets;&lt;/PRE&gt;
&lt;P&gt;The dictionary tables store the information that proc datasets reads so skip that step. The select into will create only has many of the DATASET macro variables as needed.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Any proc sql code sets the automatic macrovariable sqlobs from the last request, which in this case would be the number of member names in the library. The upcase is used incase you pass mixed or lower case to the macro for the export library as the value stored is upper case.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;There is also no need with approach to delete a table as you didn't create one&lt;/P&gt;</description>
      <pubDate>Wed, 24 Jan 2018 19:03:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-to-export-each-file-in-a-library-to-an-Excel-file/m-p/430597#M281614</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2018-01-24T19:03:49Z</dc:date>
    </item>
    <item>
      <title>Re: Macro to export each file in a library to an Excel file</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-export-each-file-in-a-library-to-an-Excel-file/m-p/430602#M281615</link>
      <description>&lt;P&gt;Had to change &amp;amp;&amp;amp;dataset&amp;amp;index..xlsx to&amp;nbsp; &amp;amp;&amp;amp;dataset&amp;amp;index. otherwise it created namexlsx.XLS.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Other than that I think it works!!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 24 Jan 2018 19:13:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-to-export-each-file-in-a-library-to-an-Excel-file/m-p/430602#M281615</guid>
      <dc:creator>HB</dc:creator>
      <dc:date>2018-01-24T19:13:44Z</dc:date>
    </item>
  </channel>
</rss>

