<?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: Export to CSV with spaces for empty strings in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Export-to-CSV-with-spaces-for-empty-strings/m-p/692890#M211176</link>
    <description>&lt;P&gt;SAS stores character strings as fixed length.&amp;nbsp; So values are padded with spaces to their full assigned length.&amp;nbsp; It will ignore trailing spaces in comparisons and treat a string of all spaces as "missing".&amp;nbsp; So your DO loop is doing nothing.&lt;/P&gt;</description>
    <pubDate>Tue, 20 Oct 2020 14:32:25 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2020-10-20T14:32:25Z</dc:date>
    <item>
      <title>Export to CSV with spaces for empty strings</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Export-to-CSV-with-spaces-for-empty-strings/m-p/692836#M211150</link>
      <description>&lt;P&gt;I need to export a table to csv having all cells filled either with data either with space, but SAS does not keep character field with single space and on export I receive:&lt;/P&gt;&lt;P&gt;"","","A","5"&lt;/P&gt;&lt;P&gt;instead of wanted:&lt;/P&gt;&lt;P&gt;" "," ","A","5"&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The table has more than 500 columns so defining format would be a bad idea.&lt;/P&gt;</description>
      <pubDate>Tue, 20 Oct 2020 12:30:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Export-to-CSV-with-spaces-for-empty-strings/m-p/692836#M211150</guid>
      <dc:creator>i_Van</dc:creator>
      <dc:date>2020-10-20T12:30:31Z</dc:date>
    </item>
    <item>
      <title>Re: Export to CSV with spaces for empty strings</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Export-to-CSV-with-spaces-for-empty-strings/m-p/692878#M211169</link>
      <description>&lt;P&gt;Please at least show how you are generating the CSV file. What you show is not typical for many CSV files.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And why is a format a bad idea?&lt;/P&gt;</description>
      <pubDate>Tue, 20 Oct 2020 14:15:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Export-to-CSV-with-spaces-for-empty-strings/m-p/692878#M211169</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2020-10-20T14:15:25Z</dc:date>
    </item>
    <item>
      <title>Re: Export to CSV with spaces for empty strings</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Export-to-CSV-with-spaces-for-empty-strings/m-p/692884#M211172</link>
      <description>&lt;P&gt;the initial dataset is downloaded from oracle database, (the table has more than 500 columns and I'm not sure that no new columns would be inserted - so that's why I think that directly insert FORMAT is not a good idea) then some magic and I need to export the resulting dataset.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm using:&lt;/P&gt;&lt;PRE&gt;%ds2csv(data=Basis_upd, runmode=b, sepchar=3B, csvfile=&amp;amp;export_dir.\&amp;amp;date_exp._Basis_update.csv);&lt;/PRE&gt;&lt;P&gt;to fill in empty fields I've tried&lt;/P&gt;&lt;PRE&gt;data Basis_upd;
   set Basis_upd;
   array change $ _character_;
        do over change;
            if missing(change) then change=' ';
        end;
 run ;&lt;/PRE&gt;&lt;P&gt;that code correctly fills in empty cells if I use then change='tst';&amp;nbsp;, but if I try to use then change='&amp;lt;space&amp;gt;'; the&amp;nbsp;cell remains empty,&amp;nbsp;not&amp;nbsp;holding single space.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 20 Oct 2020 14:23:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Export-to-CSV-with-spaces-for-empty-strings/m-p/692884#M211172</guid>
      <dc:creator>i_Van</dc:creator>
      <dc:date>2020-10-20T14:23:30Z</dc:date>
    </item>
    <item>
      <title>Re: Export to CSV with spaces for empty strings</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Export-to-CSV-with-spaces-for-empty-strings/m-p/692889#M211175</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/208885"&gt;@i_Van&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;I need to export a table to csv having all cells filled either with data either with space, but SAS does not keep character field with single space and on export I receive:&lt;/P&gt;
&lt;P&gt;"","","A","5"&lt;/P&gt;
&lt;P&gt;instead of wanted:&lt;/P&gt;
&lt;PRE&gt;,,A,5&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The table has more than 500 columns so defining format would be a bad idea.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Please explain more about why you need this (how are you planning to use the resulting file) and what methods you tried to generate the file.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In a normal CSV file quotes are only needed around values that contain the delimiter or the quote character.&amp;nbsp; So a line to represent those four values would look like this:&lt;/P&gt;
&lt;PRE&gt;,,A,5&lt;/PRE&gt;
&lt;P&gt;You can force SAS to add quotes around values by using the ~ modifier in the PUT statement.&lt;/P&gt;
&lt;P&gt;Example:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  infile cards dsd truncover ;
  length var1-var4 $10;
  input var1-var4 ;
cards;
,,A,5
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;217   data _null_;
218     set have;
219     file log dsd;
220     put (var1-var4) (~) ;
221   run;

