<?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: Spliting a dataset based on row count in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Spliting-a-dataset-based-on-row-count/m-p/872921#M344891</link>
    <description>&lt;P&gt;Similar but less code:&lt;/P&gt;
&lt;PRE&gt;data have;
  input name $;
datalines;
abc
pdq
vvv
jhi
ccc
;
data want;
   set have nobs=num;
   group = (_n_ le round( (num/2),1) );
run;&lt;/PRE&gt;
&lt;P&gt;Assumes that the ORDER of the names in the data is important.&lt;/P&gt;
&lt;P&gt;Nobs= creates a temporary variable of the count of observations in the data set.&lt;/P&gt;
&lt;P&gt;Round( (num/2,1) ) rounds the division of the number of records divided by 2 to the nearest integer, so the 2.5 you would get from 5 records gets rounded to 3.&lt;/P&gt;
&lt;P&gt;_N_ is a counter for the number of iterations of the data step. In a simple action like this it will act as a row counter. so if the _N_ is less than or equal to that half way we will get 1 and 0 other wise as that is how SAS returns the values of logical comparisons: 1 for true, 0 for false.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Caution: this sort of algorithm doesn't extend well to a generic N number of groups. Proc Rank or Surveyselect might be better in a more complex selection.&lt;/P&gt;</description>
    <pubDate>Fri, 28 Apr 2023 20:44:08 GMT</pubDate>
    <dc:creator>ballardw</dc:creator>
    <dc:date>2023-04-28T20:44:08Z</dc:date>
    <item>
      <title>Spliting a dataset based on row count</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Spliting-a-dataset-based-on-row-count/m-p/872898#M344876</link>
      <description>&lt;P&gt;Suppose I have a list of new visitors sorted by last name descending. If I have an even number of visitors I want to send the top half to one salesperson and another to the other salesperson.&lt;BR /&gt;&lt;BR /&gt;Have:&lt;/P&gt;&lt;TABLE border="1"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;Zuniga&lt;/TD&gt;&lt;TD&gt;James&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;Utiga&lt;/TD&gt;&lt;TD&gt;Tom&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;Balfor&lt;/TD&gt;&lt;TD&gt;Jane&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;P&gt;Anderson&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;Jessica&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&lt;BR /&gt;Want:&lt;BR /&gt;Salesperson A gets:&lt;/P&gt;&lt;TABLE border="1"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;Zuniga&lt;/TD&gt;&lt;TD&gt;James&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;Utiga&lt;/TD&gt;&lt;TD&gt;Tom&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&lt;BR /&gt;Sales person B gets:&lt;/P&gt;&lt;TABLE border="1"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;Balfor&lt;/TD&gt;&lt;TD&gt;Jane&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;Anderson&lt;/TD&gt;&lt;TD&gt;Jessica&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&lt;BR /&gt;However, suppose I have and odd number of visitors. Since Salesperson A has a higher priority, I want them to get the extra visitor.&lt;BR /&gt;&lt;BR /&gt;Have:&lt;/P&gt;&lt;TABLE border="1"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;Zuniga&lt;/TD&gt;&lt;TD&gt;James&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;Utiga&lt;/TD&gt;&lt;TD&gt;Tom&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;Lemieux&lt;/TD&gt;&lt;TD&gt;Lynn&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;Balfor&lt;/TD&gt;&lt;TD&gt;Jane&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;P&gt;Anderson&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;Jessica&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&lt;BR /&gt;Want:&lt;BR /&gt;Salesperson A gets:&lt;/P&gt;&lt;TABLE border="1"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;Zuniga&lt;/TD&gt;&lt;TD&gt;James&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;Utiga&lt;/TD&gt;&lt;TD&gt;Tom&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;Lemieux&lt;/TD&gt;&lt;TD&gt;Lynn&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&lt;BR /&gt;Salesperson B gets:&lt;/P&gt;&lt;TABLE border="1"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;Balfor&lt;/TD&gt;&lt;TD&gt;Jane&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;P&gt;Anderson&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;Jessica&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 28 Apr 2023 18:33:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Spliting-a-dataset-based-on-row-count/m-p/872898#M344876</guid>
      <dc:creator>mmaleta851</dc:creator>
      <dc:date>2023-04-28T18:33:18Z</dc:date>
    </item>
    <item>
      <title>Re: Spliting a dataset based on row count</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Spliting-a-dataset-based-on-row-count/m-p/872906#M344879</link>
      <description>&lt;P&gt;Here is one way to accomplish your task:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data one; &lt;BR /&gt;input last $ first $; &lt;BR /&gt;cards; &lt;BR /&gt;Zuniga James &lt;BR /&gt;Utiga Tom &lt;BR /&gt;Lemieux Lynn &lt;BR /&gt;Balfor Jane &lt;BR /&gt;Anderson Jessica &lt;BR /&gt;; &lt;BR /&gt;&lt;BR /&gt;data A B; &lt;BR /&gt;set one nobs=obs; &lt;BR /&gt;half=obs/2; &lt;BR /&gt;if mod(obs,2)=0 then do; &lt;BR /&gt;if _n_ le half then output A; &lt;BR /&gt;else output B; &lt;BR /&gt;end; &lt;BR /&gt;else do; &lt;BR /&gt;if _n_ le ceil(half) then output A; &lt;BR /&gt;else output B; &lt;BR /&gt;end; &lt;BR /&gt;&lt;BR /&gt;run; &lt;BR /&gt;&lt;BR /&gt;proc print; &lt;BR /&gt;run; &lt;/P&gt;</description>
      <pubDate>Fri, 28 Apr 2023 19:14:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Spliting-a-dataset-based-on-row-count/m-p/872906#M344879</guid>
      <dc:creator>russt_sas</dc:creator>
      <dc:date>2023-04-28T19:14:10Z</dc:date>
    </item>
    <item>
      <title>Re: Spliting a dataset based on row count</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Spliting-a-dataset-based-on-row-count/m-p/872907#M344880</link>
      <description>&lt;P&gt;Don't split the dataset. Assign value of 0 to the top half and 1 to the bottom half (or something like that). Keep everything in one data set.&lt;/P&gt;</description>
      <pubDate>Fri, 28 Apr 2023 19:34:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Spliting-a-dataset-based-on-row-count/m-p/872907#M344880</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2023-04-28T19:34:58Z</dc:date>
    </item>
    <item>
      <title>Re: Spliting a dataset based on row count</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Spliting-a-dataset-based-on-row-count/m-p/872921#M344891</link>
      <description>&lt;P&gt;Similar but less code:&lt;/P&gt;
