<?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: Group and export data in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Group-and-export-data/m-p/758405#M239457</link>
    <description>&lt;P&gt;Split it and export it.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
set sashelp.heart;
run;











%macro split_data(dataset= , n= ,path= );
%let dsid=%sysfunc(open(&amp;amp;dataset));
%let nobs=%sysfunc(attrn(&amp;amp;dsid,nlobs));
%let dsid=%sysfunc(close(&amp;amp;dsid));
%let group=%sysevalf(&amp;amp;nobs/&amp;amp;n,ceil);

data %do i=1 %to &amp;amp;group ;  want&amp;amp;i  %end;  ;
 set &amp;amp;dataset;
 select(ceil(_n_/&amp;amp;n));
%do i=1 %to &amp;amp;group ;
 when(&amp;amp;i) output want&amp;amp;i;
%end;
 otherwise;
 end;
run;

%do i=1 %to &amp;amp;group ;
proc export data=want&amp;amp;i outfile="&amp;amp;path.\want&amp;amp;i..xlsx" dbms=xlsx replace;
run;
%end;
%mend;

%split_data(dataset=have, n=500 ,path=c:\temp\ )&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Fri, 30 Jul 2021 13:42:13 GMT</pubDate>
    <dc:creator>Ksharp</dc:creator>
    <dc:date>2021-07-30T13:42:13Z</dc:date>
    <item>
      <title>Group and export data</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Group-and-export-data/m-p/758253#M239385</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Using SAS 9.4&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have 61,300 records and I want to export to excel (as a csv) every 500 records (500 is the max to import for another program). Is there an efficient way to program this so I do not have to manually export 120+ files? Thank you&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 29 Jul 2021 20:16:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Group-and-export-data/m-p/758253#M239385</guid>
      <dc:creator>GS2</dc:creator>
      <dc:date>2021-07-29T20:16:38Z</dc:date>
    </item>
    <item>
      <title>Re: Group and export data</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Group-and-export-data/m-p/758260#M239388</link>
      <description>Add a proc export to this program:&lt;BR /&gt;&lt;A href="https://gist.github.com/statgeek/abc3c6ce1dbeedb84fe7f11da0603cda" target="_blank"&gt;https://gist.github.com/statgeek/abc3c6ce1dbeedb84fe7f11da0603cda&lt;/A&gt;</description>
      <pubDate>Thu, 29 Jul 2021 20:29:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Group-and-export-data/m-p/758260#M239388</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2021-07-29T20:29:45Z</dc:date>
    </item>
    <item>
      <title>Re: Group and export data</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Group-and-export-data/m-p/758274#M239395</link>
      <description>Will I have to add a proc export for each set of 500 observations?</description>
      <pubDate>Thu, 29 Jul 2021 21:08:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Group-and-export-data/m-p/758274#M239395</guid>
      <dc:creator>GS2</dc:creator>
      <dc:date>2021-07-29T21:08:45Z</dc:date>
    </item>
    <item>
      <title>Re: Group and export data</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Group-and-export-data/m-p/758275#M239396</link>
      <description>&lt;P&gt;If you add the Proc Export to the macro, you should not have to code multiple Proc Exports.&amp;nbsp; I think right after the data step would be a good spot to add the Proc Export.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Jim&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;    %do i=1 %to &amp;amp;nfiles;
    
        %*Split file by number of records;
        data &amp;amp;outDsnPrefix.&amp;amp;i.;
            set &amp;amp;dsn. (firstobs=&amp;amp;first obs=&amp;amp;last);
        run;

        %*Increment counters to have correct first/last;
        %let first = %eval(&amp;amp;last+1);
        %let last = %eval((&amp;amp;i. + 1)*&amp;amp;size.);
    %end;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 29 Jul 2021 21:12:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Group-and-export-data/m-p/758275#M239396</guid>
      <dc:creator>jimbarbour</dc:creator>
      <dc:date>2021-07-29T21:12:30Z</dc:date>
    </item>
    <item>
      <title>Re: Group and export data</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Group-and-export-data/m-p/758280#M239400</link>
      <description>First, Excel files are not the same as CSV and export to a CSV is much more trivial. &lt;BR /&gt;&lt;BR /&gt;The program linked is a macro program that you can easily adapt and only need to add the PROC EXPORT once. The macro splits the files you just need to export it to a name of your choice. Go through it, run it with some sample data - it set up to run with some data from SASHELP so should run right away for you. &lt;BR /&gt;&lt;BR /&gt;Once it's working to generate the data set add your export step. &lt;BR /&gt;&lt;BR /&gt;Or I answered a very similar question earlier today about exporting to CSV by group variable. It would be trivial to create a grouping variable for every 500 records and then use the same approach on your CSV files. That question has a few other solutions that you may find easier to adapt as well. &lt;BR /&gt;&lt;BR /&gt;Honestly, this question is asked an answered at least monthly, if you search on here you'll find many data step solutions, some exactly what you need and some where you need to add a variation. Pick one approach and try and work through that.</description>
      <pubDate>Thu, 29 Jul 2021 21:22:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Group-and-export-data/m-p/758280#M239400</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2021-07-29T21:22:10Z</dc:date>
    </item>
    <item>
      <title>Re: Group and export data</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Group-and-export-data/m-p/758405#M239457</link>
      <description>&lt;P&gt;Split it and export it.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
set sashelp.heart;
run;











%macro split_data(dataset= , n= ,path= );
%let dsid=%sysfunc(open(&amp;amp;dataset));
%let nobs=%sysfunc(attrn(&amp;amp;dsid,nlobs));
%let dsid=%sysfunc(close(&amp;amp;dsid));
%let group=%sysevalf(&amp;amp;nobs/&amp;amp;n,ceil);

data %do i=1 %to &amp;amp;group ;  want&amp;amp;i  %end;  ;
 set &amp;amp;dataset;
 select(ceil(_n_/&amp;amp;n));
%do i=1 %to &amp;amp;group ;
 when(&amp;amp;i) output want&amp;amp;i;
%end;
 otherwise;
 end;
run;

%do i=1 %to &amp;amp;group ;
proc export data=want&amp;amp;i outfile="&amp;amp;path.\want&amp;amp;i..xlsx" dbms=xlsx replace;
run;
%end;
%mend;

%split_data(dataset=have, n=500 ,path=c:\temp\ )&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 30 Jul 2021 13:42:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Group-and-export-data/m-p/758405#M239457</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2021-07-30T13:42:13Z</dc:date>
    </item>
  </channel>
</rss>

