<?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 Trying to split a dataset . in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Trying-to-split-a-dataset/m-p/597604#M172208</link>
    <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Hi All,&lt;/P&gt;
&lt;P&gt;I am trying to just extract 10 observations for a dataset and chose to do it in the below method.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But it does not seem to work . Can you help me with my logic below&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also before using the do while&amp;nbsp; i tried using do until but the code would not process . i put put statements and looked in the log&lt;/P&gt;
&lt;P&gt;but do until would not even go past 'point 1' ..&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;thanks .&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data test;&lt;BR /&gt;set sashelp.cars;&lt;BR /&gt;counter+1;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data test_out;&lt;BR /&gt;set test;&lt;BR /&gt;put 'point1';&lt;/P&gt;
&lt;P&gt;do while (counter =&amp;lt;10);&lt;BR /&gt;output;&lt;BR /&gt;put 'point2';&lt;BR /&gt;counter +1;&lt;BR /&gt;end;&lt;BR /&gt;run;&lt;/P&gt;</description>
    <pubDate>Fri, 18 Oct 2019 04:52:16 GMT</pubDate>
    <dc:creator>dennis_oz</dc:creator>
    <dc:date>2019-10-18T04:52:16Z</dc:date>
    <item>
      <title>Trying to split a dataset .</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Trying-to-split-a-dataset/m-p/597604#M172208</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Hi All,&lt;/P&gt;
&lt;P&gt;I am trying to just extract 10 observations for a dataset and chose to do it in the below method.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But it does not seem to work . Can you help me with my logic below&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also before using the do while&amp;nbsp; i tried using do until but the code would not process . i put put statements and looked in the log&lt;/P&gt;
&lt;P&gt;but do until would not even go past 'point 1' ..&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;thanks .&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data test;&lt;BR /&gt;set sashelp.cars;&lt;BR /&gt;counter+1;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data test_out;&lt;BR /&gt;set test;&lt;BR /&gt;put 'point1';&lt;/P&gt;
&lt;P&gt;do while (counter =&amp;lt;10);&lt;BR /&gt;output;&lt;BR /&gt;put 'point2';&lt;BR /&gt;counter +1;&lt;BR /&gt;end;&lt;BR /&gt;run;&lt;/P&gt;</description>
      <pubDate>Fri, 18 Oct 2019 04:52:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Trying-to-split-a-dataset/m-p/597604#M172208</guid>
      <dc:creator>dennis_oz</dc:creator>
      <dc:date>2019-10-18T04:52:16Z</dc:date>
    </item>
    <item>
      <title>Re: Trying to split a dataset .</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Trying-to-split-a-dataset/m-p/597605#M172209</link>
      <description>&lt;P&gt;You simply want to extract the first 10 obs from a data set, correct?&lt;/P&gt;</description>
      <pubDate>Fri, 18 Oct 2019 04:54:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Trying-to-split-a-dataset/m-p/597605#M172209</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2019-10-18T04:54:42Z</dc:date>
    </item>
    <item>
      <title>Re: Trying to split a dataset .</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Trying-to-split-a-dataset/m-p/597612#M172211</link>
      <description>yes , if possible using do while&lt;BR /&gt;</description>
      <pubDate>Fri, 18 Oct 2019 05:21:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Trying-to-split-a-dataset/m-p/597612#M172211</guid>
      <dc:creator>dennis_oz</dc:creator>
      <dc:date>2019-10-18T05:21:14Z</dc:date>
    </item>
    <item>
      <title>Re: Trying to split a dataset .</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Trying-to-split-a-dataset/m-p/597614#M172212</link>
      <description>&lt;P&gt;You can use Do While like this&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
    set sashelp.cars;
    do while (c &amp;lt; 10);
        output;
        c+1;
    end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;However, problems like this are easier solved with the automatic _N_ variable&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
    set sashelp.cars;
    if _N_ &amp;lt;= 10;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 18 Oct 2019 05:33:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Trying-to-split-a-dataset/m-p/597614#M172212</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2019-10-18T05:33:49Z</dc:date>
    </item>
    <item>
      <title>Re: Trying to split a dataset .</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Trying-to-split-a-dataset/m-p/597616#M172214</link>
      <description>&lt;P&gt;You don't need a counter, sas has its own implicit counter _N_:&lt;/P&gt;
