1/* Macro exportlib
2
3    Exports all SAS datasets in a data library to csv files.  One of
4    the datasets is assumed to be the result of PROC FORMAT CNTLOUT=
5    if any user formats are referenced.  Numeric variables are
6    formatted in BEST16 format so that date/time variables will be
7    exported with their internal numeric values.  A special file
8    _contents_.csv is created to hold, for all datasets combined, the
9    dataset name, dataset label, variable names, labels, formats,
10    types, and lengths.
11
12    Usage:
13
14    %INCLUDE "foo\exportlib.sas";    * Define macro;
15    LIBNAME lib ...;                 * E.g. LIBNAME d SASV5XPT "foo.xpt";
16    %exportlib(lib, outdir, tempdir);
17
18    Arguments:
19        lib     - SAS libname for input datasets
20        outdir  - directory in which to write .csv files (default ".")
21        tempdir - temporary directory to hold generated SAS code
22                  (default C:/WINDOWS/TEMP)
23                                                                             */
24%macro exportlib(lib,  outdir, tempdir);
25%IF %QUOTE(&outdir)=   %THEN %LET outdir=.;
26%IF %QUOTE(&tempdir)=  %THEN %LET tempdir=C:/WINDOWS/TEMP;
27OPTIONS NOFMTERR;
28PROC COPY IN=&lib OUT=work;RUN;
29PROC CONTENTS DATA=work._ALL_ NOPRINT
30    OUT=_contents_(KEEP=memname memlabel name type label format length
31                        nobs);RUN;
32PROC EXPORT DATA=_contents_ OUTFILE="&outdir/_contents_.csv" REPLACE;RUN;
33DATA _NULL_; SET _contents_; BY MEMNAME;
34    FILE "&tempdir/_export_.sas"; RETAIN bk -1;
35    if FIRST.MEMNAME & (NOBS > 0) THEN DO;
36        PUT "DATA " MEMNAME "; SET " MEMNAME ";FORMAT _NUMERIC_ BEST14.;RUN;";
37        PUT "PROC EXPORT DATA=" MEMNAME " OUTFILE=" '"' "&outdir/"
38            MEMNAME +bk ".csv" '" ' "REPLACE;RUN;";
39        END;
40    RUN;
41%INCLUDE "&tempdir/_export_.sas";RUN;
42%MEND exportlib;
43