<?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 How to group by different values of a variable and number them in sequence in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-group-by-different-values-of-a-variable-and-number-them/m-p/831380#M328513</link>
    <description>&lt;P&gt;Now I have a&amp;nbsp;simplified&amp;nbsp;example.&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Snipaste_2022-09-01_17-33-37.png" style="width: 141px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/74927i56D76EB6B1A960F5/image-size/small?v=v2&amp;amp;px=200" role="button" title="Snipaste_2022-09-01_17-33-37.png" alt="Snipaste_2022-09-01_17-33-37.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;As in the picture above, there are two variables. The variable y is the&amp;nbsp;original data and variable x is the order variable I want.&amp;nbsp;&lt;/P&gt;&lt;P&gt;You can see, y has a cretain&amp;nbsp;regular pattern that most of its data follow 'cc bb aa cc bb aa..'(Notice that obs 11, for some reason, there's only one 'a'. And that's the only unsual pattern we considered.).&amp;nbsp;&lt;/P&gt;&lt;P&gt;So, when I want a sequence for every two different y-values a number, it becomes a problem.&lt;/P&gt;&lt;P&gt;I try to use RETAIN statment, but as a fresh hand, I really need some support.&lt;/P&gt;&lt;P&gt;Thanks for your reading, and hope you can do me a favor.&lt;/P&gt;</description>
    <pubDate>Thu, 01 Sep 2022 09:59:45 GMT</pubDate>
    <dc:creator>Chauncy</dc:creator>
    <dc:date>2022-09-01T09:59:45Z</dc:date>
    <item>
      <title>How to group by different values of a variable and number them in sequence</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-group-by-different-values-of-a-variable-and-number-them/m-p/831380#M328513</link>
      <description>&lt;P&gt;Now I have a&amp;nbsp;simplified&amp;nbsp;example.&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Snipaste_2022-09-01_17-33-37.png" style="width: 141px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/74927i56D76EB6B1A960F5/image-size/small?v=v2&amp;amp;px=200" role="button" title="Snipaste_2022-09-01_17-33-37.png" alt="Snipaste_2022-09-01_17-33-37.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;As in the picture above, there are two variables. The variable y is the&amp;nbsp;original data and variable x is the order variable I want.&amp;nbsp;&lt;/P&gt;&lt;P&gt;You can see, y has a cretain&amp;nbsp;regular pattern that most of its data follow 'cc bb aa cc bb aa..'(Notice that obs 11, for some reason, there's only one 'a'. And that's the only unsual pattern we considered.).&amp;nbsp;&lt;/P&gt;&lt;P&gt;So, when I want a sequence for every two different y-values a number, it becomes a problem.&lt;/P&gt;&lt;P&gt;I try to use RETAIN statment, but as a fresh hand, I really need some support.&lt;/P&gt;&lt;P&gt;Thanks for your reading, and hope you can do me a favor.&lt;/P&gt;</description>
      <pubDate>Thu, 01 Sep 2022 09:59:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-group-by-different-values-of-a-variable-and-number-them/m-p/831380#M328513</guid>
      <dc:creator>Chauncy</dc:creator>
      <dc:date>2022-09-01T09:59:45Z</dc:date>
    </item>
    <item>
      <title>Re: How to group by different values of a variable and number them in sequence</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-group-by-different-values-of-a-variable-and-number-them/m-p/831383#M328515</link>
      <description>&lt;P&gt;Posting the code you have (using the little-running-man icon) would allow us to suggest code that fixes what you have, instead of posting something new.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;x needs to be incremented each second time that the value of y changes. So, you need to retain: x, the last value of y and another variable, i call it c holding the number of changes.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
   set have;
   retain x c _y;
   
   if _n_ = 1 then do;
      x = 1;
      c = 0;
   end;
   else do;
      if _y ne y then do;
         c = c + 1;
         
         if c = 2 then do;
            x = x + 1;
            c = 0;
         end;
      end;
   end;
   
   _y = y;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 01 Sep 2022 10:29:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-group-by-different-values-of-a-variable-and-number-them/m-p/831383#M328515</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2022-09-01T10:29:37Z</dc:date>
    </item>
    <item>
      <title>Re: How to group by different values of a variable and number them in sequence</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-group-by-different-values-of-a-variable-and-number-them/m-p/831388#M328517</link>
      <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/432792"&gt;@Chauncy&lt;/a&gt;&amp;nbsp;and welcome to the SAS Support Communities!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Once you have learned about &lt;A href="https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lepg/p0cuw49saqdduvn10a38f4m0zp7m.htm" target="_blank" rel="noopener"&gt;BY-group processing&lt;/A&gt;, the &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;&amp;nbsp;and the &lt;A href="https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lefunctionsref/p0ebxby8k7049en12wrfx28vyx4t.htm" target="_blank" rel="noopener"&gt;MOD function&lt;/A&gt;, you will be able to implement the solution presented by&amp;nbsp;&lt;A href="https://communities.sas.com/t5/user/viewprofilepage/user-id/15475" target="_blank" rel="noopener"&gt;andreas_lds&lt;/A&gt;&amp;nbsp;in a more concise DATA step:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want(drop=c);