&lt;PRE&gt;data have;
  input name $;
datalines;
abc
pdq
vvv
jhi
ccc
;
data want;
   set have nobs=num;
   group = (_n_ le round( (num/2),1) );
run;&lt;/PRE&gt;
&lt;P&gt;Assumes that the ORDER of the names in the data is important.&lt;/P&gt;
&lt;P&gt;Nobs= creates a temporary variable of the count of observations in the data set.&lt;/P&gt;
&lt;P&gt;Round( (num/2,1) ) rounds the division of the number of records divided by 2 to the nearest integer, so the 2.5 you would get from 5 records gets rounded to 3.&lt;/P&gt;
&lt;P&gt;_N_ is a counter for the number of iterations of the data step. In a simple action like this it will act as a row counter. so if the _N_ is less than or equal to that half way we will get 1 and 0 other wise as that is how SAS returns the values of logical comparisons: 1 for true, 0 for false.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Caution: this sort of algorithm doesn't extend well to a generic N number of groups. Proc Rank or Surveyselect might be better in a more complex selection.&lt;/P&gt;</description>
      <pubDate>Fri, 28 Apr 2023 20:44:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Spliting-a-dataset-based-on-row-count/m-p/872921#M344891</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2023-04-28T20:44:08Z</dc:date>
    </item>
    <item>
      <title>Re: Spliting a dataset based on row count</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Spliting-a-dataset-based-on-row-count/m-p/872936#M344903</link>
      <description>&lt;P&gt;This worked. Thank you.&lt;/P&gt;</description>
      <pubDate>Fri, 28 Apr 2023 22:14:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Spliting-a-dataset-based-on-row-count/m-p/872936#M344903</guid>
      <dc:creator>mmaleta851</dc:creator>
      <dc:date>2023-04-28T22:14:08Z</dc:date>
    </item>
  </channel>
</rss>

