BookmarkSubscribeRSS Feed

Macro code for exporting data from Library to an excel file

Started ‎09-01-2014 by
Modified ‎10-05-2015 by
Views 2,118

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

select

libname, memname from

dictionary.tables

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

Quit;

 

data tab;

set tables;

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

run;

 

proc sql noprint;

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

quit;

 

/* 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;

sheet="&sh";

run;

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

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

%end;

%mend autoexp;

 

 

/*  Calling  */

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

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

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

SAS AI and Machine Learning Courses

The rapid growth of AI technologies is driving an AI skills gap and demand for AI talent. Ready to grow your AI literacy? SAS offers free ways to get started for beginners, business leaders, and analytics professionals of all skill levels. Your future self will thank you.

Get started

Article Labels
Article Tags