<?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: Please help me group my data in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Please-help-me-group-my-data/m-p/792388#M253868</link>
    <description>&lt;P&gt;Something like this perhaps?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  do count=1 by 1 until(last.building_ID);
    set have(keep=building_ID);
    by Building_ID;
    end;
  group_ID+1;
  do _N_=1 by 1 until(last.building_ID);
    set have;
    by building_ID;
    output;
    if mod(_N_,12)=0 and count-_N_&amp;gt;12 then
      group_ID+1;
    end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The first DO loop just counts the number of apartments in each building, the second reads the data again and outputs, and for each 12 observations it adds to to the group_ID if there are enough apartments left.&lt;/P&gt;</description>
    <pubDate>Wed, 26 Jan 2022 08:42:38 GMT</pubDate>
    <dc:creator>s_lassen</dc:creator>
    <dc:date>2022-01-26T08:42:38Z</dc:date>
    <item>
      <title>Please help me group my data</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Please-help-me-group-my-data/m-p/792372#M253858</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a data set for apartments with these variables:&lt;/P&gt;&lt;P&gt;Building_id: ID for the building that the apartment is located in.&lt;/P&gt;&lt;P&gt;Apartment_id: ID for the apartment itself.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I would like to generate a variable, Group_id, that groups the apartments together in groups. These groups have to include a minimum of 12 apartments and these apartments need the same building_id.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So, if there are 23 apartments with the same building_id they would simply be in one group, because there are not enough apartments for 2 groups of 12. If there are 24 apartments with the same building_id, they would be in two groups of 12. If there are 25 apartments with the same building_id, they would be in two groups of 12 and 13.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Another example: if there are 100 apartments in the same building (with the same building_id), I want the first 12 to be grouped with one Group_id, 13-24 to be grouped with another Group_id, 25-36 with a third Group_id,..., and then the last apartments from 84-100 to be grouped together with the same Group_id. The last group includes 16 apartments (larger than 12), but if we grouped 84-96 together, then the last 4 apartments would be too few to be in a group, because of the 12 minimum rule.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;My data set include a lot of apartments with a lot of different building-id's, so I am hoping for a code that does this grouping automatically.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I hope this makes sense - please ask away if it doesn't. Hoping someone can help :-).&lt;/P&gt;</description>
      <pubDate>Wed, 26 Jan 2022 07:44:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Please-help-me-group-my-data/m-p/792372#M253858</guid>
      <dc:creator>niki0209</dc:creator>
      <dc:date>2022-01-26T07:44:42Z</dc:date>
    </item>
    <item>
      <title>Re: Please help me group my data</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Please-help-me-group-my-data/m-p/792373#M253859</link>
      <description>&lt;P&gt;See this (with simple numeric ID's):&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
building_id = 1;
do appartment_id = 1 to 23;
  output;
end;
building_id = 2;
do appartment_id = 1 to 24;
  output;
end;
building_id = 3;
do appartment_id = 1 to 25;
  output;
end;
run;

data want;
count = 0;
do until (last.building_id);
  set have;
  by building_id;
  count + 1;
end;
group_id = 1;
count2 = 0;
do until (last.building_id);
  set have;
  by building_id;
  count2 + 1;
  if mod(count2,12) = 0 and count - count2 ge 12 then group_id + 1;
  output;
end;
drop count count2;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 26 Jan 2022 07:54:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Please-help-me-group-my-data/m-p/792373#M253859</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2022-01-26T07:54:54Z</dc:date>
    </item>
    <item>
      <title>Re: Please help me group my data</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Please-help-me-group-my-data/m-p/792378#M253862</link>
      <description>Thank you very much Kurt. The code seems to be (almost) working.&lt;BR /&gt;I see, however, that the first group of each building_id contains only 11 apartments.&lt;BR /&gt;&lt;BR /&gt;Do you have any idea why that would be the case?</description>
      <pubDate>Wed, 26 Jan 2022 08:16:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Please-help-me-group-my-data/m-p/792378#M253862</guid>
      <dc:creator>niki0209</dc:creator>
      <dc:date>2022-01-26T08:16:18Z</dc:date>
    </item>
    <item>
      <title>Re: Please help me group my data</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Please-help-me-group-my-data/m-p/792380#M253863</link>
      <description>&lt;P&gt;With computers, one should always start counting at zero &lt;span class="lia-unicode-emoji" title=":winking_face:"&gt;😉&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
count = 0;
do until (last.building_id);
  set have;
  by building_id;
  count + 1;
end;
group_id = 0;
count2 = 0;
do until (last.building_id);
  set have;
  by building_id;
  if mod(count2,12) = 0 and count - count2 ge 12 then group_id + 1;
  count2 + 1;
  output;
end;
drop count count2;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 26 Jan 2022 08:21:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Please-help-me-group-my-data/m-p/792380#M253863</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2022-01-26T08:21:38Z</dc:date>
    </item>
    <item>
      <title>Re: Please help me group my data</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Please-help-me-group-my-data/m-p/792383#M253865</link>
      <description>Good point! Thank you so much. Works perfectly now. You've made my day 1000 times easier. Have a good one!</description>
      <pubDate>Wed, 26 Jan 2022 08:34:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Please-help-me-group-my-data/m-p/792383#M253865</guid>
      <dc:creator>niki0209</dc:creator>
      <dc:date>2022-01-26T08:34:18Z</dc:date>
    </item>
    <item>
      <title>Re: Please help me group my data</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Please-help-me-group-my-data/m-p/792388#M253868</link>
      <description>&lt;P&gt;Something like this perhaps?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  do count=1 by 1 until(last.building_ID);
    set have(keep=building_ID);
    by Building_ID;
    end;
  group_ID+1;
  do _N_=1 by 1 until(last.building_ID);
    set have;
    by building_ID;
    output;
    if mod(_N_,12)=0 and count-_N_&amp;gt;12 then
      group_ID+1;
    end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The first DO loop just counts the number of apartments in each building, the second reads the data again and outputs, and for each 12 observations it adds to to the group_ID if there are enough apartments left.&lt;/P&gt;</description>
      <pubDate>Wed, 26 Jan 2022 08:42:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Please-help-me-group-my-data/m-p/792388#M253868</guid>
      <dc:creator>s_lassen</dc:creator>
      <dc:date>2022-01-26T08:42:38Z</dc:date>
    </item>
  </channel>
</rss>

