We’re smarter together. Learn from this collection of community knowledge and add your expertise.

Macro code for exporting data from Library to an excel file

by Occasional Contributor srini_clinsas on ‎09-01-2014 02:51 AM - edited on ‎10-05-2015 03:43 PM by Community Manager (710 Views)

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);

Your turn
Sign In!

Want to write an article? Sign in with your profile.

Looking for the Ask the Expert series? Find it in its new home: communities.sas.com/askexpert.