<?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: Splitting a SAS table into multiple tables bases on number of rows in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Splitting-a-SAS-table-into-multiple-tables-bases-on-number-of/m-p/809061#M319032</link>
    <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;

proc surveyselect data=sashelp.heart groups=3 out=temp;
run;

data one two three;
 set temp;
 if GroupID=1 then output one;
 if GroupID=2 then output two;
 if GroupID=3 then output three;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Thu, 21 Apr 2022 13:32:37 GMT</pubDate>
    <dc:creator>Ksharp</dc:creator>
    <dc:date>2022-04-21T13:32:37Z</dc:date>
    <item>
      <title>Splitting a SAS table into multiple tables bases on number of rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Splitting-a-SAS-table-into-multiple-tables-bases-on-number-of/m-p/809051#M319029</link>
      <description>&lt;P&gt;In reality I have a SAS table having very many rows, think milions, and I want to split it into sub-tables. The reason for this is that I will export the data and those who will process it use a system that cannot process so many rows at once, so it has to be split.&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;For clarity I will provide an &lt;STRONG&gt;demonstrative example&lt;/STRONG&gt; that would solve my problems.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Assume I have a SAS table consisting of 334 rows.&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;Now I want to split this table into 3 tables, since 334/3 is 111.3333 the function would return:&amp;nbsp;&lt;BR /&gt;- table1 consisting of 111 rows.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;- table2 consisting of 111 rows.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;- table3 consisting of 112 rows.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;So I just say that I want 3 tables and then it puts the suitable amount of observations in each new table (111, 111 and 112 rows in this case).&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Any ideas on how to achieve this?&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 21 Apr 2022 13:20:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Splitting-a-SAS-table-into-multiple-tables-bases-on-number-of/m-p/809051#M319029</guid>
      <dc:creator>SasStatistics</dc:creator>
      <dc:date>2022-04-21T13:20:47Z</dc:date>
    </item>
    <item>
      <title>Re: Splitting a SAS table into multiple tables bases on number of rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Splitting-a-SAS-table-into-multiple-tables-bases-on-number-of/m-p/809059#M319031</link>
      <description>&lt;P&gt;just make a counter variable and use that. you could do if 1 &amp;lt;= count &amp;lt;= 111 then output table1. else if count 111 &amp;lt; count &amp;lt;= 223 then output table2; else if count &amp;gt; 233 then output table3;&lt;/P&gt;</description>
      <pubDate>Thu, 21 Apr 2022 13:30:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Splitting-a-SAS-table-into-multiple-tables-bases-on-number-of/m-p/809059#M319031</guid>
      <dc:creator>tarheel13</dc:creator>
      <dc:date>2022-04-21T13:30:28Z</dc:date>
    </item>
    <item>
      <title>Re: Splitting a SAS table into multiple tables bases on number of rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Splitting-a-SAS-table-into-multiple-tables-bases-on-number-of/m-p/809061#M319032</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;

proc surveyselect data=sashelp.heart groups=3 out=temp;
run;

data one two three;
 set temp;
 if GroupID=1 then output one;
 if GroupID=2 then output two;
 if GroupID=3 then output three;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 21 Apr 2022 13:32:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Splitting-a-SAS-table-into-multiple-tables-bases-on-number-of/m-p/809061#M319032</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2022-04-21T13:32:37Z</dc:date>
    </item>
    <item>
      <title>Re: Splitting a SAS table into multiple tables bases on number of rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Splitting-a-SAS-table-into-multiple-tables-bases-on-number-of/m-p/809063#M319034</link>
      <description>&lt;P&gt;First check the no of records in the table. This can be achieved by PROC DATASETS/CONTENTS, data file functions and PROC SQL DICTIONARAY.TABLES.&lt;/P&gt;
&lt;P&gt;Calculate the intervals, and put them into macro variables.&lt;/P&gt;
&lt;P&gt;Use a data step, and explicitly output to your target tables depending on the value of the automatic _n_ variable compared with the macro variables above.&lt;/P&gt;</description>
      <pubDate>Thu, 21 Apr 2022 13:34:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Splitting-a-SAS-table-into-multiple-tables-bases-on-number-of/m-p/809063#M319034</guid>
      <dc:creator>LinusH</dc:creator>
      <dc:date>2022-04-21T13:34:04Z</dc:date>
    </item>
    <item>
      <title>Re: Splitting a SAS table into multiple tables bases on number of rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Splitting-a-SAS-table-into-multiple-tables-bases-on-number-of/m-p/809070#M319038</link>
      <description>&lt;P&gt;&lt;STRONG&gt;IF&lt;/STRONG&gt; you can fit a single slice of your table (&amp;amp;max_rows_per_table) into memory then below coding approach should work.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let max_rows_per_table=5;

