<?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: how to assign grouping seq number in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/how-to-assign-grouping-seq-number/m-p/906071#M357810</link>
    <description>&lt;P&gt;data step.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
	set ds1;
	by id;
	temp1=lag(stop);
	if 1=stop=temp1 then temp2=0; 
	else temp2=stop; 
	retain temp3; 
	if first.id then temp3 =temp2;
	else temp3= temp3+temp2;
	if stop=0 then sstop=.;
	else sstop=temp3;  
	drop temp:;
proc print; run; &lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Mon, 04 Dec 2023 18:08:49 GMT</pubDate>
    <dc:creator>A_Kh</dc:creator>
    <dc:date>2023-12-04T18:08:49Z</dc:date>
    <item>
      <title>how to assign grouping seq number</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-assign-grouping-seq-number/m-p/906065#M357809</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I would like to assign seq number for each id's each "stop" activity group .&amp;nbsp; if two stops are back to back, it will be counted as 1. how can I create "sseq" using example dataset ds1?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data ds1 ; &lt;BR /&gt;input id seq stop; &lt;BR /&gt;cards ; &lt;BR /&gt;1 1 0 &lt;BR /&gt;1 2 1 &lt;BR /&gt;1 3 0 &lt;BR /&gt;1 4 0 &lt;BR /&gt;1 5 1 &lt;BR /&gt;1 6 1&lt;BR /&gt;2 1 0 &lt;BR /&gt;2 2 0 &lt;BR /&gt;2 3 1 &lt;BR /&gt;2 4 1 &lt;BR /&gt;2 5 1 &lt;BR /&gt;2 6 0 &lt;BR /&gt;2 7 1 &lt;BR /&gt;2 8 0 &lt;BR /&gt;2 9 1 &lt;BR /&gt;; &lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The expected output:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="stataq_0-1701710045105.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/90753i6B6CC7920094D4AC/image-size/medium?v=v2&amp;amp;px=400" role="button" title="stataq_0-1701710045105.png" alt="stataq_0-1701710045105.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks.&lt;/P&gt;</description>
      <pubDate>Mon, 04 Dec 2023 17:16:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-assign-grouping-seq-number/m-p/906065#M357809</guid>
      <dc:creator>stataq</dc:creator>
      <dc:date>2023-12-04T17:16:19Z</dc:date>
    </item>
    <item>
      <title>Re: how to assign grouping seq number</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-assign-grouping-seq-number/m-p/906071#M357810</link>
      <description>&lt;P&gt;data step.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
	set ds1;
	by id;
	temp1=lag(stop);
	if 1=stop=temp1 then temp2=0; 
	else temp2=stop; 
	retain temp3; 
	if first.id then temp3 =temp2;
	else temp3= temp3+temp2;
	if stop=0 then sstop=.;
	else sstop=temp3;  
	drop temp:;
proc print; run; &lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 04 Dec 2023 18:08:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-assign-grouping-seq-number/m-p/906071#M357810</guid>
      <dc:creator>A_Kh</dc:creator>
      <dc:date>2023-12-04T18:08:49Z</dc:date>
    </item>
    <item>
      <title>Re: how to assign grouping seq number</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-assign-grouping-seq-number/m-p/906080#M357812</link>
      <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/448857"&gt;@stataq&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Try this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want(drop=_c);
set ds1;
by id seq;
if first.id then _c=0;
_c+(stop &amp;amp; (~lag(stop) | first.id));
if stop then sseq=_c;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;and add more test cases, e.g., id=3 beginning with stop=1 (if this is possible in your real data).&lt;/P&gt;</description>
      <pubDate>Mon, 04 Dec 2023 18:27:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-assign-grouping-seq-number/m-p/906080#M357812</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2023-12-04T18:27:23Z</dc:date>
    </item>
    <item>
      <title>Re: how to assign grouping seq number</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-assign-grouping-seq-number/m-p/945078#M370280</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/32733"&gt;@FreelanceReinh&lt;/a&gt;&amp;nbsp;Could you further explain&amp;nbsp;&lt;/P&gt;
