像乔一样,我很感兴趣。它似乎不会PROC EXPORT
做你想做的事。但这里有一个 SAS 宏可以解决问题:
%macro mydlm(dsn, outf, dlm, headers);
/*****************************************************************
MYDLM.SAS
SAS Macro to create a delimited file from a SAS data set
Positional Parameters
DSN = SAS Dataset Name
OUTF = Output Text File
DLM = Delimiter to use (CSV, TAB, PIPE, or constant)
HEADERS = Y or N, Include line with variable names at top
Example
%mydlm( sashelp.class , 'c:\temp\tempfile.csv', csv, Y);
****************************************************************/
%if %QUPCASE(&DLM)=CSV %then %let DLM=%str(,);
%else %if %QUPCASE(&DLM)=TAB %then %let DLM='09'x;
%else %if %QUPCASE(&DLM)=PIPE %then %let DLM=%str(|);
proc contents noprint data=&DSN
out=_temp_(keep=name type length varnum label format formatd formatl);
run;
proc sort data=_temp_;
by varnum;
run;
data _null_;
set _temp_ end=eof;
call symput(cats('zvnm',put(_n_,5.)), name);
if format ne ' '
then call symput(cats('zvft',put(_n_,5.))
, cats(format
, put(formatl,best.), '.'
, put(formatd,best.))
);
else if type=2
then call symput(cats('zvft',put(_n_,5.)),cats('$char',put(length,best.),'.'));
else call symput(cats('zvft',put(_n_,5.)),' ');
if eof then call symput('zvcnt',left(put(_n_,8.)));
run;
data _null_;
file &outf;
set &dsn;
%if %upcase(&headers) = Y %then %do;
if _n_ = 1 then put
%do i =1 %to %eval(&zvcnt.-1);
"'%trim(&&zvnm&i)'" "&dlm"
%end;
"'%trim(&&zvnm&zvcnt)'" ;
%end;
put
%do i =1 %to %eval(&zvcnt.-1);
&&zvnm&i &&zvft&i "&dlm"
%end;
&&zvnm&i &&zvft&i;
run;
%mend mydlm;
该宏允许您选择 CSV、TAB 或 PIPE (|) 作为分隔符。字符变量将保留前导空格,所有变量的宽度将基于任何预定义的 SAS 变量格式。我用这个示例测试了宏:
data a;
number2 = 2;
format num comma7.;
format date yymmdd10.;
format char $char40.;
date = today();
num = 1; char = ' This has 10 leading blanks'; output;
num = 2; char = 'This has no leading blanks'; output;
run;
%mydlm( a , 'c:\temp\tempfile.csv', csv, y);
更新:为了表扬,上面的代码来源于我在这个网页上找到的想法。我本来只是将您指向该链接,但它并没有真正做到您想要的。也真的很难读。
UPDATE2:修改了示例宏以更正语法错误并使其更通用。这个新版本允许您指定任何字符串作为列分隔符。我添加它是为了支持另一个 Stack Overflow 问题,可以使用以下宏调用来回答:
%mydlm( Exp_TXT, '/fbrms01/dev/projects/tadis003/Export_txt_OF_New.txt', ~|~, Y);