<?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 Export ASCII file with double quotes in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Export-ASCII-file-with-double-quotes/m-p/736577#M229488</link>
    <description>&lt;P&gt;Hello.. I need an ASCII delimited output file from my program, with every variable enclosed in "" double quotes regardless of the contents.&amp;nbsp; Depending on the scenario, some variables can be null and should output as "".&amp;nbsp; I've searched around the forum but most people want to remove the quotes, my customer needs them in permanently:&amp;nbsp;&lt;/P&gt;&lt;P&gt;e.g.&lt;/P&gt;&lt;P&gt;"999999","","10","01NOV2020","BILI"&lt;/P&gt;&lt;P&gt;"999999","","20","05NOV2020","BILI"&lt;/P&gt;&lt;P&gt;"999999","","30","10NOV2020","BILI"&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;My datasets are built dynamically inside a macro based on some other input handled externally and because of this %ds2csv wouldn't execute (&lt;EM&gt;something to do with SCL&lt;/EM&gt;).&amp;nbsp; &amp;nbsp;&lt;SPAN style="font-family: inherit;"&gt;Anyway, I finally came up with this, which is working from my macro but is just about 95% of what I need:&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%Let outfile = '\\MyServerPath/export.dat' DLM = ',' DSD;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;DATA&lt;/STRONG&gt; _NULL_;&lt;/P&gt;&lt;P&gt;&amp;nbsp; SET WORK.OUT2_LAB;&lt;/P&gt;&lt;P&gt;&amp;nbsp; FILE &amp;amp;outfile;&lt;/P&gt;&lt;P&gt;&amp;nbsp; put (_ALL_) (~);&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;RUN&lt;/STRONG&gt;;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;With this code, null variables contain a space e.g. " " instead of "".&amp;nbsp; Is there anything I can do to adjust the above code just trim empty spaces but keep everything else the same?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So it generates this:&lt;/P&gt;&lt;P&gt;"999999",&lt;FONT color="#008000"&gt;""&lt;/FONT&gt;,"10","01NOV2020","BILI"&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;And not this:&lt;/P&gt;&lt;P&gt;"999999",&lt;FONT color="#FF0000"&gt;" "&lt;/FONT&gt;,"10","01NOV2020","BILI"&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 23 Apr 2021 11:52:50 GMT</pubDate>
    <dc:creator>Sha88</dc:creator>
    <dc:date>2021-04-23T11:52:50Z</dc:date>
    <item>
      <title>Export ASCII file with double quotes</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Export-ASCII-file-with-double-quotes/m-p/736577#M229488</link>
      <description>&lt;P&gt;Hello.. I need an ASCII delimited output file from my program, with every variable enclosed in "" double quotes regardless of the contents.&amp;nbsp; Depending on the scenario, some variables can be null and should output as "".&amp;nbsp; I've searched around the forum but most people want to remove the quotes, my customer needs them in permanently:&amp;nbsp;&lt;/P&gt;&lt;P&gt;e.g.&lt;/P&gt;&lt;P&gt;"999999","","10","01NOV2020","BILI"&lt;/P&gt;&lt;P&gt;"999999","","20","05NOV2020","BILI"&lt;/P&gt;&lt;P&gt;"999999","","30","10NOV2020","BILI"&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;My datasets are built dynamically inside a macro based on some other input handled externally and because of this %ds2csv wouldn't execute (&lt;EM&gt;something to do with SCL&lt;/EM&gt;).&amp;nbsp; &amp;nbsp;&lt;SPAN style="font-family: inherit;"&gt;Anyway, I finally came up with this, which is working from my macro but is just about 95% of what I need:&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%Let outfile = '\\MyServerPath/export.dat' DLM = ',' DSD;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;DATA&lt;/STRONG&gt; _NULL_;&lt;/P&gt;&lt;P&gt;&amp;nbsp; SET WORK.OUT2_LAB;&lt;/P&gt;&lt;P&gt;&amp;nbsp; FILE &amp;amp;outfile;&lt;/P&gt;&lt;P&gt;&amp;nbsp; put (_ALL_) (~);&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;RUN&lt;/STRONG&gt;;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;With this code, null variables contain a space e.g. " " instead of "".&amp;nbsp; Is there anything I can do to adjust the above code just trim empty spaces but keep everything else the same?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So it generates this:&lt;/P&gt;&lt;P&gt;"999999",&lt;FONT color="#008000"&gt;""&lt;/FONT&gt;,"10","01NOV2020","BILI"&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;And not this:&lt;/P&gt;&lt;P&gt;"999999",&lt;FONT color="#FF0000"&gt;" "&lt;/FONT&gt;,"10","01NOV2020","BILI"&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 23 Apr 2021 11:52:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Export-ASCII-file-with-double-quotes/m-p/736577#M229488</guid>
      <dc:creator>Sha88</dc:creator>
      <dc:date>2021-04-23T11:52:50Z</dc:date>
    </item>
    <item>
      <title>Re: Export ASCII file with double quotes</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Export-ASCII-file-with-double-quotes/m-p/736578#M229489</link>
      <description>&lt;P&gt;You could post process the file.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  infile cards dsd truncover ;
  input (v1-v5) (:$30.);