&lt;PRE class="language-sas"&gt;&lt;CODE&gt;_c+(stop &amp;amp; (~lag(stop) | first.id));&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I have some difficulty to understand this logic.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks.&lt;/P&gt;</description>
      <pubDate>Tue, 24 Sep 2024 17:43:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-assign-grouping-seq-number/m-p/945078#M370280</guid>
      <dc:creator>stataq</dc:creator>
      <dc:date>2024-09-24T17:43:48Z</dc:date>
    </item>
    <item>
      <title>Re: how to assign grouping seq number</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-assign-grouping-seq-number/m-p/945081#M370281</link>
      <description>&lt;P&gt;Your case is a good example for a BY with the NOTSORTED option.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data ds1 ;
input id seq stop;
cards ;
1 1 0
1 2 1
1 3 0
1 4 0
1 5 1
1 6 1
2 1 0
2 2 0
2 3 1
2 4 1
2 5 1
2 6 0
2 7 1
2 8 0
2 9 1
;
run;

data ds1;
   set ds1;
   by id stop notsorted;
   if first.id then _sseq = 0;
   if first.stop and stop ne 0 then _sseq + 1;
   if stop ne 0 then sseq=_sseq;
   drop _sseq;
   run;
proc print;
   run;
 &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Capture.PNG" style="width: 165px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/100632i56DD95686389E8B7/image-size/large?v=v2&amp;amp;px=999" role="button" title="Capture.PNG" alt="Capture.PNG" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 24 Sep 2024 18:05:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-assign-grouping-seq-number/m-p/945081#M370281</guid>
      <dc:creator>data_null__</dc:creator>
      <dc:date>2024-09-24T18:05:15Z</dc:date>
    </item>
    <item>
      <title>Re: how to assign grouping seq number</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-assign-grouping-seq-number/m-p/945085#M370283</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/448857"&gt;@stataq&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/32733"&gt;@FreelanceReinh&lt;/a&gt;&amp;nbsp;Could you further explain&amp;nbsp;&lt;/P&gt;
