<?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: Output groups of observations to multiple datasets in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Output-groups-of-observations-to-multiple-datasets/m-p/277885#M55865</link>
    <description>&lt;P&gt;It's best not to split the data. Why do you want to split it?&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;A href="http://www.sascommunity.org/wiki/Split_Data_into_Subsets" target="_blank"&gt;http://www.sascommunity.org/wiki/Split_Data_into_Subsets&lt;/A&gt;&lt;/P&gt;</description>
    <pubDate>Thu, 16 Jun 2016 13:22:26 GMT</pubDate>
    <dc:creator>Reeza</dc:creator>
    <dc:date>2016-06-16T13:22:26Z</dc:date>
    <item>
      <title>Output groups of observations to multiple datasets</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Output-groups-of-observations-to-multiple-datasets/m-p/277829#M55837</link>
      <description>&lt;P&gt;Hi&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have several datasets for processing, one variable, varying number of observations in each.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I need to&amp;nbsp;split the datasets into multiple datasets. &amp;nbsp;I need to output the first 80 observations, then the next 80 and so on until I reach the end of the dataset which will output the remaining observations.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What would be the simplest way to tackle this?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks&lt;/P&gt;</description>
      <pubDate>Thu, 16 Jun 2016 09:50:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Output-groups-of-observations-to-multiple-datasets/m-p/277829#M55837</guid>
      <dc:creator>kimdukes77</dc:creator>
      <dc:date>2016-06-16T09:50:25Z</dc:date>
    </item>
    <item>
      <title>Re: Output groups of observations to multiple datasets</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Output-groups-of-observations-to-multiple-datasets/m-p/277836#M55843</link>
      <description>&lt;P&gt;By far, the easiest way is not to split the observations. &amp;nbsp;Instead, create a new variable that is 1 for the first 80 observations, 2 for the next 80, etc. &amp;nbsp;Then use a BY statement when processing the data set later. &amp;nbsp;Here is a simple example:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want;&lt;/P&gt;
&lt;P&gt;set a b c d e f g;&lt;/P&gt;
&lt;P&gt;batch = ceil(_n_/80);&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;</description>
      <pubDate>Thu, 16 Jun 2016 10:08:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Output-groups-of-observations-to-multiple-datasets/m-p/277836#M55843</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2016-06-16T10:08:39Z</dc:date>
    </item>
    <item>
      <title>Re: Output groups of observations to multiple datasets</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Output-groups-of-observations-to-multiple-datasets/m-p/277840#M55845</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;SPAN class=""&gt;&lt;A id="link_8" class="lia-link-navigation lia-page-link lia-user-name-link" href="https://communities.sas.com/t5/user/viewprofilepage/user-id/58206" target="_self"&gt;kimdukes77&lt;/A&gt;,&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN class=""&gt;You could specify multiple table names in the DATA statement, for example&lt;/SPAN&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA table1 table2;
  set table;
  if _N_&amp;lt;=80 then output table1;
  else if _N_&amp;lt;=160 then output table2;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;SPAN class=""&gt;If you know the number of observations you could create a step like the previous one.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN class=""&gt;If the number of observations is going to be dynamic, counting the number of rows and apply ceil(nobs/80) can give you the number of data sets that you need&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 16 Jun 2016 10:11:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Output-groups-of-observations-to-multiple-datasets/m-p/277840#M55845</guid>
      <dc:creator>arodriguez</dc:creator>
      <dc:date>2016-06-16T10:11:10Z</dc:date>
    </item>
    <item>
      <title>Re: Output groups of observations to multiple datasets</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Output-groups-of-observations-to-multiple-datasets/m-p/277885#M55865</link>
      <description>&lt;P&gt;It's best not to split the data. Why do you want to split it?&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;A href="http://www.sascommunity.org/wiki/Split_Data_into_Subsets" target="_blank"&gt;http://www.sascommunity.org/wiki/Split_Data_into_Subsets&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 16 Jun 2016 13:22:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Output-groups-of-observations-to-multiple-datasets/m-p/277885#M55865</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2016-06-16T13:22:26Z</dc:date>
    </item>
    <item>
      <title>Re: Output groups of observations to multiple datasets</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Output-groups-of-observations-to-multiple-datasets/m-p/281456#M57083</link>
      <description>&lt;P&gt;Let us know if this works.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This is based on macro and extracts 2 records from original dataset and creates table. &amp;nbsp;The sample dataset has 10 records.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;/* sample dataset */
