BookmarkSubscribeRSS Feed
ADN
Fluorite | Level 6 ADN
Fluorite | Level 6

I have a SAS dataset which I want to export as csv with each value enclosed around double quotes("). 

Now one of the values is as Arijit "Debnath.

 

Currently the ds2csv macro exports it as "Arijit ""Debnath"

I want it to be exported as "Arijit \"Debnath".

 

Is there any way to achieve the same.

6 REPLIES 6
Mazi
Pyrite | Level 9

Hi, 

Can you send a sample of the dataset and the macro test please?

Patrick
Opal | Level 21

If I save an Excel sheet as .csv with a cell value of Arijit "Debnath then I see in the .csv text file opened via a text editor a string "Arijit ""Debnath"

If I use Proc Export to create this .csv then the string in the created .csv text file becomes "Arijit ""Debnath"

 

If I create a .csv via a text editor and add string "Arijit \"Debnath" and open this .csv via Excel then the cell value shows as Arijit \Debnath"

 

I'm not sure what you're trying to achieve but it looks to me the valid csv representation of your source string should be "Arijit ""Debnath" which is what Proc Export creates.

yabwon
Onyx | Level 15

The \" looks like escape for double quotes in JSON string.

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



Patrick
Opal | Level 21

@yabwon wrote:

The \" looks like escape for double quotes in JSON string.


The OP mentioned explicitly .csv which is why I ignored the possibility that this might be an escape character.

yabwon
Onyx | Level 15

something like this may help:

data have;
  a=42;
  b='Arijit "Debnath';
  c=1000000;
  length d $ 50;
  d="ABCDEFGH";
run;

filename f "%sysfunc(pathname(work))/test.csv";

%ds2csv(csvfref=f,data=have,runmode=B)

data _null_;
  infile f SHAREBUFFERS;
  file f;
  input;
  _infile_ = tranwrd(_infile_,' ""',' \"');
  put _infile_;
run;

[EDIT:]

I assume there is a space before "" in the string, so the situation like:

"ABC""DEF"

or 

"""ABCD"

won't be solved.

 

[EDIT2:]

Small update to fix leading or trailing  multiple quotes  like: """AB" or "AB"""":

data have;
  a=42;
  b='Arijit "Debnath';
  c=1000000;
  length d $ 50;
  d="ABCD""EFGH";
  e="""ABCDEFGH";
  f="ABCDEFGH""";
run;

filename f "%sysfunc(pathname(work))/test.csv";

%ds2csv(csvfref=f,data=have,runmode=B)

data _null_;
  infile f SHAREBUFFERS;
  file f;
  input;
  _infile_ = tranwrd(_infile_,'"""','"\"');
  _infile_ = tranwrd(_infile_,'""','\"');
  put _infile_;
run;

 

 

 

 

Bart

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



Tom
Super User Tom
Super User

@ADN wrote:

I have a SAS dataset which I want to export as csv with each value enclosed around double quotes("). 

Now one of the values is as Arijit "Debnath.

 

Currently the ds2csv macro exports it as "Arijit ""Debnath"

I want it to be exported as "Arijit \"Debnath".

 

Is there any way to achieve the same.


Sounds like your target system wants some strange (unix shell based perhaps) variant of CSV.  I would first check if there is not an option in that system that will allow it to accept a NORMAL CSV file with normal handling of double quote characters.   Most databases that accept that strange format also have options to accept normal CSV files instead.

 

You could also go find the SAS Ballot item I proposed many many years ago for SAS to enhance their support for delimited file generation (actually don't waste your time as they already said they didn't want to do it.)

 

If not then you might be able to post process the file and just replace any "" with \".  So if the lines are not longer than 32K you could do something like:

data _null_;
  infile 'orginal.csv' ;
  file 'adjusted.csv';
  input;
  _infile_=tranwrd(_infile_,'""','\"');
  put _infile_;
run;

Here is a more complicated data step that should be able to transform any CSV into that non-standard format.  Basically it reads each field value.  It it starts with a double quote it removes the outer quotes and replaces the doubled up imbedded quotes with "escaped" quotes instead and then adds the outer quotes back.

 

 You can add LRECL= options on the FILE and INFILE statement to support files with lines longer then 32767 bytes.  You also need to select length for the VALUE variable that is longer than the maximum field values width.  For this example I have used 200 bytes, but SAS can support up to 32767.   (For best performance don't use too long a value, SAS string operations really slow down with long strings.)

data _null_;
  infile csv dsd truncover column=cc length=ll ;
  file notcsv ;
  do field=1 by 1 until (cc>ll);
    input value ~ :$200. @;
    if value=:'"' then value=cat('"',tranwrd(substr(value,2,length(value)-2),'""','\"'),'"');
    len=lengthn(value);
    if field>1 then put ',' @;
    put value $varying200. len @;
  end;
  put;
run;

 

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 6 replies
  • 950 views
  • 1 like
  • 5 in conversation