<?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 multiple csv files in SAS in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Export-multiple-csv-files-in-SAS/m-p/807434#M318352</link>
    <description>&lt;P&gt;Hi all, I want to export multiple csv files (by gender) from one sas dataset. My code below works to create the files, however I can't figure out how to keep the column names in the exported files. Please help!&lt;/P&gt;
&lt;P&gt;Note: I am using the sashelp.class dataset for my code below&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data _null_;
  set class;
  length fv $ 200;
  fv = "C:\Users\abi\Desktop\Output" || TRIM(put(sex,4.)) || ".csv";
  file write filevar=fv dsd dlm=',' lrecl=32000;
  put (_all_) (:);;
run;&lt;/PRE&gt;</description>
    <pubDate>Tue, 12 Apr 2022 16:55:26 GMT</pubDate>
    <dc:creator>Abishekaa</dc:creator>
    <dc:date>2022-04-12T16:55:26Z</dc:date>
    <item>
      <title>Export multiple csv files in SAS</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Export-multiple-csv-files-in-SAS/m-p/807434#M318352</link>
      <description>&lt;P&gt;Hi all, I want to export multiple csv files (by gender) from one sas dataset. My code below works to create the files, however I can't figure out how to keep the column names in the exported files. Please help!&lt;/P&gt;
&lt;P&gt;Note: I am using the sashelp.class dataset for my code below&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data _null_;
  set class;
  length fv $ 200;
  fv = "C:\Users\abi\Desktop\Output" || TRIM(put(sex,4.)) || ".csv";
  file write filevar=fv dsd dlm=',' lrecl=32000;
  put (_all_) (:);;
run;&lt;/PRE&gt;</description>
      <pubDate>Tue, 12 Apr 2022 16:55:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Export-multiple-csv-files-in-SAS/m-p/807434#M318352</guid>
      <dc:creator>Abishekaa</dc:creator>
      <dc:date>2022-04-12T16:55:26Z</dc:date>
    </item>
    <item>
      <title>Re: Export multiple csv files in SAS</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Export-multiple-csv-files-in-SAS/m-p/807435#M318353</link>
      <description>&lt;P&gt;Is the source data sorted by the variable used to split?&lt;/P&gt;
&lt;P&gt;If so use BY group processing to decide when to write the header row.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let path=%sysfunc(pathname(work));

proc sort data=sashelp.class out=class;
  by sex;
run;

data _null_;
  set class;
  by sex ;
  fv=cats("&amp;amp;path/",sex,'.csv');
  file csv filevar=fv dsd ;
  if first.sex then put "NAME,SEX,AGE,HEIGHT,WEIGHT";
  put (_all_) (+0);
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Otherwise create the files with the header rows first then use the MOD option on the FILE statement in your step that writes the data lines.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc contents data=class noprint out=names(keep=varnum name label);
run;

proc sql;
create tables headers as 
  select distinct a.*,cats("&amp;amp;path/",b.sex,'.csv') as fv 
  from names a 
     , class b
  order by fv, varnum
;
quit;

data _null_;
  set headers;
  file out dsd filevar=fv ;
  put name @ ;
run;

data _null_;
  set class;
  fv=cats("&amp;amp;path/",sex,'.csv');
  file csv filevar=fv dsd mod;
  put (_all_) (+0);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 12 Apr 2022 17:12:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Export-multiple-csv-files-in-SAS/m-p/807435#M318353</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-04-12T17:12:42Z</dc:date>
    </item>
    <item>
      <title>Re: Export multiple csv files in SAS</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Export-multiple-csv-files-in-SAS/m-p/807467#M318371</link>
      <description>This works perfectly to get the column names in my csv file. Thanks so much, Tom!!!</description>
      <pubDate>Tue, 12 Apr 2022 19:18:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Export-multiple-csv-files-in-SAS/m-p/807467#M318371</guid>
      <dc:creator>Abishekaa</dc:creator>
      <dc:date>2022-04-12T19:18:59Z</dc:date>
    </item>
  </channel>
</rss>

