Sample 24583: Using Macro to create a comma delimited file
Dynamically generate a CSV or comma delimited
file with macro logic and SQL dictionary tables.
See also
Robust macro to write a delimited file.
These sample files and code examples are provided by SAS Institute
Inc. "as is" without warranty of any kind, either express or implied, including
but not limited to the implied warranties of merchantability and fitness for a
particular purpose. Recipients acknowledge and agree that SAS Institute shall
not be liable for any damages whatsoever arising out of their use of this material.
In addition, SAS Institute will provide no support for the materials contained herein.
/* Use this macro to generate a comma delimited file. You can */
/* change the delimiter via the DLM= option on the FILE */
/* statement. */
/* */
/* Customizations are only needed in the last two lines. */
/* 1) On the FILENAME statement, replace "c:\macro.txt" with */
/* the path of the flat file you are writing to on your */
/* operating system. */
/* 2) Specify your libref and member names as the first two */
/* parameters in the macro invocation. */
/* */
/* See also Sample 24727, Robust macro to write a delimited file. */
%macro delim(lib,dsn,out);
proc sql noprint;
/* The tilde modifier works in conjunction with DSD to quote */
/* all values written out by the PUT statement. If you remove */
/* the tilde below, only the values that contain the specified */
/* delimiter will be quoted. */
select trim(name)||'~' into :vars separated by " "
from dictionary.columns
where libname=%upcase("&lib") and memname=%upcase("&dsn");
quit;
data _null_;
file &out noprint DSD;
set &lib..&dsn;
put &vars;
run;
%mend delim;
/* May add options on your FILENAME statement */
filename outfile "c:\macro.txt" lrecl=1000;
/* Create pretend data to test the macro */
data one;
input a b $ c;
datalines;
1 joe 2
3 jan 4
;
/* Call the macro supplying the libref,dataset name, and the fileref */
/* created in the macro on the FILENAME statement */
%delim(work,one,outfile)
These sample files and code examples are provided by SAS Institute
Inc. "as is" without warranty of any kind, either express or implied, including
but not limited to the implied warranties of merchantability and fitness for a
particular purpose. Recipients acknowledge and agree that SAS Institute shall
not be liable for any damages whatsoever arising out of their use of this material.
In addition, SAS Institute will provide no support for the materials contained herein.
OUTPUT to external file
"1","joe","2"
"3","jan","4"
Dynamically generate a CSV or comma delimited
file with macro logic and SQL dictionary tables.
| Type: | Sample |
| Topic: | SAS Reference ==> DATA Step Common Programming Tasks ==> Reading and Writing External Data Common Programming Tasks ==> Working with Character Data
|
| Date Modified: | 2007-12-11 14:16:39 |
| Date Created: | 2004-09-30 14:08:56 |
Operating System and Release Information
| SAS System | Base SAS | All | 8 TS M0 | n/a |