Date: Wed, 5 Nov 1997 17:47:03 CST
Reply-To: Undetermined origin c/o LISTSERV administrator
<owner-LISTSERV@UGA.CC.UGA.EDU>
Sender: "SAS(r) Discussion" <SAS-L@UGA.CC.UGA.EDU>
From: Undetermined origin c/o LISTSERV administrator
<owner-LISTSERV@UGA.CC.UGA.EDU>
Subject: Simple macro to write tab-delimited file from SAS dataset
This isn't a production-level macro, but it handle many kinds
of data. It's designed to write tab-delimited files which will
be written by Excel, so there's a hardcoded limit of 255 columns.
----------
data _null_;
length outval $200.;
length fmt1-fmt255 $9.;
array fmtinfo{*} fmt1-fmt255;
file "&FILENAME." recfm=v lrecl=10000 &DISP.;
dsid = open("&DATA.", 'IS');
if dsid = 0 then /* No good */
do;
error "ERROR: '&DATA.' is not a valid dataset name.";
abort;
end;
nvars = attrn(dsid, 'NVARS');
if nvars > 255 then
do;
error "ERROR: &DATA. has more than 255 columns.";
sysrc = close(dsid);
stop;
end;
do var_n = 1 to nvars;
/* Get format info */
fmtinfo{var_n} = varfmt(dsid, var_n);
%if &HEADERS. eq Y %then
%do;
/* Write header row */
varname = varname(dsid, var_n);
varlabel = scan(varlabel(dsid, var_n), 1);
/* If the first word of the label is the same as the name,
use the value from the label. It might be in mixed case. */
if varname = upcase(varlabel) then
varname = varlabel;
if var_n ne nvars then
put varname +(-1) '09'x @;
else
put varname;
%end;
end;
fetchrc = fetch(dsid, 'noset');
/* Loop through observations */
do while (fetchrc = 0);
%if &outobs. ne %then
%do;
if obscount = &outobs. then leave;
%end;
obscount + 1;
/* Loop through variables */
do var_n = 1 to nvars;
if vartype(dsid, var_n) = 'C' then
outval = trim(putc(getvarc(dsid, var_n), fmtinfo{var_n}));
else
outval = trim(putn(getvarn(dsid, var_n), fmtinfo{var_n}));
if var_n ne nvars then
put outval +(-1) '09'x @;
else
put outval;
end;
fetchrc = fetch(dsid, 'noset');
end;
sysrc = close(dsid);
file log;
outmsg = "NOTE: File &FILENAME. was written with "
|| trim(left(put(obscount, comma10.0)))
|| ' rows and '
|| trim(left(put(nvars, 3.)))
|| ' columns.';
put outmsg;
*****; run;
%mend;
----------
--
Jack_Hamilton@hccompare.com