<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: SAS Export data with double quotes escaped with backslash. in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/SAS-Export-data-with-double-quotes-escaped-with-backslash/m-p/920919#M362707</link>
    <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/138977"&gt;@ADN&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;I have a SAS dataset which I want to export as csv with each value enclosed around double quotes(").&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Now one of the values is as Arijit "Debnath.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Currently the ds2csv macro exports it as "Arijit ""Debnath"&lt;/P&gt;
&lt;P&gt;I want it to be exported as&amp;nbsp;"Arijit \"Debnath".&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Is there any way to achieve the same.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Sounds like your target system wants some strange (unix shell based perhaps) variant of CSV.&amp;nbsp; 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.&amp;nbsp; &amp;nbsp;Most databases that accept that strange format also have options to accept normal CSV files instead.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;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.)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If not then you might be able to post process the file and just replace any "" with \".&amp;nbsp; So if the lines are not longer than 32K you could do something like:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
  infile 'orginal.csv' ;
  file 'adjusted.csv';
  input;
  _infile_=tranwrd(_infile_,'""','\"');
  put _infile_;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Here is a more complicated data step that should be able to transform any CSV into that non-standard format.&amp;nbsp; Basically it reads each field value.&amp;nbsp; 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.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;You can add LRECL= options on the FILE and INFILE statement to support files with lines longer then 32767 bytes.&amp;nbsp; You also need to select length for the VALUE variable that is longer than the maximum field values width.&amp;nbsp; For this example I have used 200 bytes, but SAS can support up to 32767.&amp;nbsp; &amp;nbsp;(For best performance don't use too long a value, SAS string operations really slow down with long strings.)&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
  infile csv dsd truncover column=cc length=ll ;
  file notcsv ;
  do field=1 by 1 until (cc&amp;gt;ll);
    input value ~ :$200. @;
    if value=:'"' then value=cat('"',tranwrd(substr(value,2,length(value)-2),'""','\"'),'"');
    len=lengthn(value);
    if field&amp;gt;1 then put ',' @;
    put value $varying200. len @;
  end;
  put;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 19 Mar 2024 14:17:28 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2024-03-19T14:17:28Z</dc:date>
    <item>
      <title>SAS Export data with double quotes escaped with backslash.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-Export-data-with-double-quotes-escaped-with-backslash/m-p/920876#M362693</link>
      <description>&lt;P&gt;I have a SAS dataset which I want to export as csv with each value enclosed around double quotes(").&amp;nbsp;&lt;/P&gt;&lt;P&gt;Now one of the values is as Arijit "Debnath.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Currently the ds2csv macro exports it as "Arijit ""Debnath"&lt;/P&gt;&lt;P&gt;I want it to be exported as&amp;nbsp;"Arijit \"Debnath".&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Is there any way to achieve the same.&lt;/P&gt;</description>
      <pubDate>Tue, 19 Mar 2024 05:03:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-Export-data-with-double-quotes-escaped-with-backslash/m-p/920876#M362693</guid>
      <dc:creator>ADN</dc:creator>
      <dc:date>2024-03-19T05:03:28Z</dc:date>
    </item>
    <item>
      <title>Re: SAS Export data with double quotes escaped with backslash.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-Export-data-with-double-quotes-escaped-with-backslash/m-p/920881#M362696</link>
      <description>&lt;P&gt;Hi,&amp;nbsp;&lt;/P&gt;&lt;P&gt;Can you send a sample of the dataset and the macro test please?&lt;/P&gt;</description>
      <pubDate>Tue, 19 Mar 2024 06:51:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-Export-data-with-double-quotes-escaped-with-backslash/m-p/920881#M362696</guid>
      <dc:creator>Mazi</dc:creator>
      <dc:date>2024-03-19T06:51:48Z</dc:date>
    </item>
    <item>
      <title>Re: SAS Export data with double quotes escaped with backslash.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-Export-data-with-double-quotes-escaped-with-backslash/m-p/920895#M362698</link>
      <description>&lt;P&gt;If I save an Excel sheet as .csv with a cell value of&amp;nbsp;&lt;SPAN&gt;&lt;FONT color="#0000FF"&gt;Arijit "Debnath&lt;/FONT&gt; then I see in the .csv text file opened via a text editor a string&amp;nbsp;&lt;FONT color="#0000FF"&gt;"Arijit ""Debnath"&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#000000"&gt;&lt;SPAN&gt;If I use Proc Export to create this .csv then the string in the created .csv text file becomes&amp;nbsp;&lt;FONT color="#0000FF"&gt;"Arijit ""Debnath"&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#000000"&gt;&lt;SPAN&gt;&lt;FONT color="#0000FF"&gt;&lt;FONT color="#000000"&gt;If I create a .csv via a text editor and add string&lt;/FONT&gt;&amp;nbsp;"Arijit \"Debnath" &lt;FONT color="#000000"&gt;and open this .csv via Excel then the cell value shows as&lt;/FONT&gt;&amp;nbsp;Arijit \Debnath&lt;FONT color="#FF0000"&gt;"&lt;/FONT&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;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&amp;nbsp;&lt;SPAN&gt;&lt;FONT color="#0000FF"&gt;"Arijit ""Debnath" &lt;FONT color="#000000"&gt;which is what Proc Export creates.&lt;/FONT&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 19 Mar 2024 10:13:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-Export-data-with-double-quotes-escaped-with-backslash/m-p/920895#M362698</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2024-03-19T10:13:39Z</dc:date>
    </item>
    <item>
      <title>Re: SAS Export data with double quotes escaped with backslash.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-Export-data-with-double-quotes-escaped-with-backslash/m-p/920900#M362700</link>
      <description>&lt;P&gt;The \" looks like escape for double quotes in JSON string.&lt;/P&gt;</description>
      <pubDate>Tue, 19 Mar 2024 11:31:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-Export-data-with-double-quotes-escaped-with-backslash/m-p/920900#M362700</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2024-03-19T11:31:12Z</dc:date>
    </item>
    <item>
      <title>Re: SAS Export data with double quotes escaped with backslash.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-Export-data-with-double-quotes-escaped-with-backslash/m-p/920903#M362701</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/35763"&gt;@yabwon&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;The \" looks like escape for double quotes in JSON string.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;The OP mentioned explicitly .csv which is why I ignored the possibility that this might be an escape character.&lt;/P&gt;</description>
      <pubDate>Tue, 19 Mar 2024 11:37:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-Export-data-with-double-quotes-escaped-with-backslash/m-p/920903#M362701</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2024-03-19T11:37:02Z</dc:date>
    </item>
    <item>
      <title>Re: SAS Export data with double quotes escaped with backslash.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-Export-data-with-double-quotes-escaped-with-backslash/m-p/920905#M362702</link>
      <description>&lt;P&gt;something like this may help:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;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;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;[EDIT:]&lt;/P&gt;
&lt;P&gt;I assume there is a space before "" in the string, so the situation like:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;"ABC""DEF"

or 

"""ABCD"&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;won't be solved.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;[EDIT2:]&lt;/P&gt;
&lt;P&gt;Small update to fix leading or trailing&amp;nbsp; multiple quotes&amp;nbsp; like: """AB" or "AB"""":&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;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;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Bart&lt;/P&gt;</description>
      <pubDate>Tue, 19 Mar 2024 11:52:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-Export-data-with-double-quotes-escaped-with-backslash/m-p/920905#M362702</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2024-03-19T11:52:12Z</dc:date>
    </item>
    <item>
      <title>Re: SAS Export data with double quotes escaped with backslash.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-Export-data-with-double-quotes-escaped-with-backslash/m-p/920919#M362707</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/138977"&gt;@ADN&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;I have a SAS dataset which I want to export as csv with each value enclosed around double quotes(").&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Now one of the values is as Arijit "Debnath.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Currently the ds2csv macro exports it as "Arijit ""Debnath"&lt;/P&gt;
&lt;P&gt;I want it to be exported as&amp;nbsp;"Arijit \"Debnath".&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Is there any way to achieve the same.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Sounds like your target system wants some strange (unix shell based perhaps) variant of CSV.&amp;nbsp; 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.&amp;nbsp; &amp;nbsp;Most databases that accept that strange format also have options to accept normal CSV files instead.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;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.)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If not then you might be able to post process the file and just replace any "" with \".&amp;nbsp; So if the lines are not longer than 32K you could do something like:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
  infile 'orginal.csv' ;
  file 'adjusted.csv';
  input;
  _infile_=tranwrd(_infile_,'""','\"');
  put _infile_;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Here is a more complicated data step that should be able to transform any CSV into that non-standard format.&amp;nbsp; Basically it reads each field value.&amp;nbsp; 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.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;You can add LRECL= options on the FILE and INFILE statement to support files with lines longer then 32767 bytes.&amp;nbsp; You also need to select length for the VALUE variable that is longer than the maximum field values width.&amp;nbsp; For this example I have used 200 bytes, but SAS can support up to 32767.&amp;nbsp; &amp;nbsp;(For best performance don't use too long a value, SAS string operations really slow down with long strings.)&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
  infile csv dsd truncover column=cc length=ll ;
  file notcsv ;
  do field=1 by 1 until (cc&amp;gt;ll);
    input value ~ :$200. @;
    if value=:'"' then value=cat('"',tranwrd(substr(value,2,length(value)-2),'""','\"'),'"');
    len=lengthn(value);
    if field&amp;gt;1 then put ',' @;
    put value $varying200. len @;
  end;
  put;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 19 Mar 2024 14:17:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-Export-data-with-double-quotes-escaped-with-backslash/m-p/920919#M362707</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2024-03-19T14:17:28Z</dc:date>
    </item>
  </channel>
</rss>

