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.
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
"John Smith" | "NY" | "2018-07-05" |
"Eli Manning" | "NJ" | "2018-07-05" |
"Mookie Betts" | "MA" | "2018-07-05" |
"Phil Parker" | "TX" | "2018-07-05" |
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.
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.