<?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 table in a library to an Excel file in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-export-each-table-in-a-library-to-an-Excel-file/m-p/542165#M149806</link>
    <description>&lt;P&gt;This is what I use:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;%MACRO Export_Library(Libname,path,format=CSV);&lt;/P&gt;
&lt;P&gt;DATA MEMBERS;&lt;BR /&gt;SET SASHELP.VMEMBER(WHERE=(LIBNAME = upcase("&amp;amp;Libname")));&lt;BR /&gt;RETAIN OBS 0;&lt;BR /&gt;OBS = OBS+1;&lt;BR /&gt;KEEP MEMNAME OBS;&lt;BR /&gt;RUN;&lt;/P&gt;
&lt;P&gt;PROC SQL;&lt;BR /&gt;SELECT MIN(OBS), MAX(OBS) INTO :MIN, :MAX trimmed FROM MEMBERS;&lt;BR /&gt;QUIT;&lt;/P&gt;
&lt;P&gt;%Local D;&lt;/P&gt;
&lt;P&gt;%DO D = &amp;amp;MIN %TO &amp;amp;MAX;&lt;/P&gt;
&lt;P&gt;PROC SQL;&lt;BR /&gt;SELECT COMPRESS(MEMNAME) INTO: Table trimmed&lt;BR /&gt;FROM MEMBERS&lt;BR /&gt;WHERE OBS=&amp;amp;D;&lt;BR /&gt;QUIT;&lt;BR /&gt;&lt;BR /&gt;%let Table=%trim(&amp;amp;Table);&lt;/P&gt;
&lt;P&gt;PROC EXPORT DBMS=&amp;amp;format DATA=&amp;amp;Libname..&amp;amp;Table&lt;BR /&gt;OUTFILE="&amp;amp;path./&amp;amp;Table..&amp;amp;format" replace;&lt;BR /&gt;RUN;&lt;/P&gt;
&lt;P&gt;%END;&lt;BR /&gt;&lt;BR /&gt;%MEND Export_Library;&lt;/P&gt;</description>
    <pubDate>Mon, 11 Mar 2019 18:30:01 GMT</pubDate>
    <dc:creator>tomrvincent</dc:creator>
    <dc:date>2019-03-11T18:30:01Z</dc:date>
    <item>
      <title>Macro to export each table in a library to an Excel file</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-export-each-table-in-a-library-to-an-Excel-file/m-p/542087#M149782</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm looking for sample code or approach whereby I can export tables to excel (using PC SAS), where the following is true:&lt;/P&gt;&lt;P&gt;Using example below from&amp;nbsp; two tables, where value of column Name should (a) be selected&lt;/P&gt;&lt;P&gt;when outputted to its own excel file and include the value in the Excel file name; and&amp;nbsp;&lt;/P&gt;&lt;P&gt;(b) where the sheet name would be titled with the table name or a variation of.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In other words, if there are (a) 7 unique values in column Name, there should be 7 excel files generated; if there are two tables in library, each excel file should contain two sheets (more if more tables).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;(This is a slight variation of prior posts from Reeza and ballardw here:&amp;nbsp;&lt;A href="https://communities.sas.com/t5/General-SAS-Programming/Macro-to-export-each-file-in-a-library-to-an-Excel-file/td-p/430575" target="_blank" rel="noopener"&gt;https://communities.sas.com/t5/General-SAS-Programming/Macro-to-export-each-file-in-a-library-to-an-Excel-file/td-p/430575&lt;/A&gt;).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Sample raw data&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data likes;
    length name $10 likes $10;
    input name $ likes $;
    datalines;
        Peter Cakes
        Peter Chocolate
        Simon Basketball
        Philip Apples
        Sam Oranges
        Paul Bananas
        John Cricket
        Frank Cats
        Frank Evita
        ;run;

data dislikes;
    length name $10 dislikes $10;
    input name $ dislikes $;
    datalines;
        Peter Carrots
        Peter Eggs
        Simon Pool
        Philip Bananas
        Sam Pears
        Paul Oreos
        John Bingo
        Frank Rats
        Frank Celery
        ;run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;Sample macro that works with the first table&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro example2a; 

     proc sql noprint;
        select count(distinct name)
        into :obs
        from likes;
    quit;

    proc sql noprint;
        select distinct name
        into :name1-
        from likes;
    quit;

     %do i = 1 %to &amp;amp;obs;
        data im_.&amp;amp;&amp;amp;name&amp;amp;i;
            set likes;
            where name = "&amp;amp;&amp;amp;name&amp;amp;i";
        run;
    %end;