cards;
"999999","","10","01NOV2020","BILI"
"999999","","20","05NOV2020","BILI"
"999999","","30","10NOV2020","BILI"
;

filename original temp;
data _null_;
 set have;
 file original dsd ;
 put (v1-v5) (~) ;
run;

data _null_;
  infile original;
  input;
  put _infile_;
run;

filename want temp;
data _null_;
 infile original   ;
 file want ;
 input ;
 _infile_=tranwrd(cats(',',_infile_,','),'," ",',',"",');
 _infile_=substrn(_infile_,2,length(_infile_)-2);
 put _infile_;
run;

data _null_;
  infile want;
  input;
  put _infile_;
run;
 &lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;"999999","","10","01NOV2020","BILI"
"999999","","20","05NOV2020","BILI"
"999999","","30","10NOV2020","BILI"
&lt;/PRE&gt;</description>
      <pubDate>Fri, 23 Apr 2021 12:19:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Export-ASCII-file-with-double-quotes/m-p/736578#M229489</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-04-23T12:19:42Z</dc:date>
    </item>
    <item>
      <title>Re: Export ASCII file with double quotes</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Export-ASCII-file-with-double-quotes/m-p/736701#M229553</link>
      <description>&lt;P&gt;And what should happen if there is a double quote in your data? Or is that never going to happen?&lt;/P&gt;</description>
      <pubDate>Sat, 24 Apr 2021 01:35:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Export-ASCII-file-with-double-quotes/m-p/736701#M229553</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2021-04-24T01:35:06Z</dc:date>
    </item>
    <item>
      <title>Re: Export ASCII file with double quotes</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Export-ASCII-file-with-double-quotes/m-p/736722#M229562</link>
      <description>The system generating the data wouldn’t allow it.</description>
      <pubDate>Sat, 24 Apr 2021 07:14:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Export-ASCII-file-with-double-quotes/m-p/736722#M229562</guid>
      <dc:creator>Sha88</dc:creator>
      <dc:date>2021-04-24T07:14:25Z</dc:date>
    </item>
    <item>
      <title>Re: Export ASCII file with double quotes</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Export-ASCII-file-with-double-quotes/m-p/736786#M229602</link>
      <description>&lt;P&gt;If you only have character variables you could use a user defined format to add the quotes.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  infile cards dsd truncover;
  input (v1-v5) (:$30.);
cards;
"999999","","10","01NOV2020","BILI"
"999999","","20","05NOV2020","BILI"
"999999","","30","10NOV2020","BILI"
;

proc format ;
  value $q(max=200) ' '='""' other=[$quote.] ;
run;

data _null_;
  set have ;
  file &amp;amp;outfile dlm=',' ;
  put (_all_) (:$q200.);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Results:&lt;/P&gt;
&lt;PRE&gt;"999999","","10","01NOV2020","BILI"
"999999","","20","05NOV2020","BILI"
"999999","","30","10NOV2020","BILI"
&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;If the data has mixed numeric and character variables or variables that have formats attached then use an intermediate text file.&amp;nbsp; If there are a lot of variables you might also need to set the logical record length longer then the default of 32K bytes.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  infile cards dsd truncover;
  input v1 v2 (v3-v5) (:$30.);
