英文:
SAS CSV export has an unwanted leading comma in each row
问题
我是中文翻译,以下是您要翻译的内容:
"I'm new to SAS and am struggling to export data to CSV. My code successfully exports a file but it has a leading comma on each non-header row that creates a misalignment.
I feel like I am missing something obvious and could really use some help. Thank you!"
file "/export.csv";
set "/table" (keep= field1 field2 'field 3'n);
if _n_ = 1 then
do; /* write header row */
put "field1"
','
"field2"
','
"field3";
end;
do;
put (_all_) (',');
end;
run;
My output ends up looking like this...
field1,field2,field3,
,x ,y ,z
,a ,b ,c
,d ,e ,f...
or
Field1 | Field2 | Field3 | |
---|---|---|---|
x | y | z | |
a | b | c | |
d | e | f |
英文:
I'm new to SAS and am struggling to export data to CSV. My code successfully exports a file but it has a leading comma on each non-header row that creates a misalignment.
I feel like I am missing something obvious and could really use some help. Thank you!
data _null_;
file "/export.csv";
set "/table" (keep= field1 field2 'field 3'n);
if _n_ = 1 then
do; /* write header row */
put "field1"
','
"field2"
','
"field3";
end;
do;
put (_all_) (',');
end;
run;
My output ends up looking like this...
field1,field2,field3,
,x ,y ,z
,a ,b ,c
,d ,e ,f...
or
Field1 | Field2 | Field3 | |
---|---|---|---|
x | y | z | |
a | b | c | |
d | e | f |
答案1
得分: 2
使用 proc export
代替。相比手动导出 .csv,它将为您节省大量时间和精力。
proc export
data = table(keep=field1 field2 field3)
file = '/export.csv'
dbms = csv
replace;
run;
英文:
Use proc export
instead. It'll save you a lot of time and effort compared to coding a manual export of .csv.
proc export
data = table(keep=field1 field2 field3)
file = '/export.csv'
dbms = csv
replace;
run;
答案2
得分: 1
使用我的第二个最喜欢的CALL例程的通用版本。
data _null_;
file log ls=256 dsd; /*change LOG to your file*/
set sashelp.heart;
if _n_ eq 1 then link names;
put (_all_)(:);
return;
names:
length _name_ $32;
do while(1);
call vnext(_name_);
if _name_ eq '_name_' then leave;
put _name_ @;
end;
put;
return;
run;
英文:
A general version using my second favorite CALL routine.
data _null_;
file log ls=256 dsd; /*change LOG to your file*/
set sashelp.heart;
if _n_ eq 1 then link names;
put (_all_)(:);
return;
names:
length _name_ $32;
do while(1);
call vnext(_name_);
if _name_ eq '_name_' then leave;
put _name_ @;
end;
put;
return;
run;
答案3
得分: 1
以下是您要翻译的内容:
Since you seem to already know the variable names (since you have hard coded the header line) then just modify the PUT statement to not insert the extra comma.
put field1 (field2 'field 3'n) (',');
但真的,你应该告诉SAS你正在写一个分隔文件,使用DSD选项。
/* 写入标题行 */
data _null_;
file "/export.csv";
put "field1,field2,field 3";
run;
/* 添加实际数据 */
data _null_;
file "/export.csv" dsd mod ;
set table;
put field1 field2 'field 3'n ;
run;
如果你事先不知道变量名,只需请求SAS为您生成它们。
/* 写入标题行 */
proc transpose data=table(obs=0) out=names;
var _all_;
run;
data _null_;
file "/export.csv" dsd;
set names;
put _name_ @;
run;
/* 添加实际数据 */
data _null_;
file "/export.csv" dsd mod ;
set table ;
put (_all_) (+0);
run;
英文:
Since you seem to already know the variable names (since you have hard coded the header line) then just modify the PUT statement to not insert the extra comma.
put field1 (field2 'field 3'n) (',');
But really you should just tell SAS you are writing a delimited file by using the DSD option.
/* write header row */
data _null_;
file "/export.csv";
put "field1,field2,field 3";
run;
/* Add the actual data */
data _null_;
file "/export.csv" dsd mod ;
set table;
put field1 field2 'field 3'n ;
run;
If you don't actually know the variable names in advance then just ask SAS to generate them for you.
/* write header row */
proc transpose data=table(obs=0) out=names;
var _all_;
run;
data _null_;
file "/export.csv" dsd;
set names;
put _name_ @;
run;
/* Add the actual data */
data _null_;
file "/export.csv" dsd mod ;
set table ;
put (_all_) (+0);
run;
答案4
得分: 0
在这段代码中,你使用了dsd文件语句选项来处理数据,并将数据输出到名为"/export.csv"的文件中。你从名为"/table"的数据集中选择了字段"field1"、"field2"和"field 3"(包括字段名中的引号)。
如果你需要对代码中的某部分进行进一步解释或有其他问题,请随时提出。
英文:
In the interim, I had Magoo'd may way into a method that works, too, but these are all good answers.
I used the dsd file statement option.
data _null_;
file "/export.csv"; dsd delimiter=",";
set "/table" (keep= field1 field2 'field 3'n);
if _n_ = 1 then
do; /* write header row */
put "field1"
','
"field2"
','
"field3";
end;
do;
put (_all_) (+0);
end;
run;
答案5
得分: 0
一个更多的选项,如果重命名列是有效的。
- ODS CSV
- LABELS选项以打印标签而不是变量名,允许重命名。还支持在导出时更改/应用格式。
ods csv file='/home/fkhurshed/demo.csv';
proc print data=sashelp.class label noobs;
label age= '年龄(岁)' sex = '性别' weight = '体重(磅)' height = '身高(英寸)';
run;
ods csv close;
英文:
One more option, which does work if renaming the columns.
- ODS CSV
- LABELS option to print labels instead of variable names which allows for renaming. Also supports changing/applying formats while exporting.
ods csv file='/home/fkhurshed/demo.csv';
proc print data=sashelp.class label noobs;
label age= 'Age (years)' sex = 'Sex' weight = 'Weight (lbs)' height = 'Height (inches)';
run;
ods csv close;
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论