<?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: Distribute items to groups in SAS/IML Software and Matrix Computations</title>
    <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Distribute-items-to-groups/m-p/788628#M5743</link>
    <description>&lt;P&gt;Very interesting. Thanks!&lt;/P&gt;</description>
    <pubDate>Thu, 06 Jan 2022 11:40:01 GMT</pubDate>
    <dc:creator>WeiChen</dc:creator>
    <dc:date>2022-01-06T11:40:01Z</dc:date>
    <item>
      <title>Distribute items to groups</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Distribute-items-to-groups/m-p/788504#M5741</link>
      <description>&lt;P&gt;I feel like this should be simpler. I have N items. I want to distribute them as equally as possible to G people. If G divides N evenly, everyone gets N/G. The following tries to distribute the N=137 items to G=10 peoploe. I want some people to get 14 and some to get 13.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;PROC IML;
N = 137;
G = 10;
/* split N items among G people? */
maxnum = ceil(N / G);
print maxnum;
items = j(G, 1);
do i = 1 to G;
   if N &amp;gt;= maxnum then
      items[i] = maxnum;
   else 
      items[i] = maxnum - 1;
   N = N - items[i];
end;
print items;
total = sum(items);   /* did it work? */
print total N;
QUIT;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;What am I doing wrong?&lt;/P&gt;</description>
      <pubDate>Wed, 05 Jan 2022 17:47:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Distribute-items-to-groups/m-p/788504#M5741</guid>
      <dc:creator>WeiChen</dc:creator>
      <dc:date>2022-01-05T17:47:41Z</dc:date>
    </item>
    <item>
      <title>Re: Distribute items to groups</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Distribute-items-to-groups/m-p/788508#M5742</link>
      <description>&lt;P&gt;Close, but your logic is not quite right.&lt;/P&gt;
&lt;P&gt;I wrote about this problem in the article, &lt;A href="https://blogs.sas.com/content/iml/2019/04/08/floor-mod-trick-items-to-groups.html" target="_self"&gt;"Use the FLOOR-MOD trick to allocate items to groups."&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;If you use the function in that article, you get the right answer:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc iml;
/* See 
https://blogs.sas.com/content/iml/2019/04/08/floor-mod-trick-items-to-groups.html

   B = total number of items or tasks (B &amp;gt;= 0, scalar)
   k = total number of groups or workers (k &amp;gt; 0, scalar)
   i = scalar or vector that specifies the group(s), max(i)&amp;lt;=k
   Return the number of items to allocate to the i_th group */
start AssignItems(B, k, i);
   n = floor(B / k) + (i &amp;lt;= mod(B, k));     /* the FLOOR-MOD trick */
   return(n);
finish;

N = 137;
G = 10;
/* split N items among G people? */
idx = T(1:G);
items = AssignItems(N, G, idx);
print items;
total = sum(items);   /* did it work? */
print total N;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;By the way, notice that you do not need to loop over the number of groups. The FLOOR-MOD trick handles the assignment as a vector operation.&lt;/P&gt;</description>
      <pubDate>Wed, 05 Jan 2022 17:59:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Distribute-items-to-groups/m-p/788508#M5742</guid>
      <dc:creator>Rick_SAS</dc:creator>
      <dc:date>2022-01-05T17:59:43Z</dc:date>
    </item>
    <item>
      <title>Re: Distribute items to groups</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Distribute-items-to-groups/m-p/788628#M5743</link>
      <description>&lt;P&gt;Very interesting. Thanks!&lt;/P&gt;</description>
      <pubDate>Thu, 06 Jan 2022 11:40:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Distribute-items-to-groups/m-p/788628#M5743</guid>
      <dc:creator>WeiChen</dc:creator>
      <dc:date>2022-01-06T11:40:01Z</dc:date>
    </item>
  </channel>
</rss>

