<?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: Exporting to CSV and Adding a line with labels in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Exporting-to-CSV-and-Adding-a-line-with-labels/m-p/432889#M107265</link>
    <description>&lt;P&gt;Since there is comma in the value as 'Age, Years', I suggest to change delimeter other than comma, for example, I use semi-comma in below codes:&lt;/P&gt;&lt;P&gt;1. add delimeter into quote function.&lt;/P&gt;&lt;P&gt;2. remove quote marks to call macro variables.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;*select names into a macro variable name_list;
*Select labels into a macro variable label_list;
proc sql noprint;
	select quote(trim(name)||';') into :name_list separated by ' '
		from sashelp.vcolumn
			where libname='WORK' and memname='CLASS' and not missing (name);
	select quote(trim(coalesce(label, name))||';')  into :label_list separated by ' '
		from sashelp.vcolumn
			where libname='WORK' and memname='CLASS';
quit;

*check macro variables;
%put Name= &amp;amp;name_list;
%put;
%put Label= &amp;amp;label_list;


data _null_;
	set class;
	file fout dlm=';' dsd;

	if _n_=1 then
		do;
			put &amp;amp;label_list;
			put &amp;amp;name_list;
		end;

	put (_all_)(:);
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Hope it will help&lt;/P&gt;</description>
    <pubDate>Wed, 31 Jan 2018 19:58:45 GMT</pubDate>
    <dc:creator>MINX</dc:creator>
    <dc:date>2018-01-31T19:58:45Z</dc:date>
    <item>
      <title>Exporting to CSV and Adding a line with labels</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Exporting-to-CSV-and-Adding-a-line-with-labels/m-p/432867#M107254</link>
      <description>&lt;P&gt;I'm trying to export a file to CSV and include two line headers, one with labels and one with names. Unfortunately the names contain special characters such as commas. I'm assuming I need to mask it somehow or use single quotes or something, but nothing I've tried so far works.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I keep getting an error on the data step code. See the code and error message below.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Any help appreciated.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;*Create demo data;
data class;
	set sashelp.class;
	label age='Age, Years' weight = 'Weight(lbs)' height='Height, inches';
run;

*select names into a macro variable name_list;
*Select labels into a macro variable label_list;
proc sql noprint;
	select trim(name) into :name_list separated by ", "
		from sashelp.vcolumn
			where libname='WORK' and memname='CLASS' and not missing (name);
	select quote(trim(coalesce(label, name))) into :label_list separated by ", "
		from sashelp.vcolumn
			where libname='WORK' and memname='CLASS';
quit;

*check macro variables;
%put Name= &amp;amp;name_list;
%put;
%put Label= &amp;amp;label_list;


filename fout 'C:\_localdata\temp\Class.csv';

data _null_;
	set class;
	file fout dlm=',';

	if _n_=1 then
		do;
			put "&amp;amp;label_list";
			put "&amp;amp;name_list";
		end;

	put (_all_)(:);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Log:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;1730  filename fout 'C:\_localdata\temp\Class.csv';
1731
1732  data _null_;
1733      set class;
1734      file fout dlm=',';
1735
1736      if _n_=1 then
1737          do;
1738              put "&amp;amp;label_list";
NOTE: Line generated by the macro variable "LABEL_LIST".
1      ""Name", "Sex", "Age, Years", "Height, inches", "Weight(lbs)"
       ---   ----   ----          ----              ----           --
       61    49     49            49                49             79
                                                                   76
                           -                -
                           22               22
                           -                -
                           200              200
ERROR: The name  is not a valid SAS name.
ERROR 61-185: The name  is not a valid SAS name.

NOTE 49-169: The meaning of an identifier after a quoted string might change in a future SAS
             release.  Inserting white space between a quoted string and the succeeding
             identifier is recommended.