cards;
"999999","","10","01NOV2020","BILI"
"999999","","20","05NOV2020","BILI"
"999999","","30","10NOV2020","BILI"
;

proc format ;
  value $q(max=200) ' '='""' other=[$quote.] ;
run;

filename step1 temp;
data _null_;
  set have ;
  file step1 dsd lrecl=1000000;
  put (_all_) (+0) ;
run;

data _null_;
  infile step1 dsd length=ll column=cc truncover lrecl=1000000;
  file &amp;amp;outfile dlm=',' lrecl=1000000;
  do until(cc&amp;gt;ll);
    input value :$200. @;
    put value :$q200. @;
  end;
  put;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 24 Apr 2021 21:55:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Export-ASCII-file-with-double-quotes/m-p/736786#M229602</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-04-24T21:55:30Z</dc:date>
    </item>
    <item>
      <title>Re: Export ASCII file with double quotes</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Export-ASCII-file-with-double-quotes/m-p/736977#M229703</link>
      <description>This is working, but it’s cutting off the variables at 38 characters. Any idea why might do that? Set LRECL as above.</description>
      <pubDate>Mon, 26 Apr 2021 13:00:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Export-ASCII-file-with-double-quotes/m-p/736977#M229703</guid>
      <dc:creator>Sha88</dc:creator>
      <dc:date>2021-04-26T13:00:36Z</dc:date>
    </item>
    <item>
      <title>Re: Export ASCII file with double quotes</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Export-ASCII-file-with-double-quotes/m-p/736981#M229706</link>
      <description>&lt;P&gt;You need to show the code you ran to see what could be truncating values.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Most likely you are using a character variable of length 38 (or possibly using a format with a width of 38).&amp;nbsp; &amp;nbsp;If you do not explicitly define your variables then SAS must guess how to define them based on how you first use them.&lt;/P&gt;</description>
      <pubDate>Mon, 26 Apr 2021 13:12:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Export-ASCII-file-with-double-quotes/m-p/736981#M229706</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-04-26T13:12:09Z</dc:date>
    </item>
    <item>
      <title>Re: Export ASCII file with double quotes</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Export-ASCII-file-with-double-quotes/m-p/737037#M229726</link>
      <description>&lt;P&gt;Hi sure, sharing below.&amp;nbsp; &amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So I tried both approaches (post processing and not) but both truncate a $200. char field down to 38 only during export.&amp;nbsp; If it takeaway the new format, it exports without truncation but obviously is then missing the desired quotes "".&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In the WORK.OUT2_LAB dataset all var's are character and lengths all set, they range from $10. to $200. but none have $38.&amp;nbsp; Tempfile is not truncated.&amp;nbsp; Here is what I ran separately:&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%LET OutFileQ = &amp;amp;OFP.\&amp;amp;OutFileMask..&amp;amp;OutExtension.;
%LET TempFile = &amp;amp;OFP.\TempLab.txt;

PROC FORMAT;
  VALUE $q(MAX=200) ' '='""' other=[$quote.] ;
RUN;

FILENAME step1 "&amp;amp;TempFile";

DATA _NULL_;
	SET WORK.OUT2_LAB;
	FILE step1 DSD LRECL=10000;
	PUT (_ALL_) (+0);
RUN;

DATA _NULL_;
	INFILE step1 DSD LENGTH=LL COLUMN=CC TRUNCOVER LRECL=10000;
	FILE "&amp;amp;OutFileQ." DLM=',' LRECL=10000;
	DO UNTIL (CC&amp;gt;LL);
		INPUT VALUE :$200. @;
		PUT VALUE :$q200. @;
	END;
	PUT;
RUN;
/*File ^^ max output length 38.. LBTSTCOM truncated in output but not infile.*/

/*Separate just the below with the proc format*/
DATA WORK.TEST1;
	SET WORK.OUT2_LAB;
	FILE "&amp;amp;OutFileQ." DLM=',';
	PUT (_ALL_) (:$q200.);
