<?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: I am having n observation in a dataset.i want equal observation into 4 datasets? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/I-am-having-n-observation-in-a-dataset-i-want-equal-observation/m-p/621392#M182675</link>
    <description>&lt;P&gt;This code will work.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want1 want2 want3 want4;
  set have nobs=nrecs;
  
  select (ceil(_n_/(nrecs/4)));
    when (1) output want1;
    when (2) output want2;
    when (3) output want3;
    otherwise output want4;
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;It uses the SET statement option&amp;nbsp; NOBS=, which stores the number of obs in HAVE in variable nrecs. And it also uses the automatic variable _N_ which is the "iteration" number in the data step - equivalent to the obs number in this program.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Now say nrecs=100,&amp;nbsp; then nrecs/4=25&amp;nbsp; and _n_/(nrecs/4) ranges from .004, .008,... ,1, 1.004, 1.008, … 4.. And the CEIL functions rounds up to 1,2,3, or 4..&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Extra observations are written to WANT4, then WANT3, then WANT2.&lt;/P&gt;</description>
    <pubDate>Fri, 31 Jan 2020 08:44:01 GMT</pubDate>
    <dc:creator>mkeintz</dc:creator>
    <dc:date>2020-01-31T08:44:01Z</dc:date>
    <item>
      <title>I am having n observation in a dataset.i want equal observation into 4 datasets?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/I-am-having-n-observation-in-a-dataset-i-want-equal-observation/m-p/621384#M182670</link>
      <description>&lt;P&gt;I am having n observation in a dataset.i want equal observation into 4 datasets?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;suppose i have 20 obs in a dataset?&lt;/P&gt;&lt;P&gt;i want 1 to 5 obs in one dataset and 6 to 10 in one dataset and 11 to 15 in one dataset and 16 to 20 obs in one dataset?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;please only code required?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 31 Jan 2020 07:53:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/I-am-having-n-observation-in-a-dataset-i-want-equal-observation/m-p/621384#M182670</guid>
      <dc:creator>Saikiran_Mamidi</dc:creator>
      <dc:date>2020-01-31T07:53:08Z</dc:date>
    </item>
    <item>
      <title>Re: I am having n observation in a dataset.i want equal observation into 4 datasets?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/I-am-having-n-observation-in-a-dataset-i-want-equal-observation/m-p/621391#M182674</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/304446"&gt;@Saikiran_Mamidi&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is a code I found and seems to work fine :&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro split2(num,tab_in);
	data _null_;
		if 0 then set &amp;amp;tab_in nobs=count;
		call symput('numobs', put(count, 8.));
	run;

	%let n=%sysevalf(&amp;amp;numobs/&amp;amp;num, ceil);

	data 
		%do J=1 %to &amp;amp;num;
			SAMPLE_&amp;amp;J %end;
		;
		set &amp;amp;tab_in;

		%do I=1 %to &amp;amp;num;
			if %eval(&amp;amp;n*(&amp;amp;i-1)) &amp;lt;_n_ &amp;lt;=%eval(&amp;amp;n*&amp;amp;I) then output SAMPLE_&amp;amp;I;
		%end;
	run;

%mend split2;

/*Sample test*/

data have;
	do i=1 to 15;
		output;
	end;
run;
%split2(4,have);&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 31 Jan 2020 08:40:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/I-am-having-n-observation-in-a-dataset-i-want-equal-observation/m-p/621391#M182674</guid>
      <dc:creator>ed_sas_member</dc:creator>
      <dc:date>2020-01-31T08:40:25Z</dc:date>
    </item>
    <item>
      <title>Re: I am having n observation in a dataset.i want equal observation into 4 datasets?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/I-am-having-n-observation-in-a-dataset-i-want-equal-observation/m-p/621392#M182675</link>
      <description>&lt;P&gt;This code will work.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want1 want2 want3 want4;
  set have nobs=nrecs;
  
  select (ceil(_n_/(nrecs/4)));
    when (1) output want1;
    when (2) output want2;
    when (3) output want3;
    otherwise output want4;
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;It uses the SET statement option&amp;nbsp; NOBS=, which stores the number of obs in HAVE in variable nrecs. And it also uses the automatic variable _N_ which is the "iteration" number in the data step - equivalent to the obs number in this program.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Now say nrecs=100,&amp;nbsp; then nrecs/4=25&amp;nbsp; and _n_/(nrecs/4) ranges from .004, .008,... ,1, 1.004, 1.008, … 4.. And the CEIL functions rounds up to 1,2,3, or 4..&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Extra observations are written to WANT4, then WANT3, then WANT2.&lt;/P&gt;</description>
      <pubDate>Fri, 31 Jan 2020 08:44:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/I-am-having-n-observation-in-a-dataset-i-want-equal-observation/m-p/621392#M182675</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2020-01-31T08:44:01Z</dc:date>
    </item>
    <item>
      <title>Re: I am having n observation in a dataset.i want equal observation into 4 datasets?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/I-am-having-n-observation-in-a-dataset-i-want-equal-observation/m-p/621393#M182676</link>
      <description>&lt;P&gt;Maybe something like this&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
do i=1 to 20;
output;
end;
run;

data want1 want2 want3 want4;
set have;
if _N_&amp;lt;=5 then output want1;
else if 5&amp;lt;_N_&amp;lt;=10 then output want2;
else if 10&amp;lt;_N_&amp;lt;=15 then output want3;
else if 15&amp;lt;_N_&amp;lt;=20 then output want4;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 31 Jan 2020 08:47:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/I-am-having-n-observation-in-a-dataset-i-want-equal-observation/m-p/621393#M182676</guid>
      <dc:creator>rudfaden</dc:creator>
      <dc:date>2020-01-31T08:47:27Z</dc:date>
    </item>
    <item>
      <title>Re: I am having n observation in a dataset.i want equal observation into 4 datasets?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/I-am-having-n-observation-in-a-dataset-i-want-equal-observation/m-p/621456#M182687</link>
      <description>&lt;P&gt;You can see that there are simple versions and there are complex (but more flexible) versions.&amp;nbsp; If you use a macro-based approach, here are a couple of changes to simplify and speed up the program.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The macro contains:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
		%do I=1 %to &amp;amp;num;
			if %eval(&amp;amp;n*(&amp;amp;i-1)) &amp;lt;_n_ &amp;lt;=%eval(&amp;amp;n*&amp;amp;I) then output SAMPLE_&amp;amp;I;
		%end;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;This would work equally well:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;

		if _n_ &amp;lt;= &amp;amp;n then output SAMPLE_1;
		%do I=2 %to &amp;amp;num;
			else if _n_ &amp;lt;= %eval(&amp;amp;n*&amp;amp;I) then output SAMPLE_&amp;amp;I;
		%end;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 31 Jan 2020 15:55:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/I-am-having-n-observation-in-a-dataset-i-want-equal-observation/m-p/621456#M182687</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2020-01-31T15:55:37Z</dc:date>
    </item>
  </channel>
</rss>