data have;
  set sashelp.class;
run;

data _null_;
  if _n_=1 then
    do;
      dcl hash h1(dataset:'have(obs=0)',multidata:'y');
      h1.defineKey('sex');
      h1.defineData(all:'y');
      h1.defineDone();
    end;
  set have end=last;
  h1.add();
  if mod(_n_,&amp;amp;max_rows_per_table)=0 or last then
    do;
      h1.output(dataset:cats('work.want_',put(_n_,z10.)));
      h1.clear();
    end;
run;

proc sql;
  select *
  from dictionary.tables
  where libname='WORK' and memname like 'WANT%'
  ;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Patrick_0-1650548767889.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/70676iEB42571831EED9D6/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Patrick_0-1650548767889.png" alt="Patrick_0-1650548767889.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;The logic for naming the table slices could of course be different - like 1, 2, 3...&lt;/P&gt;
&lt;P&gt;In the hash definition for&lt;EM&gt;&amp;nbsp;h1.defineKey('&amp;lt;some variable&amp;gt;');&lt;/EM&gt;&amp;nbsp;you can select any column that exists in your source table - but ideally choose the one with the lowest length as this will save some memory.&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Using hash tables is the only way I know of that allows you to create output tables dynamically during execution time. Any other approach will require some pre-processing to define the output tables (not that hard to do but will require some additional coding to generate the required syntax dynamically).&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 21 Apr 2022 14:04:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Splitting-a-SAS-table-into-multiple-tables-bases-on-number-of/m-p/809070#M319038</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2022-04-21T14:04:40Z</dc:date>
    </item>
    <item>
      <title>Re: Splitting a SAS table into multiple tables bases on number of rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Splitting-a-SAS-table-into-multiple-tables-bases-on-number-of/m-p/809090#M319043</link>
      <description>&lt;P&gt;No need to create data sets, just select the records desired from the source data set.&lt;/P&gt;
&lt;P&gt;Pseudo code example to export records to different files in groups of 100:&lt;/P&gt;
&lt;PRE&gt;proc export data=have (firstobs=1 obs=100)
    outfile=&amp;lt;your first file&amp;gt;
    &amp;lt;other options&amp;gt;
;
run;

proc export data=have (firstobs=101 obs=200)
    outfile=&amp;lt;your 2nd file&amp;gt;
    &amp;lt;other options&amp;gt;
;
run;&lt;/PRE&gt;
&lt;P&gt;So you can use something like this to build the observation selection bit and a filename to plug into CALL EXECUTE code with boiler plate for the proc export options. Or create strings and write to a text program file to have documentation of what was created and run.&lt;/P&gt;
&lt;PRE&gt;data _null_;
   do countobs = 1 to 3025 by 300;
      groupcount+1;
      string= catx(' ',"(firstobs=",countobs," obs=",groupcount*300,')');
      put string=;
      filename = cats("&amp;lt;path&amp;gt;\filebasename",put(groupcount,z3.),'.extension');
      put filename=;
   end;
run;&lt;/PRE&gt;</description>
      <pubDate>Thu, 21 Apr 2022 14:36:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Splitting-a-SAS-table-into-multiple-tables-bases-on-number-of/m-p/809090#M319043</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2022-04-21T14:36:59Z</dc:date>
    </item>
    <item>
      <title>Re: Splitting a SAS table into multiple tables bases on number of rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Splitting-a-SAS-table-into-multiple-tables-bases-on-number-of/m-p/809126#M319050</link>
      <description>&lt;P&gt;data one two three;&lt;BR /&gt;set SASHELP.baseball;&lt;/P&gt;