ERROR 79-322: Expecting a (.

ERROR 76-322: Syntax error, statement will be ignored.

ERROR 22-322: Syntax error, expecting one of the following: a name, an integer constant,
              arrayname, #, $, &amp;amp;, (, +, -, /, //, :, ;, =, ?, @, @@, OVERPRINT, [, _ALL_,
              _BLANKPAGE_, _ODS_, _PAGE_, {, ~.

ERROR 200-322: The symbol is not recognized and will be ignored.

1739              put "&amp;amp;name_list";
1740          end;
1741
1742      put (_all_)(:);
1743  run;

NOTE: The SAS System stopped processing this step because of errors.
NOTE: DATA statement used (Total process time):
      real time           0.01 seconds
      cpu time            0.03 seconds

&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;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 31 Jan 2018 18:58:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Exporting-to-CSV-and-Adding-a-line-with-labels/m-p/432867#M107254</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2018-01-31T18:58:54Z</dc:date>
    </item>
    <item>
      <title>Re: Exporting to CSV and Adding a line with labels</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Exporting-to-CSV-and-Adding-a-line-with-labels/m-p/432882#M107263</link>
      <description>&lt;P&gt;Isn't option dsd (in the file statement) supposed to handle internal delimiters properly?&lt;/P&gt;</description>
      <pubDate>Wed, 31 Jan 2018 19:23:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Exporting-to-CSV-and-Adding-a-line-with-labels/m-p/432882#M107263</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2018-01-31T19:23:10Z</dc:date>
    </item>
    <item>
      <title>Re: Exporting to CSV and Adding a line with labels</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Exporting-to-CSV-and-Adding-a-line-with-labels/m-p/432889#M107265</link>
      <description>&lt;P&gt;Since there is comma in the value as 'Age, Years', I suggest to change delimeter other than comma, for example, I use semi-comma in below codes:&lt;/P&gt;&lt;P&gt;1. add delimeter into quote function.&lt;/P&gt;&lt;P&gt;2. remove quote marks to call macro variables.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;*select names into a macro variable name_list;
*Select labels into a macro variable label_list;
proc sql noprint;
	select quote(trim(name)||';') into :name_list separated by ' '
		from sashelp.vcolumn
			where libname='WORK' and memname='CLASS' and not missing (name);
	select quote(trim(coalesce(label, name))||';')  into :label_list separated by ' '
		from sashelp.vcolumn
			where libname='WORK' and memname='CLASS';
quit;

*check macro variables;
%put Name= &amp;amp;name_list;
%put;
%put Label= &amp;amp;label_list;


data _null_;
	set class;
	file fout dlm=';' dsd;

	if _n_=1 then
		do;
			put &amp;amp;label_list;
			put &amp;amp;name_list;
		end;

	put (_all_)(:);
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Hope it will help&lt;/P&gt;</description>
      <pubDate>Wed, 31 Jan 2018 19:58:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Exporting-to-CSV-and-Adding-a-line-with-labels/m-p/432889#M107265</guid>
      <dc:creator>MINX</dc:creator>
      <dc:date>2018-01-31T19:58:45Z</dc:date>
    </item>
    <item>
      <title>Re: Exporting to CSV and Adding a line with labels</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Exporting-to-CSV-and-Adding-a-line-with-labels/m-p/432891#M107266</link>
      <description>&lt;P&gt;Would this do the job?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;*Create demo data;
data class;
	set sashelp.class;
	label age='Age, Years' weight = 'Weight(lbs)' height='Height, inches';
run;

proc sql noprint;
create table temp as
select name as _name_, label as _label_
from dictionary.columns
where libname="WORK" and upcase(memname)="CLASS";
select cats(quote(name),"n") into :varList separated by ' '
from dictionary.columns
where libname="WORK" and upcase(memname)="CLASS";
quit;

data _null_;
file "&amp;amp;sasforum.\datasets\TwoLinesHeader.csv" dsd;
set class;
if _n_ = 1 then do;
do until(eof);
    set temp end=eof;
    put _name_ @;
    end;
put;
eof = 0;
do until(eof);
    set temp end=eof;
    put _label_ @;
    end;
put;
end;

put (&amp;amp;varList) (:);
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 31 Jan 2018 20:06:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Exporting-to-CSV-and-Adding-a-line-with-labels/m-p/432891#M107266</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2018-01-31T20:06:13Z</dc:date>
    </item>
    <item>
      <title>Re: Exporting to CSV and Adding a line with labels</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Exporting-to-CSV-and-Adding-a-line-with-labels/m-p/432995#M107297</link>
      <description>&lt;P&gt;If you dont object to using GOTOs, you can get by without any macro:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;*Create demo data;
data class;
	set sashelp.class;
	label age='Age, Years' weight = 'Weight(lbs)' height='Height, inches';
run;

proc sql;
create table temp as
select name as _name_, label as _label_
from dictionary.columns
where libname="WORK" and upcase(memname)="CLASS";
quit;

data _null_;
file "&amp;amp;sasforum.\datasets\TwoLinesHeader_2.csv" dsd;
set class;
if _n_ = 1 then goto headers;
put (_all_) (:);
return;

headers:
    do until(eof);
        set temp end=eof;
        put _name_ @;
        end;
    put;
    eof = 0;
    do until(eof);
        set temp end=eof;
        put _label_ @;
        end;
    put;
    return;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 01 Feb 2018 04:04:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Exporting-to-CSV-and-Adding-a-line-with-labels/m-p/432995#M107297</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2018-02-01T04:04:59Z</dc:date>
    </item>
    <item>
      <title>Re: Exporting to CSV and Adding a line with labels</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Exporting-to-CSV-and-Adding-a-line-with-labels/m-p/433167#M107379</link>
      <description>&lt;P&gt;Thanks&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/462"&gt;@PGStats&lt;/a&gt;&amp;nbsp;this one worked for what I need. The only change I ended up making was to add the COALESCEC function to take the variable name when the label is missing. Otherwise this works exactly as needed.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;coalescec(label, name) as _label_&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 01 Feb 2018 16:34:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Exporting-to-CSV-and-Adding-a-line-with-labels/m-p/433167#M107379</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2018-02-01T16:34:52Z</dc:date>
    </item>
  </channel>
</rss>