data test;
   input empid 
         firstname $4.
         lastname $4.;
         datalines;
1 Sara A
2 Sara B
3 Sara C
4 Sara D
5 Sara E
6 Sara F
7 Sara G
8 Sara H
9 Sara I
10 Sara J
;

/* get # of records from original dataset */
proc sql;
select count(*) into: total_rec
from test;
quit;
%put Total # of records: &amp;amp;total_rec;

/* define a counter */
%LET i = 1;

/* macro */
%MACRO test1 (val);

	/* create table with 2 records*/

	%if &amp;amp;total_rec &amp;gt;=2 %then
		%do;
			%put ######################;
			%put Iteration #&amp;amp;i;

			/* extract 2 records */
			proc sql outobs=2;
			create table table_&amp;amp;i as
			select * from test;
			quit;
			%put Table &amp;amp;i created;

			/* sort extracted records */
			proc sort data=table_&amp;amp;i; by empid; run;

			/* remove the extracted records from the original dataset */
			data test;
			merge test (in=a) table_&amp;amp;i (in=b);
			by empid;
			if a and not b;
			run;

			/* count the remaining records */
			proc sql;
			select count(*) into: total_rec
			from test;
			quit;

			/* if we have more or equal to 2 records loop macro */
			%if &amp;amp;total_rec &amp;gt;= 2 %then
				%do;
				%LET i = %sysevalf(&amp;amp;i + 1);
				%test1(&amp;amp;i);
				%put &amp;amp;i;
				%end;
			%else
				%if &amp;amp;total_rec &amp;gt; 0 %then 
					/* there must be atleast 1 record */
					%do;
						%LET i = %sysevalf(&amp;amp;i + 1);
						/* extract remaining records */
						proc sql;
						create table table_&amp;amp;i as
						select * from test;
						quit;
					%end;
		%end;

%MEND;

%test1(&amp;amp;i);&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 30 Jun 2016 15:12:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Output-groups-of-observations-to-multiple-datasets/m-p/281456#M57083</guid>
      <dc:creator>GoExplore</dc:creator>
      <dc:date>2016-06-30T15:12:27Z</dc:date>
    </item>
    <item>
      <title>Re: Output groups of observations to multiple datasets</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Output-groups-of-observations-to-multiple-datasets/m-p/281967#M57239</link>
      <description>&lt;P&gt;This does what you want quite simply, using firstobs &amp;amp; obs dataset options.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT face="comic sans ms,sans-serif"&gt;data have; do x=1 to 250; output; end;run;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT face="comic sans ms,sans-serif"&gt;%macro split (dsn=,splitobs=);&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="comic sans ms,sans-serif"&gt;proc sql noprint;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="comic sans ms,sans-serif"&gt;select count(*) into : totobs&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="comic sans ms,sans-serif"&gt;from &amp;amp;dsn;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="comic sans ms,sans-serif"&gt;quit;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="comic sans ms,sans-serif"&gt;%do i=1 %to &amp;amp;totobs %by &amp;amp;splitobs;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="comic sans ms,sans-serif"&gt;data want%eval(1+&amp;amp;i/&amp;amp;splitobs);&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="comic sans ms,sans-serif"&gt;set &amp;amp;dsn (firstobs=&amp;amp;i obs=%eval(&amp;amp;i+&amp;amp;splitobs-1));&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="comic sans ms,sans-serif"&gt;run;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="comic sans ms,sans-serif"&gt;%end;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="comic sans ms,sans-serif"&gt;%mend;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="comic sans ms,sans-serif"&gt;%split(dsn=have,splitobs=80);&lt;/FONT&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 04 Jul 2016 09:11:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Output-groups-of-observations-to-multiple-datasets/m-p/281967#M57239</guid>
      <dc:creator>JohnHoughton</dc:creator>
      <dc:date>2016-07-04T09:11:34Z</dc:date>
    </item>
  </channel>
</rss>

