BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
DeepakSwain
Pyrite | Level 9

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. 

Swain
1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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?

 

View solution in original post

3 REPLIES 3
ChrisNZ
Tourmaline | Level 20

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.

ChrisNZ
Tourmaline | Level 20

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

Tom
Super User Tom
Super User

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?

 

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 6003 views
  • 2 likes
  • 3 in conversation