&lt;P&gt;You simply do:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have(obs=10);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;or if you insist use do while then&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
       do while _N_ le 10;
            output;
      end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;or&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
   set have;
        counter +1;
        do while counter le 10;
             output;
       end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;in your code you probably got 10 times same first observation.&lt;/P&gt;</description>
      <pubDate>Fri, 18 Oct 2019 05:40:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Trying-to-split-a-dataset/m-p/597616#M172214</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2019-10-18T05:40:04Z</dc:date>
    </item>
    <item>
      <title>Re: Trying to split a dataset .</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Trying-to-split-a-dataset/m-p/597647#M172231</link>
      <description>&lt;P&gt;Note that some responses output the first observation 10 times and some require brackets around conditions.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If you really want the first 10 observations output, as opposed to the first observation output 10 times, using a do-while or a do-until loop then the set statement should be in the loop so that a new observation is read from the input data set in each iteration of the loop.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;As you might already be aware, the SAS data step automatically iterates over a data set in a set statement without the need for explicit looping code. In which case my preferred solution is the &lt;FONT face="courier new,courier"&gt;obs=10&lt;/FONT&gt; data set option demonstrated by&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/88384"&gt;@Shmuel&lt;/a&gt;.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Otherwise, if you are learning about loops and how they can be used to read data sets then you could try something like:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* do-until loop */
data want(drop = counter);
   do until(counter ge 10);
      set sashelp.class;
      output;
      counter + 1;
   end;

   /* prevent any more data being read */
   stop;
run;


/* do-while loop */
data want(drop = counter);
   counter + 1;

   do while(counter le 10);
      set sashelp.class;
      output;
      counter + 1;
   end;

   /* prevent infinite looping */
   stop;
run;

/* do-iterative loop */
data want(drop = counter);
   do counter = 1 to 10;
      set sashelp.class;
      output;
   end;

   /* prevent any more data being read */
   stop;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Amir.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 18 Oct 2019 09:01:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Trying-to-split-a-dataset/m-p/597647#M172231</guid>
      <dc:creator>Amir</dc:creator>
      <dc:date>2019-10-18T09:01:21Z</dc:date>
    </item>
    <item>
      <title>Re: Trying to split a dataset .</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Trying-to-split-a-dataset/m-p/597832#M172295</link>
      <description>&lt;P&gt;If you want to use a specific set of &lt;STRONG&gt;sequential&lt;/STRONG&gt; observations in almost&amp;nbsp;any procedure there is no need to create an additional data set at all. Data set options FIRSTOBS and OBS will do it.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;proc print data=sashelp.cars(firstobs=1 obs=10);
run;

proc print data=sashelp.cars(firstobs=5 obs=15);
run;&lt;/PRE&gt;</description>
      <pubDate>Fri, 18 Oct 2019 21:06:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Trying-to-split-a-dataset/m-p/597832#M172295</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2019-10-18T21:06:06Z</dc:date>
    </item>
    <item>
      <title>Re: Trying to split a dataset .</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Trying-to-split-a-dataset/m-p/597866#M172310</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/19264"&gt;@dennis_oz&lt;/a&gt;:&lt;/P&gt;
&lt;P&gt;You can do both WHILE or UNTIL, and the auto-dropped counter _ N_ (or _IORC_ if you wish) can be incorporated into the loop specifications:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want ;                                                                                                                             
  do _n_ = 1 by 1 while (_n_ &amp;lt;= 10) ;                                                                                                   
* do _n_ = 1 by 1 until (_n_  = 10) ;                                                                                                   
    set sashelp.class ;                                                                                                                 
    output ;                                                                                                                            
  end ;                                                                                                                                 
  stop ;                                                                                                                                
run ;  
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;However, your requirement of using WHILE or UNTIL doesn't make much sense because if you use FROM-TO specification, WHILE is applied implicitly:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want ;                                                                                                                             
  do _n_ = 1 to 10 ;                                                                                                                    
    set sashelp.class ;                                                                                                                 
    output ;                                                                                                                            
  end ;                                                                                                                                 
  stop ;                                                                                                                                
run ;  
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I agree with other responders that for a thing like that, you only need the OBS=10 data set option. Or, if you need to select N consecutive records from anywhere in the file, you can combine FIRSTOBS= with OBS=. For example, to select 10 records starting from record 5:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want ;                                                                                                                             
  set sashelp.class (firstobs=5 obs=14) ;                                                                                               
run ;                                         
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Kind regards&lt;/P&gt;
&lt;P&gt;Paul D.&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 19 Oct 2019 03:53:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Trying-to-split-a-dataset/m-p/597866#M172310</guid>
      <dc:creator>hashman</dc:creator>
      <dc:date>2019-10-19T03:53:37Z</dc:date>
    </item>
  </channel>
</rss>

