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.

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 2 replies
  • 878 views
  • 0 likes
  • 3 in conversation