<?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: Loop through a column to identify groups in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Loop-through-a-column-to-identify-groups/m-p/822131#M324610</link>
    <description>&lt;P&gt;Something like this, perhaps?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data want;
  set have;
  by ID;
  if first.ID then group=1;
  else group+missing(flag);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;What it does:&lt;/P&gt;
&lt;P&gt;If we get the first ID, GROUP is initialized to 1. Else, we use the SUM statement (like "A+B") to add 1 (SAS sees the logical value TRUE as the number 1) when FLAG is missing. The SUM statement automatically retains the variable GROUP, so we do not have to use a RETAIN statement.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The SUM statement&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;group+missing(flag);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;is the same as&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if missing(flag) then group=sum(group,1);
retain group;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;- a very useful shorthand to learn if you are programming in SAS.&lt;/P&gt;</description>
    <pubDate>Thu, 07 Jul 2022 19:16:13 GMT</pubDate>
    <dc:creator>s_lassen</dc:creator>
    <dc:date>2022-07-07T19:16:13Z</dc:date>
    <item>
      <title>Loop through a column to identify groups</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Loop-through-a-column-to-identify-groups/m-p/822113#M324602</link>
      <description>&lt;P&gt;Hi all,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I want to identify groups through the column "flag" which takes values of missing and 1. When it is missing, it means that a new group starts from the observation; 1 means the same group as the last observation. Below is the sample data with the column "group" to be identified.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;TABLE style="border-collapse: collapse; width: 144pt;" border="0" width="192" cellspacing="0" cellpadding="0"&gt;
&lt;TBODY&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD width="64" height="20" class="xl64" style="height: 15.0pt; width: 48pt;"&gt;ID&lt;/TD&gt;
&lt;TD width="64" class="xl63" style="border-left: none; width: 48pt;"&gt;flag&lt;/TD&gt;
&lt;TD width="64" class="xl63" style="border-left: none; width: 48pt;"&gt;group&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD height="20" class="xl64" style="height: 15.0pt; border-top: none;"&gt;1&lt;/TD&gt;
&lt;TD class="xl65" style="border-top: none; border-left: none;"&gt;.&lt;/TD&gt;
&lt;TD class="xl64" style="border-top: none; border-left: none;"&gt;1&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD height="20" class="xl64" style="height: 15.0pt; border-top: none;"&gt;1&lt;/TD&gt;
&lt;TD class="xl65" style="border-top: none; border-left: none;"&gt;1&lt;/TD&gt;
&lt;TD class="xl64" style="border-top: none; border-left: none;"&gt;1&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD height="20" class="xl64" style="height: 15.0pt; border-top: none;"&gt;1&lt;/TD&gt;
&lt;TD class="xl65" style="border-top: none; border-left: none;"&gt;1&lt;/TD&gt;
&lt;TD class="xl64" style="border-top: none; border-left: none;"&gt;1&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD height="20" class="xl64" style="height: 15.0pt; border-top: none;"&gt;1&lt;/TD&gt;
&lt;TD class="xl65" style="border-top: none; border-left: none;"&gt;1&lt;/TD&gt;
&lt;TD class="xl64" style="border-top: none; border-left: none;"&gt;1&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD height="20" class="xl64" style="height: 15.0pt; border-top: none;"&gt;1&lt;/TD&gt;
&lt;TD class="xl65" style="border-top: none; border-left: none;"&gt;.&lt;/TD&gt;
&lt;TD class="xl64" style="border-top: none; border-left: none;"&gt;2&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD height="20" class="xl64" style="height: 15.0pt; border-top: none;"&gt;1&lt;/TD&gt;
&lt;TD class="xl65" style="border-top: none; border-left: none;"&gt;1&lt;/TD&gt;
&lt;TD class="xl64" style="border-top: none; border-left: none;"&gt;2&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD height="20" class="xl64" style="height: 15.0pt; border-top: none;"&gt;2&lt;/TD&gt;
&lt;TD class="xl65" style="border-top: none; border-left: none;"&gt;.&lt;/TD&gt;
&lt;TD class="xl64" style="border-top: none; border-left: none;"&gt;1&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD height="20" class="xl64" style="height: 15.0pt; border-top: none;"&gt;2&lt;/TD&gt;
&lt;TD class="xl65" style="border-top: none; border-left: none;"&gt;1&lt;/TD&gt;
&lt;TD class="xl64" style="border-top: none; border-left: none;"&gt;1&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD height="20" class="xl64" style="height: 15.0pt; border-top: none;"&gt;2&lt;/TD&gt;
&lt;TD class="xl65" style="border-top: none; border-left: none;"&gt;1&lt;/TD&gt;
&lt;TD class="xl64" style="border-top: none; border-left: none;"&gt;1&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD height="20" class="xl64" style="height: 15.0pt; border-top: none;"&gt;3&lt;/TD&gt;
&lt;TD class="xl65" style="border-top: none; border-left: none;"&gt;.&lt;/TD&gt;
&lt;TD class="xl64" style="border-top: none; border-left: none;"&gt;1&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD height="20" class="xl64" style="height: 15.0pt; border-top: none;"&gt;3&lt;/TD&gt;
&lt;TD class="xl65" style="border-top: none; border-left: none;"&gt;1&lt;/TD&gt;
&lt;TD class="xl64" style="border-top: none; border-left: none;"&gt;1&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD height="20" class="xl64" style="height: 15.0pt; border-top: none;"&gt;3&lt;/TD&gt;
&lt;TD class="xl65" style="border-top: none; border-left: none;"&gt;.&lt;/TD&gt;
&lt;TD class="xl64" style="border-top: none; border-left: none;"&gt;2&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;</description>
      <pubDate>Thu, 07 Jul 2022 18:31:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Loop-through-a-column-to-identify-groups/m-p/822113#M324602</guid>
      <dc:creator>lizzy28</dc:creator>
      <dc:date>2022-07-07T18:31:43Z</dc:date>
    </item>
    <item>
      <title>Re: Loop through a column to identify groups</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Loop-through-a-column-to-identify-groups/m-p/822129#M324608</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set have;
