<?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 Do loop for export in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Do-loop-for-export/m-p/548015#M151919</link>
    <description>&lt;P&gt;OK I know how to export a table to csv&amp;nbsp;and I have found BAT way to split the csv&amp;nbsp;into 5000 rows, but it does not keep the header for each file. Just the first file. SO I am looking for a do loop that will export&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;proc export DATA=clean_table&lt;BR /&gt;OUTFILE="C:\Table_2019_04_02.csv"&lt;BR /&gt;DBMS=DLM REPLACE;&lt;BR /&gt;DELIMITER=',';&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Depending on the day, the source file could contain between 3000 and 200000 rows of data. I would love to get the files to be exported&amp;nbsp;like&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;clean_table 14654 rows&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Table_2019_04_02-01.csv (5000 rows)&lt;/P&gt;&lt;P&gt;Table_2019_04_02-02.csv&amp;nbsp;(5000 rows)&lt;/P&gt;&lt;P&gt;Table_2019_04_02-03.csv (4654 rows)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any help would be appreciated! Thanks!&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 02 Apr 2019 19:26:46 GMT</pubDate>
    <dc:creator>nickbarrett</dc:creator>
    <dc:date>2019-04-02T19:26:46Z</dc:date>
    <item>
      <title>Do loop for export</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-loop-for-export/m-p/548015#M151919</link>
      <description>&lt;P&gt;OK I know how to export a table to csv&amp;nbsp;and I have found BAT way to split the csv&amp;nbsp;into 5000 rows, but it does not keep the header for each file. Just the first file. SO I am looking for a do loop that will export&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;proc export DATA=clean_table&lt;BR /&gt;OUTFILE="C:\Table_2019_04_02.csv"&lt;BR /&gt;DBMS=DLM REPLACE;&lt;BR /&gt;DELIMITER=',';&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Depending on the day, the source file could contain between 3000 and 200000 rows of data. I would love to get the files to be exported&amp;nbsp;like&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;clean_table 14654 rows&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Table_2019_04_02-01.csv (5000 rows)&lt;/P&gt;&lt;P&gt;Table_2019_04_02-02.csv&amp;nbsp;(5000 rows)&lt;/P&gt;&lt;P&gt;Table_2019_04_02-03.csv (4654 rows)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any help would be appreciated! Thanks!&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 02 Apr 2019 19:26:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-loop-for-export/m-p/548015#M151919</guid>
      <dc:creator>nickbarrett</dc:creator>
      <dc:date>2019-04-02T19:26:46Z</dc:date>
    </item>
    <item>
      <title>Re: Do loop for export</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-loop-for-export/m-p/548023#M151921</link>
      <description>&lt;P&gt;Not a generic solution but should work for the request.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;proc export DATA=clean_table (firstobs=1 obs=5000)
   OUTFILE="C:\Table_2019_04_02_01.csv"
   DBMS=DLM REPLACE;
   DELIMITER=',';
run;
proc export DATA=clean_table (firstobs=5001 obs=10000)
   OUTFILE="C:\Table_2019_04_02_02.csv"
   DBMS=DLM REPLACE;
   DELIMITER=',';
run;
proc export DATA=clean_table (firstobs=10001 )
   OUTFILE="C:\Table_2019_04_02_03.csv"
   DBMS=DLM REPLACE;
   DELIMITER=',';
run;&lt;/PRE&gt;
&lt;P&gt;Firstobs specifies the number of the first observation to process and OBS specifies the number of the last observation to process.&lt;/P&gt;
&lt;P&gt;When OBS is not specified then the default should be to get the remaining.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 02 Apr 2019 19:36:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-loop-for-export/m-p/548023#M151921</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2019-04-02T19:36:20Z</dc:date>
    </item>
    <item>
      <title>Re: Do loop for export</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-loop-for-export/m-p/548029#M151926</link>
      <description>&lt;P&gt;Why do you want to split them?&lt;/P&gt;
&lt;P&gt;For such a simple split you can just do it with a single data step.&lt;/P&gt;
&lt;P&gt;Here is one that will put in the header row for you.&amp;nbsp; Make sure your real file does not already include the variables used (_EOF, FNAME, _FILE, _P, _NOBS).&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let path=%sysfunc(pathname(work));
%let n=5;
%let dsn=sashelp.class;

proc transpose data=&amp;amp;dsn(obs=0) out=names;
var _all_;
run;

data _null_;
  do _n_=1 to &amp;amp;n ;
    if _n_=1 then link header;
    set &amp;amp;dsn end=_eof;
    put (_all_) (+0);
  end;
  if _eof then stop;
return;
header:
length fname $200 ;
_file+1;
fname=cats("&amp;amp;path/file",_file,'.csv');
putlog fname=;
file out filevar=fname dsd ;
do _p=1 to _nobs;
  set names point=_p nobs=_nobs;
  put _name_ @;
end;
put;
return;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 02 Apr 2019 19:56:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-loop-for-export/m-p/548029#M151926</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-04-02T19:56:09Z</dc:date>
    </item>
    <item>
      <title>Re: Do loop for export</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-loop-for-export/m-p/548041#M151935</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;&amp;nbsp;solution is much more efficient than mine.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This macro splits the files easily for you and then you can export them. You could modify the macro to have it exported as the last step as well.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;A href="https://gist.github.com/statgeek/abc3c6ce1dbeedb84fe7f11da0603cda" target="_blank"&gt;https://gist.github.com/statgeek/abc3c6ce1dbeedb84fe7f11da0603cda&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you're new to macro programming, I recommend the UCLA tutorials and there's a tutorial on my Github main page which you can find from the Gist page - just walks through the steps to convert a working program to a macro. It's not great but it's there &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also, you can do this directly within a data step, using the FILEVAR statement which you change the output data file on the fly. This is ultimately Tom's solution.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/269045"&gt;@nickbarrett&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;OK I know how to export a table to csv&amp;nbsp;and I have found BAT way to split the csv&amp;nbsp;into 5000 rows, but it does not keep the header for each file. Just the first file. SO I am looking for a do loop that will export&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc export DATA=clean_table&lt;BR /&gt;OUTFILE="C:\Table_2019_04_02.csv"&lt;BR /&gt;DBMS=DLM REPLACE;&lt;BR /&gt;DELIMITER=',';&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Depending on the day, the source file could contain between 3000 and 200000 rows of data. I would love to get the files to be exported&amp;nbsp;like&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;clean_table 14654 rows&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Table_2019_04_02-01.csv (5000 rows)&lt;/P&gt;
&lt;P&gt;Table_2019_04_02-02.csv&amp;nbsp;(5000 rows)&lt;/P&gt;
&lt;P&gt;Table_2019_04_02-03.csv (4654 rows)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Any help would be appreciated! Thanks!&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 02 Apr 2019 20:32:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-loop-for-export/m-p/548041#M151935</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2019-04-02T20:32:01Z</dc:date>
    </item>
  </channel>
</rss>

