<?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 a dataset to csv inserting the delimiter at the end of each row in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Export-a-dataset-to-csv-inserting-the-delimiter-at-the-end-of/m-p/986624#M379974</link>
    <description>&lt;P&gt;If you are using the DSD option then the way to add an extra delimiter is to write a missing value.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt; 73         data _null_;
 74           file log dsd dlm=';';
 75           set sashelp.class(obs=3);
 76           _n_=.;
 77           put (_all_ _n_) (:) ;
 78         run;
 
 Alfred;M;14;69;112.5;
 Alice;F;13;56.5;84;
 Barbara;F;13;65.3;98;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The simplest way to get the list of names is using PROC TRANSPOSE with zero observations.&amp;nbsp; Then you can use that list to generate the header row.&amp;nbsp; You can then use the MOD option on the FILE statement to append the data lines.&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;data test; set sashelp.class(obs=3); run;

proc transpose data=test(obs=0) out=names;
  var _all_;
run;

filename csv temp;
data _null_;
  set names end=eof;
  file csv dsd dlm=';';
  put _name_ @;
  if eof then do; _n_=.; put _n_; end;
run;
data _null_;
  file csv mod dsd dlm=';';
  set test ;
  _n_=.;
  put (_all_ _n_) (:) ;
run;

data _null_;
  infile csv;
  input;
  put _infile_;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result:&lt;/P&gt;
&lt;PRE&gt;Name;Sex;Age;Height;Weight;
Alfred;M;14;69;112.5;
Alice;F;13;56.5;84;
Barbara;F;13;65.3;98;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 21 Apr 2026 18:43:39 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2026-04-21T18:43:39Z</dc:date>
    <item>
      <title>Export a dataset to csv inserting the delimiter at the end of each row</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Export-a-dataset-to-csv-inserting-the-delimiter-at-the-end-of/m-p/986584#M379966</link>
      <description>&lt;P&gt;The question is very simple but I cannot find a working solution: &lt;STRONG&gt;given a generic dataset how to create a csv with header and data rows ending with the delimiter&lt;/STRONG&gt;?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Usually to export a dataset to a csv I use this code&lt;/P&gt;
&lt;DIV&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let lib=WORK;
%let mem=have;
%let outfile=;

data have;
input col1 col2 $2.; cards;
1 ab
3 cv
run;

proc export&amp;nbsp;
data=have
dbms=dlm
outfile="&amp;amp;outfile.";
delimiter=";";
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;/DIV&gt;
&lt;DIV&gt;and then the csv contains&lt;/DIV&gt;
&lt;DIV&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;col1;col2
1;ab
3;cv&lt;/CODE&gt;&lt;/PRE&gt;
&lt;/DIV&gt;
&lt;DIV&gt;However, what if we need to place the delimiter at the end of each row too? That is, to produce the following&amp;nbsp;&lt;/DIV&gt;
&lt;DIV&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;col1;col2;
1;ab;
3;cv;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;/DIV&gt;
&lt;DIV&gt;I tried using put in a data step, but while it works for the header row, it inserts an extra whitespace before the delimiter in the data rows. Moreover, it is not good since you have to manually write the column names in the put statement.&lt;/DIV&gt;
&lt;DIV&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* Build the header string in the same order as variables appear in the dataset */
proc sql;
select name into :header separated by ';'
from dictionary.columns
where libname = "%upcase(&amp;amp;lib)" and memname = "%upcase(&amp;amp;mem)"
order by varnum;
quit;

/* Write data to csv */
data _null_;
set have;
file "&amp;amp;outfile." lrecl=32767;
if _n_=1 then put "&amp;amp;header.;";
put col1 ';' col2 ';';
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;/DIV&gt;
&lt;DIV&gt;output&lt;/DIV&gt;
&lt;DIV&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;col1;col2;
1 ;ab ;
3 ;cv ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;/DIV&gt;
&lt;DIV&gt;AI suggested this code&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* Write header line, with trailing semicolon */
data _null_;
file "&amp;amp;outfile" lrecl=32767 encoding="utf-8";
put "&amp;amp;header.;"; /* &amp;lt;-- trailing ; */
run;