" "," ","A","5"&lt;/PRE&gt;</description>
      <pubDate>Tue, 20 Oct 2020 14:30:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Export-to-CSV-with-spaces-for-empty-strings/m-p/692889#M211175</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-10-20T14:30:00Z</dc:date>
    </item>
    <item>
      <title>Re: Export to CSV with spaces for empty strings</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Export-to-CSV-with-spaces-for-empty-strings/m-p/692890#M211176</link>
      <description>&lt;P&gt;SAS stores character strings as fixed length.&amp;nbsp; So values are padded with spaces to their full assigned length.&amp;nbsp; It will ignore trailing spaces in comparisons and treat a string of all spaces as "missing".&amp;nbsp; So your DO loop is doing nothing.&lt;/P&gt;</description>
      <pubDate>Tue, 20 Oct 2020 14:32:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Export-to-CSV-with-spaces-for-empty-strings/m-p/692890#M211176</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-10-20T14:32:25Z</dc:date>
    </item>
    <item>
      <title>Re: Export to CSV with spaces for empty strings</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Export-to-CSV-with-spaces-for-empty-strings/m-p/692919#M211182</link>
      <description>&lt;P&gt;There is no need to use a macro to write a delimited text file.&amp;nbsp; Just write your own data step(s) and then you can have full control.&amp;nbsp; So first get a list of the variables.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc contents data=Basis_upd noprint out=contents; run;
proc sort; by varnum; run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Then you can use that data to write the header line.&amp;nbsp; If you want to add quotes around both the numeric and character variables then just use _all_ variable list:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;put (_all_) (~) ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;And if you only want it to add the quotes around the character variables you can also use that data to generate the PUT statement with the ~ modifiers.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Example:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;filename csv "&amp;amp;export_dir.\&amp;amp;date_exp._Basis_update.csv";
filename putstmt temp;
data _null_;
   set contents end=eof;
   file csv dsd dlm=';' lrecl=1000000 ;
   put name ~ @;
   file putstmt lrecl=75 ;
   if _n_=1 then put 'put ' @;
   put name @ ;
   if type=2 then put '~ ' @ ;
   if eof then put ';'
 run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Then you can use another data step to write the lines of data.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
   set Basis_upd ;
   file csv dsd dlm=';' lrecl=1000000 mod;
 %include putstmt ;
 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;</description>
      <pubDate>Tue, 20 Oct 2020 15:17:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Export-to-CSV-with-spaces-for-empty-strings/m-p/692919#M211182</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-10-20T15:17:11Z</dc:date>
    </item>
    <item>
      <title>Re: Export to CSV with spaces for empty strings</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Export-to-CSV-with-spaces-for-empty-strings/m-p/692921#M211183</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;Please explain more about why you need this (how are you planning to use the resulting file) and what methods you tried to generate the file.&lt;P&gt;In a normal CSV file quotes are only needed around values that contain the delimiter or the quote character.&lt;/P&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;The "perfect" format of CSV&amp;nbsp;is required for further data transfer - for upload to next system. I just have demand to store each value in double&amp;nbsp;quotes and fill all blank strings with single space.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you - the variant below worked, can you kindly help how to export column names? the code below exports just data without first row having column names&lt;/P&gt;&lt;PRE&gt;data _null_;
    set Basis_upd;
	file "&amp;amp;export_dir.&amp;amp;date_exp.\&amp;amp;date_exp._Basis_update_.csv" dlm='3B'x dsd ;
    put (AUSPLATZ_UNTERSICHERH_ID--KOMMENTAR) (~) ;
   run;&lt;/PRE&gt;</description>
      <pubDate>Tue, 20 Oct 2020 15:16:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Export-to-CSV-with-spaces-for-empty-strings/m-p/692921#M211183</guid>
      <dc:creator>i_Van</dc:creator>
      <dc:date>2020-10-20T15:16:14Z</dc:date>
    </item>
    <item>
      <title>Re: Export to CSV with spaces for empty strings</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Export-to-CSV-with-spaces-for-empty-strings/m-p/692928#M211190</link>
      <description>&lt;P&gt;I am interested in learning what tools are requiring such formatting, especially if it is a popular tool like a database or other analysis software.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;See my other post with details of how to get the names.&amp;nbsp; If you want to add quotes around all of the variables then you can simplify by using PROC TRANSPOSE to get the names.&amp;nbsp; But that will not give you the type of the variables.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc transpose data=Basis_upd(obs=0) out=names; run;
filename csv "&amp;amp;export_dir.&amp;amp;date_exp.\&amp;amp;date_exp._Basis_update_.csv";
data _null_;
  set names;
  file csv dlm=';' dsd lrecl=1000000 ;
  put _name_ ~ @ ;
run;

data _null_;
  set Basis_upd;
  file csv dlm=';' dsd  mod lrecl=1000000 ;
  put (_all_) (~);
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 20 Oct 2020 16:11:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Export-to-CSV-with-spaces-for-empty-strings/m-p/692928#M211190</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-10-20T16:11:42Z</dc:date>
    </item>
  </channel>
</rss>