&lt;P&gt;which= mod(_n_,3);&lt;BR /&gt;if which= 0 then output one;&lt;BR /&gt;else do;&lt;BR /&gt;if which &amp;lt; 2 then output two;&lt;BR /&gt;else output three;&lt;BR /&gt;end;&lt;BR /&gt;drop which;&lt;BR /&gt;run;&lt;/P&gt;</description>
      <pubDate>Thu, 21 Apr 2022 17:02:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Splitting-a-SAS-table-into-multiple-tables-bases-on-number-of/m-p/809126#M319050</guid>
      <dc:creator>Michael_Harper</dc:creator>
      <dc:date>2022-04-21T17:02:36Z</dc:date>
    </item>
    <item>
      <title>Re: Splitting a SAS table into multiple tables bases on number of rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Splitting-a-SAS-table-into-multiple-tables-bases-on-number-of/m-p/809136#M319054</link>
      <description>&lt;P&gt;A solution using CALL EXECUTE for the data set SASHELP.CARS&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql; /* Find number of observations in data set */
    select nobs into :nobs from sashelp.vtable where libname='SASHELP' and memname="CARS";
quit;
%let n_splits=5; /* User specifies number of splits desired */

data _null_;
    nobs_per_split=&amp;amp;nobs/&amp;amp;n_splits;
    do i=1 to &amp;amp;n_splits;
        start=round((i-1)*nobs_per_split+1);
        end=round(i*nobs_per_split);
        call execute(cat('proc export data=sashelp.cars(firstobs=',start,' obs=',end,') outfile="table',i,'.csv" dbms=csv; run;'));
    end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 21 Apr 2022 17:44:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Splitting-a-SAS-table-into-multiple-tables-bases-on-number-of/m-p/809136#M319054</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2022-04-21T17:44:45Z</dc:date>
    </item>
    <item>
      <title>Re: Splitting a SAS table into multiple tables bases on number of rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Splitting-a-SAS-table-into-multiple-tables-bases-on-number-of/m-p/809139#M319056</link>
      <description>&lt;P&gt;No need to split into datasets. Just use the FILEVAR= option of the FILE statement in the DATA step that does the export.&lt;/P&gt;
&lt;P&gt;In which file format do you export your data?&lt;/P&gt;</description>
      <pubDate>Thu, 21 Apr 2022 17:45:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Splitting-a-SAS-table-into-multiple-tables-bases-on-number-of/m-p/809139#M319056</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2022-04-21T17:45:23Z</dc:date>
    </item>
    <item>
      <title>Re: Splitting a SAS table into multiple tables bases on number of rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Splitting-a-SAS-table-into-multiple-tables-bases-on-number-of/m-p/848806#M335572</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/18408"&gt;@Ksharp&lt;/a&gt;&amp;nbsp; escreveu:&lt;BR /&gt;&lt;PRE&gt;&lt;CODE class=""&gt;proc surveyselect data=sashelp.heart groups=3 out=temp;
run;

data one two three;
 set temp;
 if GroupID=1 then output one;
 if GroupID=2 then output two;
 if GroupID=3 then output three;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;Can I use this to make a PROC SQL split a query?&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;I have a long-lasting query and want to split the results in one million rows per time.&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;How can I do this?&lt;/P&gt;</description>
      <pubDate>Fri, 09 Dec 2022 20:36:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Splitting-a-SAS-table-into-multiple-tables-bases-on-number-of/m-p/848806#M335572</guid>
      <dc:creator>Holmes</dc:creator>
      <dc:date>2022-12-09T20:36:07Z</dc:date>
    </item>
    <item>
      <title>Re: Splitting a SAS table into multiple tables bases on number of rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Splitting-a-SAS-table-into-multiple-tables-bases-on-number-of/m-p/848926#M335641</link>
      <description>Please start a brand new session, this question is almost three years ago.&lt;BR /&gt;" want to split the results in one million rows per time. “&lt;BR /&gt;You need make id variable.Like:&lt;BR /&gt;data have;&lt;BR /&gt;set have;&lt;BR /&gt; id+1;&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;proc sql;&lt;BR /&gt;create table part1 as&lt;BR /&gt;select * from have where id in (1:1000000);&lt;BR /&gt;............&lt;BR /&gt;quit;</description>
      <pubDate>Sat, 10 Dec 2022 19:35:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Splitting-a-SAS-table-into-multiple-tables-bases-on-number-of/m-p/848926#M335641</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2022-12-10T19:35:08Z</dc:date>
    </item>
  </channel>
</rss>