%mend;

%example2a

/*source: https://www.quanticate.com/blog/bid/58366/the-into-statement-in-proc-sql-to-create-macro-variables*/&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Sample output of 1 file&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;&lt;BR /&gt;%let filetitle=Monthly_Report;&lt;BR /&gt;%let drop=C:\Users\sxxxxx\temp\sas\;&lt;BR /&gt;&lt;BR /&gt;PROC EXPORT 
      DATA=/*name*/
      OUTFILE= "&amp;amp;drop.&amp;amp;filetitle./*name*/.xlsx"
      DBMS=xlsx replace; 
      SHEET="/*Likes*/";
run;

PROC EXPORT 
      DATA=/*name*/
      OUTFILE= "&amp;amp;drop.&amp;amp;filetitle./*name*/.xlsx"
      DBMS=xlsx replace; 
      SHEET="/*Dislikes*/";
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;thank you- I don't&amp;nbsp; have plenty of experience creating dynamic outputs, so adding an explanation to your suggestion would be highly appreciated (note that i have downloaded and will begin to study the 1300+ page SAS Functions and call routine manual to educate myself on this topic).&lt;/P&gt;</description>
      <pubDate>Mon, 11 Mar 2019 15:53:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-to-export-each-table-in-a-library-to-an-Excel-file/m-p/542087#M149782</guid>
      <dc:creator>brulard</dc:creator>
      <dc:date>2019-03-11T15:53:29Z</dc:date>
    </item>
    <item>
      <title>Re: Macro to export each table in a library to an Excel file</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-export-each-table-in-a-library-to-an-Excel-file/m-p/542165#M149806</link>
      <description>&lt;P&gt;This is what I use:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;%MACRO Export_Library(Libname,path,format=CSV);&lt;/P&gt;
&lt;P&gt;DATA MEMBERS;&lt;BR /&gt;SET SASHELP.VMEMBER(WHERE=(LIBNAME = upcase("&amp;amp;Libname")));&lt;BR /&gt;RETAIN OBS 0;&lt;BR /&gt;OBS = OBS+1;&lt;BR /&gt;KEEP MEMNAME OBS;&lt;BR /&gt;RUN;&lt;/P&gt;
&lt;P&gt;PROC SQL;&lt;BR /&gt;SELECT MIN(OBS), MAX(OBS) INTO :MIN, :MAX trimmed FROM MEMBERS;&lt;BR /&gt;QUIT;&lt;/P&gt;
&lt;P&gt;%Local D;&lt;/P&gt;
&lt;P&gt;%DO D = &amp;amp;MIN %TO &amp;amp;MAX;&lt;/P&gt;
&lt;P&gt;PROC SQL;&lt;BR /&gt;SELECT COMPRESS(MEMNAME) INTO: Table trimmed&lt;BR /&gt;FROM MEMBERS&lt;BR /&gt;WHERE OBS=&amp;amp;D;&lt;BR /&gt;QUIT;&lt;BR /&gt;&lt;BR /&gt;%let Table=%trim(&amp;amp;Table);&lt;/P&gt;
&lt;P&gt;PROC EXPORT DBMS=&amp;amp;format DATA=&amp;amp;Libname..&amp;amp;Table&lt;BR /&gt;OUTFILE="&amp;amp;path./&amp;amp;Table..&amp;amp;format" replace;&lt;BR /&gt;RUN;&lt;/P&gt;
&lt;P&gt;%END;&lt;BR /&gt;&lt;BR /&gt;%MEND Export_Library;&lt;/P&gt;</description>
      <pubDate>Mon, 11 Mar 2019 18:30:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-to-export-each-table-in-a-library-to-an-Excel-file/m-p/542165#M149806</guid>
      <dc:creator>tomrvincent</dc:creator>
      <dc:date>2019-03-11T18:30:01Z</dc:date>
    </item>
    <item>
      <title>Re: Macro to export each table in a library to an Excel file</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-export-each-table-in-a-library-to-an-Excel-file/m-p/542166#M149807</link>
      <description>Thanks Tom... I will try it</description>
      <pubDate>Mon, 11 Mar 2019 18:32:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-to-export-each-table-in-a-library-to-an-Excel-file/m-p/542166#M149807</guid>
      <dc:creator>brulard</dc:creator>
      <dc:date>2019-03-11T18:32:04Z</dc:date>
    </item>
    <item>
      <title>Re: Macro to export each table in a library to an Excel file</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-export-each-table-in-a-library-to-an-Excel-file/m-p/542167#M149808</link>
      <description>&lt;P&gt;It defaults out to CSV but you can change it to xls, xlsx,&amp;nbsp; etc.&lt;/P&gt;</description>
      <pubDate>Mon, 11 Mar 2019 18:33:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-to-export-each-table-in-a-library-to-an-Excel-file/m-p/542167#M149808</guid>
      <dc:creator>tomrvincent</dc:creator>
      <dc:date>2019-03-11T18:33:49Z</dc:date>
    </item>
    <item>
      <title>Re: Macro to export each table in a library to an Excel file</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-export-each-table-in-a-library-to-an-Excel-file/m-p/544043#M150425</link>
      <description>&lt;P&gt;hello. So here is a tweak of Tom's and some other codes to export&amp;nbsp; excel files (i) based on char values from a user 'name' column (eg., where values like John, Jane, etc.) , and (ii) table name(s) of all tables within given library (eg., work.table_a; ...table_b) such as the excel file name is the user 'name' value, and each sheet name is equal to the table names (where user name value is found)...there may be a more efficient means of coding this.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%Let Libname=IM_RE; /* edit libname here */
