对此不熟悉,所以道歉。我在SAS中有一个文件,我需要以CSV格式导出,我需要在所有字段中添加双引号。我怎么能做到这一点?提前谢谢。
答案 0 :(得分:6)
有很多方法可以从SAS创建CSV文件。使用proc export
不会用双引号包装每个字段,因此最容易执行此操作的是%ds2csv()
宏。假设您拥有SAS 9.2或更高版本。它的文档可以在这里找到:
http://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a002683390.htm
运行它的一个例子:
%ds2csv(data=sashelp.retail, runmode=b, csvfile=c:\class.csv);
产地:
"Retail sales in millions of $","DATE","YEAR","MONTH","DAY"
"$220","80Q1","1980","1","1"
"$257","80Q2","1980","4","1"
"$258","80Q3","1980","7","1"
答案 1 :(得分:0)
这是另一个使用数据步骤
data _null_;
file 'data_want.csv' dsd dlm = ',';
set data_have;
if _n_ = 1 then put @1 "variables name to input"; /* Add variable names */
put (_all_) (~);
run;
答案 2 :(得分:0)
这是一种通过包含标题获得相同结果的方法
proc contents data=sashelp.class out=vars(keep=name varnum);run;
proc sql noprint;
select quote(strip(name)) into :vars separated by '|' from vars
order by varnum;
quit;
data _null_;
file 'test.txt' dsd dlm='|';
set sashelp.class;
if _n_ = 1 then put %sysfunc(quote(&vars.));
put (_all_) (~);
run;
答案 3 :(得分:0)
我知道这是一个古老的问题,但我的2便士......
下面的宏不会引用格式化为字符值的数值,但它很容易实现。
%macro csvQuoteChars(lib=,dset=,outPath=);
proc contents data=&lib..&dset out=c(keep=name type) noprint;
run;
%let dsid = %sysfunc(open(c));
%let nobs = %sysfunc(attrn(&dsid,nlobs));
data _null_;
set &lib..&dset;
file "&outPath\&lib..&dset..csv" dsd;
%do i=1 %to &nobs;
%let rc = %sysfunc(fetchobs(&dsid,&i));
%let vName = %sysfunc(getvarc(&dsid,1));
%let vType = %sysfunc(getvarn(&dsid,2));
%put &=vName &=vType;
%if &vType = 2 %then %do;
put (&vName) (~) @;
%end;
%else %if &vType = 1 %then %do;
put &vName @;
%end;
%if &i = &nobs %then %do;
put;
%end;
%end;
run;
%let dsid = %sysfunc(close(&dsid));
%mend csvQuoteChars;
%csvQuoteChars(lib=sashelp,dset=class,outPath=d:\temp);