RUN;
/*File ^^ max output length 38.. LBTSTCOM truncated in export but not OUT2_LAB.*/&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Thanks for the assistance.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 26 Apr 2021 16:14:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Export-ASCII-file-with-double-quotes/m-p/737037#M229726</guid>
      <dc:creator>Sha88</dc:creator>
      <dc:date>2021-04-26T16:14:45Z</dc:date>
    </item>
    <item>
      <title>Re: Export ASCII file with double quotes</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Export-ASCII-file-with-double-quotes/m-p/737038#M229727</link>
      <description>Or should I do $quote200. in the PROC FORMAT?</description>
      <pubDate>Mon, 26 Apr 2021 16:18:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Export-ASCII-file-with-double-quotes/m-p/737038#M229727</guid>
      <dc:creator>Sha88</dc:creator>
      <dc:date>2021-04-26T16:18:32Z</dc:date>
    </item>
    <item>
      <title>Re: Export ASCII file with double quotes</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Export-ASCII-file-with-double-quotes/m-p/737050#M229735</link>
      <description>&lt;P&gt;Yes.&amp;nbsp; The WIDTH from the $Q format is NOT being passed to the $QUOTE format call.&amp;nbsp; Instead it is defaulting to $QUOTE40. which means there is room for only 38 characters.&amp;nbsp; So include the width in the nested format in the defintion.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;PROC FORMAT;
  VALUE $Q(MAX=32767) ' '='""' other=[$quote32767.] ;
RUN;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But you might also be truncating the total line length.&amp;nbsp;&lt;STRONG&gt;Why did you include a LRECL of 10,000?&amp;nbsp; That is SHORTER than the default of 32,767?&amp;nbsp;&lt;/STRONG&gt; In my example I use a LRECL or 1,000,000 to show that you could use values larger than the default.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But if your variables have the wrong format then the PUT statement will truncate, no matter what you do if you let SAS use those formats for printing.&amp;nbsp;&amp;nbsp;For example try this program:&lt;/P&gt;
&lt;PRE&gt;1671  data test;
1672    x='This is a long string';
1673    format x $10.;
1674    put x;
1675  run;

This is a&lt;/PRE&gt;
&lt;P&gt;The maximum length for a character variable is 32,767 bytes.&amp;nbsp;So if the total width of the generated lines is less than this then the simple postprocessing using the _INFILE_ variable will work.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%LET OutFileQ = &amp;amp;OFP.\&amp;amp;OutFileMask..&amp;amp;OutExtension.;
%LET TempFile = &amp;amp;OFP.\TempLab.txt;
FILENAME step1 "&amp;amp;TempFile";
FILENAME OUTFILE "&amp;amp;outfileq";

DATA _NULL_;
  SET WORK.OUT2_LAB;
  FILE step1 DSD LRECL=32767;
  PUT (_ALL_) (~);
RUN;
DATA _NULL_;
  INFILE step1 LRECL=32767;
  FILE outfile LRECL=32767;
  INPUT;
  _infile_=tranwrd(cats(',',_infile_,','),'," ",',',"",');
  _infile_=tranwrd(_infile_,',".",',',"",');
  _infile_=substr(_infile_,2,length(_infile_)-2);
  PUT _infile_;
RUN;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you want to use the other method then use something larger than 200 if some of your variables could require that.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;PROC FORMAT;
  VALUE $Q(MAX=32767) ' '='""' other=[$quote32767.] ;
RUN;
DATA _NULL_;
  SET WORK.OUT2_LAB;
  FILE step1 DSD LRECL=2000000;
  PUT (_ALL_) (+0);
RUN;

DATA _NULL_;
  INFILE step1 DSD LENGTH=LL COLUMN=CC TRUNCOVER LRECL=2000000;
  FILE "&amp;amp;OutFileQ." DLM=',' LRECL=2000000;
  DO UNTIL (CC&amp;gt;LL);
    INPUT VALUE :$32765. @;
    PUT VALUE :$Q32767. @;
  END;
  PUT;
RUN;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;NOTE: That printing with the $QUOTE format the width specified counts the quotes. So $QUOTE10. will print the first 8 characters of the string.&lt;/P&gt;</description>
      <pubDate>Mon, 26 Apr 2021 16:53:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Export-ASCII-file-with-double-quotes/m-p/737050#M229735</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-04-26T16:53:45Z</dc:date>
    </item>
  </channel>
</rss>

