<?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 TO split dataset by loop and obs should not be in another dataaset in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/TO-split-dataset-by-loop-and-obs-should-not-be-in-another/m-p/46109#M12229</link>
    <description>Hi i will get  dataset i dont know the number of observastions are there i want to divide it in to 5 dataset&lt;BR /&gt;
and name them as d1 to d5 dynamically how can i do by loop and each dataset obs should not go to another dataset based on id variable&lt;BR /&gt;
ex:&lt;BR /&gt;
data test;&lt;BR /&gt;
input id;&lt;BR /&gt;
cards;&lt;BR /&gt;
1&lt;BR /&gt;
1&lt;BR /&gt;
2&lt;BR /&gt;
2&lt;BR /&gt;
2&lt;BR /&gt;
3&lt;BR /&gt;
3&lt;BR /&gt;
4&lt;BR /&gt;
5&lt;BR /&gt;
6&lt;BR /&gt;
6&lt;BR /&gt;
7&lt;BR /&gt;
7&lt;BR /&gt;
8&lt;BR /&gt;
run;&lt;BR /&gt;
output&lt;BR /&gt;
d1&lt;BR /&gt;
1&lt;BR /&gt;
1&lt;BR /&gt;
1&lt;BR /&gt;
d2&lt;BR /&gt;
2&lt;BR /&gt;
2&lt;BR /&gt;
3&lt;BR /&gt;
3&lt;BR /&gt;
d3&lt;BR /&gt;
4&lt;BR /&gt;
5&lt;BR /&gt;
d4&lt;BR /&gt;
6&lt;BR /&gt;
6&lt;BR /&gt;
d5&lt;BR /&gt;
7&lt;BR /&gt;
7&lt;BR /&gt;
8&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
As the dataset should divide in to 5 datasets and the id variable obs should be only in one dataset only for ex:d3 is having obs '4' the obs of that dataset should be only in d3 it sholuld not be in another dataset .The test dataset should divide in to 5 not exactly all the five dataset should be having the same no of obs it can vary can any one help me in this.</description>
    <pubDate>Thu, 07 Apr 2011 09:21:57 GMT</pubDate>
    <dc:creator>R_Win</dc:creator>
    <dc:date>2011-04-07T09:21:57Z</dc:date>
    <item>
      <title>TO split dataset by loop and obs should not be in another dataaset</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/TO-split-dataset-by-loop-and-obs-should-not-be-in-another/m-p/46109#M12229</link>
      <description>Hi i will get  dataset i dont know the number of observastions are there i want to divide it in to 5 dataset&lt;BR /&gt;
and name them as d1 to d5 dynamically how can i do by loop and each dataset obs should not go to another dataset based on id variable&lt;BR /&gt;
ex:&lt;BR /&gt;
data test;&lt;BR /&gt;
input id;&lt;BR /&gt;
cards;&lt;BR /&gt;
1&lt;BR /&gt;
1&lt;BR /&gt;
2&lt;BR /&gt;
2&lt;BR /&gt;
2&lt;BR /&gt;
3&lt;BR /&gt;
3&lt;BR /&gt;
4&lt;BR /&gt;
5&lt;BR /&gt;
6&lt;BR /&gt;
6&lt;BR /&gt;
7&lt;BR /&gt;
7&lt;BR /&gt;
8&lt;BR /&gt;
run;&lt;BR /&gt;
output&lt;BR /&gt;
d1&lt;BR /&gt;
1&lt;BR /&gt;
1&lt;BR /&gt;
1&lt;BR /&gt;
d2&lt;BR /&gt;
2&lt;BR /&gt;
2&lt;BR /&gt;
3&lt;BR /&gt;
3&lt;BR /&gt;
d3&lt;BR /&gt;
4&lt;BR /&gt;
5&lt;BR /&gt;
d4&lt;BR /&gt;
6&lt;BR /&gt;
6&lt;BR /&gt;
d5&lt;BR /&gt;
7&lt;BR /&gt;
7&lt;BR /&gt;
8&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
As the dataset should divide in to 5 datasets and the id variable obs should be only in one dataset only for ex:d3 is having obs '4' the obs of that dataset should be only in d3 it sholuld not be in another dataset .The test dataset should divide in to 5 not exactly all the five dataset should be having the same no of obs it can vary can any one help me in this.</description>
      <pubDate>Thu, 07 Apr 2011 09:21:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/TO-split-dataset-by-loop-and-obs-should-not-be-in-another/m-p/46109#M12229</guid>
      <dc:creator>R_Win</dc:creator>
      <dc:date>2011-04-07T09:21:57Z</dc:date>
    </item>
    <item>
      <title>Re: TO split dataset</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/TO-split-dataset-by-loop-and-obs-should-not-be-in-another/m-p/46110#M12230</link>
      <description>Not sure if there's a better way, but as a quick and dirty you could try something like this:&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