set have;
by y notsorted;
if first.y then do;
  c+1;
  x+mod(c,2);
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;At the first observation of each group of equal &lt;FONT face="courier new,courier"&gt;y&lt;/FONT&gt; values ("&lt;FONT face="courier new,courier"&gt;if first.y&lt;/FONT&gt; ..."), the counter &lt;FONT face="courier new,courier"&gt;c&lt;/FONT&gt; is incremented ("&lt;FONT face="courier new,courier"&gt;c+1;&lt;/FONT&gt;"). If the value of &lt;FONT face="courier new,courier"&gt;c&lt;/FONT&gt; is odd (1, 3, 5, ...), variable&amp;nbsp;&lt;FONT face="courier new,courier"&gt;x&lt;/FONT&gt; is incremented by 1 (=&lt;FONT face="courier new,courier"&gt;mod(c,2)&lt;/FONT&gt;) as well, otherwise its value doesn't change because the increment &lt;FONT face="courier new,courier"&gt;mod(c,2)&lt;/FONT&gt;&amp;nbsp;is zero. For both &lt;FONT face="courier new,courier"&gt;c&lt;/FONT&gt; and &lt;FONT face="courier new,courier"&gt;x&lt;/FONT&gt; the initial values are 0 and they are retained by virtue of the sum statement. As variable&amp;nbsp;&lt;FONT face="courier new,courier"&gt;c&lt;/FONT&gt; is not needed in dataset WANT, I have dropped it using the &lt;A href="https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/ledsoptsref/n15goor3q758g5n1eykstufkpdhy.htm" target="_blank" rel="noopener"&gt;DROP= dataset option&lt;/A&gt;&amp;nbsp;(but you may want to see it for better understanding).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 01 Sep 2022 11:25:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-group-by-different-values-of-a-variable-and-number-them/m-p/831388#M328517</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2022-09-01T11:25:04Z</dc:date>
    </item>
    <item>
      <title>Re: How to group by different values of a variable and number them in sequence</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-group-by-different-values-of-a-variable-and-number-them/m-p/831494#M328587</link>
      <description>&lt;P&gt;Thank you for your help! Your code really does what I needed. Comparing y and _y(previous y value) is a new idea for me. This gave me a deeper understanding of the underlying mechanism of data-step and retian statement.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 02 Sep 2022 02:15:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-group-by-different-values-of-a-variable-and-number-them/m-p/831494#M328587</guid>
      <dc:creator>Chauncy</dc:creator>
      <dc:date>2022-09-02T02:15:13Z</dc:date>
    </item>
    <item>
      <title>Re: How to group by different values of a variable and number them in sequence</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-group-by-different-values-of-a-variable-and-number-them/m-p/831498#M328591</link>
      <description>&lt;P&gt;Thank you for your help ! Your code works, of course! The notsorted option is brand new for me, though I have learned BY statement. Looks like it could simplify many problems.&lt;/P&gt;</description>
      <pubDate>Fri, 02 Sep 2022 02:28:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-group-by-different-values-of-a-variable-and-number-them/m-p/831498#M328591</guid>
      <dc:creator>Chauncy</dc:creator>
      <dc:date>2022-09-02T02:28:57Z</dc:date>
    </item>
  </channel>
</rss>

