Add double quotes to every variable record

Reply
New Contributor
Posts: 2

Add double quotes to every variable record

I have a SAS dataset that I need to send to a company, but they are asking for every single record to be surrounded in double-quotes;

 

i.e. A dataset looking like:

 

name,state,date

John Smith,NY,2018-07-05

Eli Manning,NJ,2018-07-05

Mookie Betts,MA,2018-07-05

Phil Parker,TX,2018-07-05

 

...needs to look like:

 

"John Smith","NY","2018-07-05"

"Eli Manning","NJ","2018-07-05"

"Mookie Betts","MA,"2018-07-05"

"Phil Parker","TX","2018-07-05"

 

Is there an easy way to run the quote() function through every variable?  Thanks.

PROC Star
Posts: 1,784

Re: Add double quotes to every variable record

[ Edited ]
data have;
infile cards dlm=',';
input (name  state  date) (:$30.);
cards;
John Smith,NY,2018-07-05
Eli Manning,NJ,2018-07-05
Mookie Betts,MA,2018-07-05
Phil Parker,TX,2018-07-05
;

data want;
set have;
array t(*) name--date;
do _n_=1 to dim(t);
t(_n_)=quote(strip(t(_n_)));
end;
run;

 

 

SAS Output

Obs name state date1234
"John Smith""NY""2018-07-05"
"Eli Manning""NJ""2018-07-05"
"Mookie Betts""MA""2018-07-05"
"Phil Parker""TX""2018-07-05"

 

Super User
Super User
Posts: 8,086

Re: Add double quotes to every variable record

[ Edited ]

Use the ~ (tilda) modifier in your PUT statement.

filename csv temp;
data _null_ ;
  set sashelp.class (obs=5);
  file csv dsd ;
  put (_all_) (~) ;
run;
  

Ask them why they want to waste that much space in your files.  That is two extra characters per field per observation.

 

You should also ask them how you should handle values that already include double quote characters.  What SAS does (and is normal for CSV files) is to double the embedded quotes.  So a value like:

He said, "Stop".

would be written to the file as 

"He said, ""Stop""."

A lot of database systems have adopted Unix style escape characters for data files instead of following established CSV standards.

"He said, \"Stop\"."

That would be much harder to generate using SAS.

Ask a Question
Discussion stats
  • 2 replies
  • 62 views
  • 0 likes
  • 3 in conversation