data test;&lt;BR /&gt;
format i 8.;&lt;BR /&gt;
do i = 1 to 10;&lt;BR /&gt;
    output;&lt;BR /&gt;
end;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
data a b c d e error;&lt;BR /&gt;
set test;&lt;BR /&gt;
select (mod(_n_, 5));&lt;BR /&gt;
    when (0) output a;&lt;BR /&gt;
    when (1) output b;&lt;BR /&gt;
    when (2) output c;&lt;BR /&gt;
    when (3) output d;&lt;BR /&gt;
    when (4) output e;&lt;BR /&gt;
    otherwise output error;&lt;BR /&gt;
end;&lt;BR /&gt;
run;</description>
      <pubDate>Thu, 07 Apr 2011 10:40:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/TO-split-dataset-by-loop-and-obs-should-not-be-in-another/m-p/46110#M12230</guid>
      <dc:creator>DF</dc:creator>
      <dc:date>2011-04-07T10:40:56Z</dc:date>
    </item>
    <item>
      <title>Re: TO split dataset</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/TO-split-dataset-by-loop-and-obs-should-not-be-in-another/m-p/46111#M12231</link>
      <description>Hi.&lt;BR /&gt;
I have to leave now.&lt;BR /&gt;
Give you some clue. in the dictionary table dictionary.tables ,there is a 'nobs' contains the table 's physical number of observations,You can calculated it by yourself to decide the number of obs for dataset d1 - d5 .&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
Ksharp</description>
      <pubDate>Thu, 07 Apr 2011 10:42:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/TO-split-dataset-by-loop-and-obs-should-not-be-in-another/m-p/46111#M12231</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2011-04-07T10:42:35Z</dc:date>
    </item>
    <item>
      <title>Re: TO split dataset</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/TO-split-dataset-by-loop-and-obs-should-not-be-in-another/m-p/46112#M12232</link>
      <description>I am not sure why the complete post is not visible. Please edit to view the complete post.&lt;BR /&gt;
I think this works:&lt;BR /&gt;
&lt;BR /&gt;
data a b c d e;&lt;BR /&gt;
set act.talent nobs=t;&lt;BR /&gt;
/*In my case I am interested in 5 data sets */&lt;BR /&gt;
t1=round(t/5);&lt;BR /&gt;
if _n_&amp;lt;=t1 then output a;&lt;BR /&gt;
else if _n_&amp;lt;=t1*2 then output b;&lt;BR /&gt;
else if _n_&amp;lt;=t1*3 then output c;&lt;BR /&gt;
else if _n_&amp;lt;=t1*4 then output d;&lt;BR /&gt;
else if _n_&amp;lt;=t1*5 then output e;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
The above code is useful when you want to create less number of datasets from the input data set.&lt;BR /&gt;
Its better to use the below macro code:&lt;BR /&gt;
&lt;BR /&gt;
%macro numofdatasets(no);&lt;BR /&gt;
%do i=1 %to &amp;amp;no;&lt;BR /&gt;
data _&amp;amp;i;&lt;BR /&gt;
set act.talent nobs=t;&lt;BR /&gt;
/*In my case I am interested in 3 data sets */&lt;BR /&gt;
t1=round(t/&amp;amp;no);&lt;BR /&gt;
if t1*(&amp;amp;i-1) &amp;lt; _n_ &amp;lt;=t1*&amp;amp;i then output _&amp;amp;i;&lt;BR /&gt;
run;&lt;BR /&gt;
%end;&lt;BR /&gt;
%mend;&lt;BR /&gt;
%numofdatasets(5);&lt;BR /&gt;
&lt;BR /&gt;
You can change the number of data set you want to create easily in the macro call. But the last data set will have less obervations if the number of observations is not a multiple of the number of datasets required.&lt;BR /&gt;
&lt;BR /&gt;
Message was edited by: SAS333&lt;BR /&gt;
&lt;BR /&gt;
Message was edited by: SAS333

Message was edited by: SAS333</description>
      <pubDate>Thu, 07 Apr 2011 19:41:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/TO-split-dataset-by-loop-and-obs-should-not-be-in-another/m-p/46112#M12232</guid>
      <dc:creator>SAS333</dc:creator>
      <dc:date>2011-04-07T19:41:51Z</dc:date>
    </item>
    <item>
      <title>Re: TO split dataset</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/TO-split-dataset-by-loop-and-obs-should-not-be-in-another/m-p/46113#M12233</link>
      <description>The forum doesn't like the symbols for "less than or equal to" etc. so it is best to use SAS's abbreviation's (lt, le, gt, ge, eq, ne).  Only you can edit your own posts.</description>
      <pubDate>Thu, 07 Apr 2011 19:52:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/TO-split-dataset-by-loop-and-obs-should-not-be-in-another/m-p/46113#M12233</guid>
      <dc:creator>RickM</dc:creator>
      <dc:date>2011-04-07T19:52:48Z</dc:date>
    </item>
    <item>
      <title>Re: TO split dataset</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/TO-split-dataset-by-loop-and-obs-should-not-be-in-another/m-p/46114#M12234</link>
      <description>Hello, &lt;BR /&gt;
