- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Hi there,
I need your kind help to export a sas data having double quote in s=csv format.
data have;
name='john';
age='25';
run;
data have2;
set have (rename=(name=name1 age=age1));
name=quote(strip(name1));
age=quote(strip(age1));
drop name1 age1;
run;
proc export data=have2
outfile="M:\test\want.csv" dbms=csv replace;
run;
I am expecting :
NAME,AGE
"john","25"
I am getting:
NAME,AGE
"""john""","""25"""
Can you suggest revision in the existing code to get the desired output.
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
In a CSV file the quotes only need to be added when they are needed to protect delimiters (or to protect quotes themselves to avoid confusion).
In your example data there is no need for the quotes because the values do not contain either delimiters or quotes. So the proper file for that data is:
NAME,AGE john,25
Please explain WHY you want a non-standard file format?
If you really really need to have SAS add quotes just use the ~ modifier.
data _null_;
set have;
file "M:\test\want.csv" dsd ;
put (name age) (~);
run;
If you want to include the header row then write that first and then use the MOD option on the FILE statement when writing the data.
proc transpose data=have(obs=0) out=names;
var _all_;
run;
data _null_;
set names;
file "M:\test\want.csv" dsd ;
put _name_ @ ;
run;
data _null_;
set have;
file "M:\test\want.csv" dsd mod;
put (_all_) (~);
run;
PS How come the column headers don't have quotes around them in your example?
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
The result you are getting is the expected result.
I am expecting :
NAME,AGE
"john","25"
If you have this data in the CSV file, the quotes are seen as delimiters and stripped when the data is read.
The output from this data is unquoted values.
Since you export quoted values, SAS adds surrounding quotes so that the the values read from the CSV file are also quoted.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Note that proc export
can't wrap every (unquoted) field in double-quotes; the easiest one that will do this is the %ds2csv()
macro.
See http://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a002683390.htm
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
In a CSV file the quotes only need to be added when they are needed to protect delimiters (or to protect quotes themselves to avoid confusion).
In your example data there is no need for the quotes because the values do not contain either delimiters or quotes. So the proper file for that data is:
NAME,AGE john,25
Please explain WHY you want a non-standard file format?
If you really really need to have SAS add quotes just use the ~ modifier.
data _null_;
set have;
file "M:\test\want.csv" dsd ;
put (name age) (~);
run;
If you want to include the header row then write that first and then use the MOD option on the FILE statement when writing the data.
proc transpose data=have(obs=0) out=names;
var _all_;
run;
data _null_;
set names;
file "M:\test\want.csv" dsd ;
put _name_ @ ;
run;
data _null_;
set have;
file "M:\test\want.csv" dsd mod;
put (_all_) (~);
run;
PS How come the column headers don't have quotes around them in your example?