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.
Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.
Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.
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.