/* Append data rows, each ending with a trailing semicolon */
data _null_;
set &amp;amp;lib..&amp;amp;mem;
file "&amp;amp;outfile" mod dsd dlm=';' lrecl=32767 encoding="utf-8";
put (_all_) (:+0) ';'; /* &amp;lt;-- trailing ; */
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;which however places two delimiters at the end of each data row&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;col1;col2;
1;ab;;
3;cv;;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;AI then tried to fix it, but it is not able to find a way.&lt;/P&gt;
&lt;P&gt;Any suggestion?&lt;/P&gt;
&lt;/DIV&gt;</description>
      <pubDate>Tue, 21 Apr 2026 09:07:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Export-a-dataset-to-csv-inserting-the-delimiter-at-the-end-of/m-p/986584#M379966</guid>
      <dc:creator>Rabelais</dc:creator>
      <dc:date>2026-04-21T09:07:16Z</dc:date>
    </item>
    <item>
      <title>Re: Export a dataset to csv inserting the delimiter at the end of each row</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Export-a-dataset-to-csv-inserting-the-delimiter-at-the-end-of/m-p/986585#M379967</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let outfile=c:\temp\want.csv ;


data have;
input col1 col2 $2.; 
cards;
1 ab
3 cv
;
run;
 
/* Build the header string in the same order as variables appear in the dataset */
proc sql noprint;
select name into :header separated by ';'
from dictionary.columns
where libname = "WORK" and memname = "HAVE"
order by varnum;
quit;

/* Write data to csv */
data _null_;
set have;
file "&amp;amp;outfile." lrecl=32767 dlm=';' dsd;
if _n_=1 then put "&amp;amp;header.;";
put col1  col2 +(-1) ';';
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 21 Apr 2026 09:59:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Export-a-dataset-to-csv-inserting-the-delimiter-at-the-end-of/m-p/986585#M379967</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2026-04-21T09:59:59Z</dc:date>
    </item>
    <item>
      <title>Re: Export a dataset to csv inserting the delimiter at the end of each row</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Export-a-dataset-to-csv-inserting-the-delimiter-at-the-end-of/m-p/986588#M379968</link>
      <description>&lt;P&gt;Just to fix the output issue, you could use the of _all_ construct with catx:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
	set &amp;amp;Lib..&amp;amp;Mem;
	length out $1000;
	out = cats(catx(';', of _all_),';');
	file "&amp;amp;outfile" mod lrecl=32767 encoding="utf-8";
	put out;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 21 Apr 2026 10:39:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Export-a-dataset-to-csv-inserting-the-delimiter-at-the-end-of/m-p/986588#M379968</guid>
      <dc:creator>LinusH</dc:creator>
      <dc:date>2026-04-21T10:39:36Z</dc:date>
    </item>
    <item>
      <title>Re: Export a dataset to csv inserting the delimiter at the end of each row</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Export-a-dataset-to-csv-inserting-the-delimiter-at-the-end-of/m-p/986590#M379969</link>
      <description>Wow what sorcery is this! Many thanks!</description>
      <pubDate>Tue, 21 Apr 2026 10:52:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Export-a-dataset-to-csv-inserting-the-delimiter-at-the-end-of/m-p/986590#M379969</guid>
      <dc:creator>Rabelais</dc:creator>
      <dc:date>2026-04-21T10:52:44Z</dc:date>
    </item>
    <item>
      <title>Re: Export a dataset to csv inserting the delimiter at the end of each row</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Export-a-dataset-to-csv-inserting-the-delimiter-at-the-end-of/m-p/986624#M379974</link>
      <description>&lt;P&gt;If you are using the DSD option then the way to add an extra delimiter is to write a missing value.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt; 73         data _null_;
 74           file log dsd dlm=';';
 75           set sashelp.class(obs=3);
 76           _n_=.;
 77           put (_all_ _n_) (:) ;
 78         run;
 
 Alfred;M;14;69;112.5;
 Alice;F;13;56.5;84;
 Barbara;F;13;65.3;98;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The simplest way to get the list of names is using PROC TRANSPOSE with zero observations.&amp;nbsp; Then you can use that list to generate the header row.&amp;nbsp; You can then use the MOD option on the FILE statement to append the data lines.&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;data test; set sashelp.class(obs=3); run;

proc transpose data=test(obs=0) out=names;
  var _all_;
run;

filename csv temp;
data _null_;
  set names end=eof;
  file csv dsd dlm=';';
  put _name_ @;
  if eof then do; _n_=.; put _n_; end;
run;
data _null_;
  file csv mod dsd dlm=';';
  set test ;
  _n_=.;
  put (_all_ _n_) (:) ;
run;

data _null_;
  infile csv;
  input;
  put _infile_;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result:&lt;/P&gt;
&lt;PRE&gt;Name;Sex;Age;Height;Weight;
Alfred;M;14;69;112.5;
Alice;F;13;56.5;84;
Barbara;F;13;65.3;98;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 21 Apr 2026 18:43:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Export-a-dataset-to-csv-inserting-the-delimiter-at-the-end-of/m-p/986624#M379974</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2026-04-21T18:43:39Z</dc:date>
    </item>
  </channel>
</rss>

