我有一个脚注,用逗号列出项目。输出需要发送到.csv。因为.csv是逗号分隔的,所以当在Excel中打开时,列表的项目将输出到不同的单元格中。导出到.csv时如何转义逗号?
例如,
ods csvall file = "test.csv";
title 'The first seven letters of the alphabet.';
data test;
input x $ ;
datalines;
a
B
c
D
e
F
G
;
run;
footnote 'Observe that the letters a, c, and e are lowercase.';
proc print data = test;
run;
ods csvall close;
我尝试使用ods escapechar "\";
定义转义符号并将脚注更改为
footnote 'Observe that the letters a\, c\, and e are lowercase.';
但这不起作用。可能csvall
不是ODS的一部分。但是,除此之外,我还不确定还有什么可以尝试。
这是上述代码生成的文件中的文字:
The first seven letters of the alphabet.
"Obs","x"
"1","a"
"2","B"
"3","c"
"4","D"
"5","e"
"6","F"
"7","G"
Observe that the letters a, c, and e are lowercase.
更新:根据回答重新考虑我的问题,将脚注划分为不同的细胞不是由于SAS。导入CSV时,由Excel引起。但是,我认为没有办法自动覆盖此行为。
答案 0 :(得分:1)
如果在脚注文本中添加单引号,则会引用逗号并将其读回field1。
ods csvall file = "~/test.csv";
title 'The first seven letters of the alphabet.';
data test;
input x $ ;
datalines;
a
B
c
D
e
F
G
;
run;
footnote "'Observe that the letters a, c, and e are lowercase.'";
proc print data = test;
run;
ods csvall close;
data _null_;
infile "~/test.csv" dsd missover;
length field1-field2 $60.;
input (field:)(:);
put (field:)(=);
run;
答案 1 :(得分:1)
我认为你的目标是生成一个包含如下文本的文件:
"The first seven letters of the alphabet."
"Obs","x"
"1","a"
"2","B"
"3","c"
"4","D"
"5","e"
"6","F"
"7","G"
"Observe that the letters a, c, and e are lowercase."
这应该按照您期望的方式运行 - 大多数程序不应将页脚引用文本中的逗号解释为字段分隔符。
我建议使用数据步骤而不是ods csvall
来执行此操作,因为它相对简单:
data _null_;
file "/tmp/test2.csv" dsd;
set test end = eof;
if _n_ = 1 then put '"The first seven letters of the alphabet."';
put _n_ x;
if eof then put '"Observe that the letters a, c, and e are lowercase."';
run;
N.B。如果它们包含逗号或嵌入式引号(它们被提升为双引号),它只会在字符变量周围添加引号,并且您必须自己在字符串文字周围添加引号。