%put &amp;amp;libname;

%MACRO Export;

proc sql noprint;
   select count(distinct Business_Owner) /* Edit source column as needed */
   into :obs
   from &amp;amp;libname..quad1; /* Edit source table as needed*/
quit;

proc sql noprint;
   select distinct Business_Owner
   into :name1- notrim
   from &amp;amp;libname..quad1 (where=(Business_Owner ne ''));
quit;
&lt;BR /&gt;/*edit: adding a 'distinct' in lieu of'group' segment*/
proc sql; create table test as
select distinct LIBNAME, memname
from dictionary.columns
WHERE LIBNAME=upcase("&amp;amp;Libname")
;quit;

DATA MEMBERS;
SET test;
RETAIN OBS 0;
OBS = OBS+1;
KEEP MEMNAME OBS;
RUN;

PROC SQL noprint;
SELECT MIN(OBS), MAX(OBS) INTO :MIN, :MAX trimmed FROM MEMBERS;
QUIT;

%Local D;

%DO D = &amp;amp;MIN %TO &amp;amp;MAX;
/*b*/
%do i = 1 %to &amp;amp;obs;
/*b*/
PROC SQL noprint;
SELECT COMPRESS(MEMNAME) INTO: Table trimmed
FROM MEMBERS
WHERE OBS=&amp;amp;D;
QUIT;

%let Table=%trim(&amp;amp;Table);


PROC EXPORT /*exports to a default unix server. Add a filepath to outfile as needed*/
      DATA=&amp;amp;libname..&amp;amp;Table (where=(Business_Owner= "&amp;amp;&amp;amp;name&amp;amp;i")) 
      OUTFILE="%qsubstr(&amp;amp;&amp;amp;name&amp;amp;i.,1).xlsx"
     DBMS=xlsx replace;
      SHEET="&amp;amp;Table";
     run;

     /* 19.03.18.9:00am –optional code to generate tables which is good 
     for data validation 
     data &amp;amp;libname..&amp;amp;Table._&amp;amp;&amp;amp;name&amp;amp;i;
         set &amp;amp;libname..&amp;amp;Table;
         where Business_Owner = "&amp;amp;&amp;amp;name&amp;amp;i";
     run;  */
    
%end;
%end;
%MEND Export;

Options symbolgen mlogic  ;
/*Remove above line when macro working .For efficiency, consider putting
Options NoSymbolgen nomlogic  ;
*/

%Export;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 20 Mar 2019 12:01:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-to-export-each-table-in-a-library-to-an-Excel-file/m-p/544043#M150425</guid>
      <dc:creator>brulard</dc:creator>
      <dc:date>2019-03-20T12:01:28Z</dc:date>
    </item>
  </channel>
</rss>