&lt;PRE class="language-sas"&gt;&lt;CODE&gt;_c+(stop &amp;amp; (~lag(stop) | first.id));&lt;/CODE&gt;&lt;/PRE&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;First of all, this is a &lt;A href="https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lestmtsref/n1dfiqj146yi2cn1maeju9wo7ijs.htm" target="_blank" rel="noopener"&gt;sum statement&lt;/A&gt;, i.e., variable &lt;FONT face="courier new,courier"&gt;_c&lt;/FONT&gt; (the "counter") is incremented by the value in the outer parentheses. That increment is a &lt;EM&gt;Boolean value&lt;/EM&gt;: either &lt;FONT face="courier new,courier"&gt;1&lt;/FONT&gt; or &lt;FONT face="courier new,courier"&gt;0&lt;/FONT&gt;, depending on whether the logical expression involving the AND (&lt;FONT face="courier new,courier"&gt;&amp;amp;&lt;/FONT&gt;), OR (&lt;FONT face="courier new,courier"&gt;|&lt;/FONT&gt;) and NOT (&lt;FONT face="courier new,courier"&gt;~&lt;/FONT&gt;) operators is TRUE (&lt;FONT face="courier new,courier"&gt;1&lt;/FONT&gt;) or FALSE (&lt;FONT face="courier new,courier"&gt;0&lt;/FONT&gt;). Non-zero, non-missing values of variable &lt;FONT face="courier new,courier"&gt;stop&lt;/FONT&gt;&amp;nbsp;(in particular the value &lt;FONT face="courier new,courier"&gt;1&lt;/FONT&gt;) are evaluated to TRUE. Zero and missing values are evaluated to FALSE.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The LAG function in this DATA step is called once for each observation of dataset DS1, which means that it returns the value of &lt;FONT face="courier new,courier"&gt;stop&lt;/FONT&gt; from the previous observation (and a numeric missing value in the very first observation). The value of automatic variable &lt;FONT face="courier new,courier"&gt;first.id&lt;/FONT&gt; is &lt;FONT face="courier new,courier"&gt;1&lt;/FONT&gt; for the first observation of each &lt;FONT face="courier new,courier"&gt;id&lt;/FONT&gt; BY-group and &lt;FONT face="courier new,courier"&gt;0&lt;/FONT&gt; otherwise.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So, considering that &lt;FONT face="courier new,courier"&gt;stop&lt;/FONT&gt; has only values &lt;FONT face="courier new,courier"&gt;1&lt;/FONT&gt; or &lt;FONT face="courier new,courier"&gt;0&lt;/FONT&gt;, the increment equals&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;&lt;FONT face="courier new,courier"&gt;1&lt;/FONT&gt; if the current observation has &lt;FONT face="courier new,courier"&gt;stop=1&lt;/FONT&gt; AND (the previous observation has &lt;FONT face="courier new,courier"&gt;stop=0&lt;/FONT&gt; OR the current observation is the first of the current &lt;FONT face="courier new,courier"&gt;id&lt;/FONT&gt;)&lt;/LI&gt;
&lt;LI&gt;&lt;FONT face="courier new,courier"&gt;0&lt;/FONT&gt; otherwise.&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;This is exactly what we need: A new "block" of consecutive observations with &lt;FONT face="courier new,courier"&gt;stop=1&lt;/FONT&gt;&amp;nbsp;of an &lt;FONT face="courier new,courier"&gt;id&lt;/FONT&gt; must (obviously) start with an observation with&amp;nbsp;&lt;FONT face="courier new,courier"&gt;stop=1&lt;/FONT&gt; and the only exception to the requirement "the previous observation had &lt;FONT face="courier new,courier"&gt;stop=0&lt;/FONT&gt;" (avoiding an incrementation &lt;EM&gt;within&lt;/EM&gt; a block) is that we are at the first observation of the &lt;FONT face="courier new,courier"&gt;id&lt;/FONT&gt;. In the latter case the previous observation may be the last of a "&lt;FONT face="courier new,courier"&gt;stop=1&lt;/FONT&gt; block" of the previous &lt;FONT face="courier new,courier"&gt;id&lt;/FONT&gt;. Also, for the very first observation of dataset DS1 there is no previous observation, &lt;FONT face="courier new,courier"&gt;lag(stop)=.&lt;/FONT&gt; (missing, i.e. FALSE), hence &lt;FONT face="courier new,courier"&gt;~lag(stop)=1&lt;/FONT&gt; (TRUE), but this is actually irrelevant because&amp;nbsp;&lt;FONT face="courier new,courier"&gt;first.id=1&lt;/FONT&gt; makes the subexpression &lt;FONT face="courier new,courier"&gt;(~lag(stop) | first.id)&lt;/FONT&gt;&amp;nbsp;TRUE anyway.&lt;/P&gt;</description>
      <pubDate>Tue, 24 Sep 2024 18:46:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-assign-grouping-seq-number/m-p/945085#M370283</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2024-09-24T18:46:31Z</dc:date>
    </item>
    <item>
      <title>Re: how to assign grouping seq number</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-assign-grouping-seq-number/m-p/945121#M370304</link>
      <description>&lt;P&gt;To get a variable with those missing values you will need an separate variable to keep the count.&amp;nbsp; So use the BY statement to find the groups. The SUM statement to increment the counter and an IF statement to assign the counter to create your wanted variable with the missing values otherwise.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set ds1;
  by id stop notsorted;
  if first.id then counter=0;
  counter + (first.stop and stop);
  if stop then sseq=counter;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Tom_0-1727218109356.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/100643iB591C23E9220033E/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Tom_0-1727218109356.png" alt="Tom_0-1727218109356.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 24 Sep 2024 22:55:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-assign-grouping-seq-number/m-p/945121#M370304</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2024-09-24T22:55:56Z</dc:date>
    </item>
  </channel>
</rss>