&lt;BR /&gt;
Use &amp;lt; instead of &amp;lt; and &amp;gt; instead of &amp;gt; (Also add ; after &amp;lt; and ; after &amp;gt;). &lt;BR /&gt;
&lt;BR /&gt;
SPR&lt;BR /&gt;
&lt;BR /&gt;
Message was edited by: SPR&lt;BR /&gt;
&lt;BR /&gt;
Message was edited by: SPR&lt;BR /&gt;
&lt;BR /&gt;
Message was edited by: SPR

Message was edited by: SPR</description>
      <pubDate>Thu, 07 Apr 2011 21:00:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/TO-split-dataset-by-loop-and-obs-should-not-be-in-another/m-p/46114#M12234</guid>
      <dc:creator>SPR</dc:creator>
      <dc:date>2011-04-07T21:00:00Z</dc:date>
    </item>
    <item>
      <title>Re: TO split dataset by loop and obs should not be in another dataaset</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/TO-split-dataset-by-loop-and-obs-should-not-be-in-another/m-p/46115#M12235</link>
      <description>Emmmm.&lt;BR /&gt;
It is much complicated.&lt;BR /&gt;
Suppose your id variable is numeric.&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
[pre]&lt;BR /&gt;
data test;&lt;BR /&gt;
input id;&lt;BR /&gt;
cards;&lt;BR /&gt;
1&lt;BR /&gt;
1&lt;BR /&gt;
2&lt;BR /&gt;
2&lt;BR /&gt;
2&lt;BR /&gt;
3&lt;BR /&gt;
3&lt;BR /&gt;
4&lt;BR /&gt;
5&lt;BR /&gt;
6&lt;BR /&gt;
6&lt;BR /&gt;
7&lt;BR /&gt;
7&lt;BR /&gt;
8&lt;BR /&gt;
;&lt;BR /&gt;
run;&lt;BR /&gt;
ods output nlevels=level;&lt;BR /&gt;
proc freq data=test nlevels ;&lt;BR /&gt;
 table id /out=id(keep=id) nopercent nofreq nocum ;&lt;BR /&gt;
run;&lt;BR /&gt;
data temp(keep=id flag);&lt;BR /&gt;
 set id;&lt;BR /&gt;
 if _n_ eq 1 then set level;&lt;BR /&gt;
 retain flag 0;&lt;BR /&gt;
 div=floor(nlevels/5);&lt;BR /&gt;
 if div eq 1 then do;&lt;BR /&gt;
                  if mod(_n_,div) eq 0  and flag le 4 then flag+1;&lt;BR /&gt;
                  end;&lt;BR /&gt;
  else do;&lt;BR /&gt;
       if mod(_n_,div) eq 1  and flag le 4 then flag+1;&lt;BR /&gt;
       end;&lt;BR /&gt;
run;&lt;BR /&gt;
data op;&lt;BR /&gt;
 set temp;&lt;BR /&gt;
 by flag;&lt;BR /&gt;
 length level $ 200;&lt;BR /&gt;
 retain level;&lt;BR /&gt;
 if first.flag then call missing(level);&lt;BR /&gt;
 level=catx(' ',level,id);&lt;BR /&gt;
 if last.flag then output;&lt;BR /&gt;
run;&lt;BR /&gt;
data _null_;&lt;BR /&gt;
 set op;&lt;BR /&gt;
 call symputx(cats('d',flag),level);&lt;BR /&gt;
run;&lt;BR /&gt;
data d1 d2 d3 d4 d5;&lt;BR /&gt;
 set test;&lt;BR /&gt;
 select;&lt;BR /&gt;
  when (id in (&amp;amp;d1)) output d1;&lt;BR /&gt;
  when (id in (&amp;amp;d2)) output d2;&lt;BR /&gt;
  when (id in (&amp;amp;d3)) output d3;&lt;BR /&gt;
  when (id in (&amp;amp;d4)) output d4;&lt;BR /&gt;
  when (id in (&amp;amp;d5)) output d5;&lt;BR /&gt;
  otherwise;&lt;BR /&gt;
 end;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
[/pre]&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
Ksharp

Message was edited by: Ksharp</description>
      <pubDate>Mon, 11 Apr 2011 09:23:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/TO-split-dataset-by-loop-and-obs-should-not-be-in-another/m-p/46115#M12235</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2011-04-11T09:23:19Z</dc:date>
    </item>
  </channel>
</rss>

