BookmarkSubscribeRSS Feed

Macro code for exporting data from Library to an excel file

Started ‎09-01-2014 by
Modified ‎10-05-2015 by
Views 1,891

Hi all,


I thought of writing a macro code which can export all the datasets from a library into an excel file.

Here is the code that does the task well.



/* Define macro for export */

%macro autoexp(File, libnm);

%let libval=%upcase(&libnm);

proc sql;

create table tables as


libname, memname from


where libname="&libval" & memtype="DATA";   * Use double quote only in macros  ;



data tab;

set tables;

dnm=trim(libname)|| '.' || trim(memname);



proc sql noprint;

select dnm into :dnams separated by '*' from tab;



/* Export from datasets one by one into the excel file */

%let i=1;

%let dns= %scan(&dnams, &i, %str(*) );

%do %while (&dns ne  );

%let sh=%scan(&dns,2);

proc export data=&dns outfile="&file" dbms=excel replace;



%let i=%eval(&i+1);

%let dns= %scan(&dnams, &i, %str(*) );


%mend autoexp;



/*  Calling  */

%autoexp(d:\sas output\mass export.xls , syst);

Version history
Last update:
‎10-05-2015 03:43 PM
Updated by:


Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.


Register now!

Free course: Data Literacy Essentials

Data Literacy is for all, even absolute beginners. Jump on board with this free e-learning  and boost your career prospects.

Get Started

Article Labels
Article Tags