BookmarkSubscribeRSS Feed
Srigyan
Quartz | Level 8

I have a sas data set and I need to export in csv and that csv file needs to be imported somewhere else. We are not sure what delemeter can be used to seprate one column to another column.As you can say Record can have anything inside it. So taking any delimeter like comma,tab or space etc is risky.
1) Please suggest how to solve this problem.

2) One solution is my mind to replace (")double quote with (') single quote in RECORD field and cover char with (") double quote while export it. What code should be written.

            2.1) How to replace double quote with single quote.

            2.2) How to export csv file covering char field with double quote.

 

 

NAMERECORDAMOUNTDATE
NAME-1RECORD"120.8317/10/2017
NAME-2RECO,RD"29.8630/05/2018
NAME-3RECORD"35.6805/08/2012
NAME-4REC,ORD"43.2101/06/2012
NAME-5RECORD"5465.3703/10/2014
4 REPLIES 4
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Why do you have a single double quote in the RECORD field?  That seems like a very bad idea, as it will most likely break any system or process you try to put it into and limits your export options.  I would highly recommend removing the double quotes in there and only using single quotes if you really have to, to avoid comlications.  

data want;
  set have;
  record=tranwrd(record,'"',"'");
run;

Should replace double quote with single.  Then you can export as csv, and surround all text data with double quotes.

 

Personally I would do data cleaning on RECORD column before doing anything further, remove all quotes, remove commas, remove any other character not needed.

Srigyan
Quartz | Level 8

how to export all char with double quote 

 

Tom
Super User Tom
Super User

Just create a NORMAL csv file. In a CSV file values are separated by commas.  You can make a generalized version using a different delimiter. In fact in many European countries they use semi-colon as the delimiter because commas are use as decimal places and so appear frequently in numeric fields.

 

Values can OPTIONALLY be enclosed in quotes. When a value is quoted then embedded quotes should be doubled up. Note that values that contain the delimiter (comma) or quote character MUST be enclosed in quotes. 

 

Le'ts try it with your example data.

data have ;
  infile cards dsd dlm='|' truncover ;
  input NAME $ RECORD :$10. AMOUNT DATE :ddmmyy. ;
  format date yymmdd10. ;
cards;
NAME-1|RECORD"1|20.83|17/10/2017
NAME-2|RECO,RD"2|9.86|30/05/2018
NAME-3|RECORD"3|5.68|05/08/2012
NAME-4|REC,ORD"4|3.21|01/06/2012
NAME-5|RECORD"5|465.37|03/10/2014
;

filename csv temp;
data _null_;
  set have ;
  file csv dsd ;
  put (_all_) (+0);
run;

PS Don't use MDY or DMY order for dates. Which ever one you pick will confuse half of your global audience.

 

Result:

NAME-1,"RECORD""1",20.83,2017-10-17
NAME-2,"RECO,RD""2",9.86,2018-05-30
NAME-3,"RECORD""3",5.68,2012-08-05
NAME-4,"REC,ORD""4",3.21,2012-06-01
NAME-5,"RECORD""5",465.37,2014-10-03

 

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 4 replies
  • 973 views
  • 0 likes
  • 4 in conversation