BookmarkSubscribeRSS Feed
anthdelm
Calcite | Level 5

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.

2 REPLIES 2
novinosrin
Tourmaline | Level 20
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"

 

Tom
Super User Tom
Super User

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.

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 1528 views
  • 0 likes
  • 3 in conversation