by id;
retain group;
if first.id then group=0;
if missing(Flag) then group+1;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/52727"&gt;@lizzy28&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hi all,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I want to identify groups through the column "flag" which takes values of missing and 1. When it is missing, it means that a new group starts from the observation; 1 means the same group as the last observation. Below is the sample data with the column "group" to be identified.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;TABLE style="border-collapse: collapse; width: 144pt;" border="0" width="192" cellspacing="0" cellpadding="0"&gt;
&lt;TBODY&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD width="64" height="20" class="xl64" style="height: 15.0pt; width: 48pt;"&gt;ID&lt;/TD&gt;
&lt;TD width="64" class="xl63" style="border-left: none; width: 48pt;"&gt;flag&lt;/TD&gt;
&lt;TD width="64" class="xl63" style="border-left: none; width: 48pt;"&gt;group&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD height="20" class="xl64" style="height: 15.0pt; border-top: none;"&gt;1&lt;/TD&gt;
&lt;TD class="xl65" style="border-top: none; border-left: none;"&gt;.&lt;/TD&gt;
&lt;TD class="xl64" style="border-top: none; border-left: none;"&gt;1&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD height="20" class="xl64" style="height: 15.0pt; border-top: none;"&gt;1&lt;/TD&gt;
&lt;TD class="xl65" style="border-top: none; border-left: none;"&gt;1&lt;/TD&gt;
&lt;TD class="xl64" style="border-top: none; border-left: none;"&gt;1&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD height="20" class="xl64" style="height: 15.0pt; border-top: none;"&gt;1&lt;/TD&gt;
&lt;TD class="xl65" style="border-top: none; border-left: none;"&gt;1&lt;/TD&gt;
&lt;TD class="xl64" style="border-top: none; border-left: none;"&gt;1&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD height="20" class="xl64" style="height: 15.0pt; border-top: none;"&gt;1&lt;/TD&gt;
&lt;TD class="xl65" style="border-top: none; border-left: none;"&gt;1&lt;/TD&gt;
&lt;TD class="xl64" style="border-top: none; border-left: none;"&gt;1&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD height="20" class="xl64" style="height: 15.0pt; border-top: none;"&gt;1&lt;/TD&gt;
&lt;TD class="xl65" style="border-top: none; border-left: none;"&gt;.&lt;/TD&gt;
&lt;TD class="xl64" style="border-top: none; border-left: none;"&gt;2&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD height="20" class="xl64" style="height: 15.0pt; border-top: none;"&gt;1&lt;/TD&gt;
&lt;TD class="xl65" style="border-top: none; border-left: none;"&gt;1&lt;/TD&gt;
&lt;TD class="xl64" style="border-top: none; border-left: none;"&gt;2&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD height="20" class="xl64" style="height: 15.0pt; border-top: none;"&gt;2&lt;/TD&gt;
&lt;TD class="xl65" style="border-top: none; border-left: none;"&gt;.&lt;/TD&gt;
&lt;TD class="xl64" style="border-top: none; border-left: none;"&gt;1&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD height="20" class="xl64" style="height: 15.0pt; border-top: none;"&gt;2&lt;/TD&gt;
&lt;TD class="xl65" style="border-top: none; border-left: none;"&gt;1&lt;/TD&gt;
&lt;TD class="xl64" style="border-top: none; border-left: none;"&gt;1&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD height="20" class="xl64" style="height: 15.0pt; border-top: none;"&gt;2&lt;/TD&gt;
&lt;TD class="xl65" style="border-top: none; border-left: none;"&gt;1&lt;/TD&gt;
&lt;TD class="xl64" style="border-top: none; border-left: none;"&gt;1&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD height="20" class="xl64" style="height: 15.0pt; border-top: none;"&gt;3&lt;/TD&gt;
&lt;TD class="xl65" style="border-top: none; border-left: none;"&gt;.&lt;/TD&gt;
&lt;TD class="xl64" style="border-top: none; border-left: none;"&gt;1&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD height="20" class="xl64" style="height: 15.0pt; border-top: none;"&gt;3&lt;/TD&gt;
&lt;TD class="xl65" style="border-top: none; border-left: none;"&gt;1&lt;/TD&gt;
&lt;TD class="xl64" style="border-top: none; border-left: none;"&gt;1&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD height="20" class="xl64" style="height: 15.0pt; border-top: none;"&gt;3&lt;/TD&gt;
&lt;TD class="xl65" style="border-top: none; border-left: none;"&gt;.&lt;/TD&gt;
&lt;TD class="xl64" style="border-top: none; border-left: none;"&gt;2&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 07 Jul 2022 19:13:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Loop-through-a-column-to-identify-groups/m-p/822129#M324608</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2022-07-07T19:13:50Z</dc:date>
    </item>
    <item>
      <title>Re: Loop through a column to identify groups</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Loop-through-a-column-to-identify-groups/m-p/822131#M324610</link>
      <description>&lt;P&gt;Something like this, perhaps?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data want;
  set have;
  by ID;
  if first.ID then group=1;
  else group+missing(flag);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;What it does:&lt;/P&gt;
&lt;P&gt;If we get the first ID, GROUP is initialized to 1. Else, we use the SUM statement (like "A+B") to add 1 (SAS sees the logical value TRUE as the number 1) when FLAG is missing. The SUM statement automatically retains the variable GROUP, so we do not have to use a RETAIN statement.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The SUM statement&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;group+missing(flag);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;is the same as&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if missing(flag) then group=sum(group,1);
retain group;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;- a very useful shorthand to learn if you are programming in SAS.&lt;/P&gt;</description>
      <pubDate>Thu, 07 Jul 2022 19:16:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Loop-through-a-column-to-identify-groups/m-p/822131#M324610</guid>
      <dc:creator>s_lassen</dc:creator>
      <dc:date>2022-07-07T19:16:13Z</dc:date>
    </item>
  </channel>
</rss>

