<?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 the decremental seq and up to 3 in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/how-to-assign-the-decremental-seq-and-up-to-3/m-p/960622#M374628</link>
    <description>&lt;P&gt;Please provide the sample data in a usable form, as shown in these &lt;A href="https://blogs.sas.com/content/sastraining/2016/03/11/jedi-sas-tricks-data-to-data-step-macro/" target="_self"&gt;instructions and examples&lt;/A&gt;, and as &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/18408"&gt;@Ksharp&lt;/a&gt; showed in &lt;A href="https://communities.sas.com/t5/SAS-Programming/Assign-Flag-as-minimum-value-if-same-value-then-last-records/m-p/960587#M374620" target="_self"&gt;your last thread&lt;/A&gt;. We shouldn't have to do this for you. Screen captures or copy and paste from Excel are not acceptable.&lt;/P&gt;</description>
    <pubDate>Sat, 01 Mar 2025 17:21:16 GMT</pubDate>
    <dc:creator>PaigeMiller</dc:creator>
    <dc:date>2025-03-01T17:21:16Z</dc:date>
    <item>
      <title>how to assign the decremental seq and up to 3</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-assign-the-decremental-seq-and-up-to-3/m-p/960615#M374624</link>
      <description>&lt;P&gt;Hi Team&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Assign Sequence Based on Record Count&lt;/STRONG&gt;:&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;If there's only 1 record in a group (&lt;CODE&gt;count = 1&lt;/CODE&gt;), assign &lt;CODE&gt;seq = 1&lt;/CODE&gt;.&lt;/LI&gt;&lt;LI&gt;If there are 2 records in a group (&lt;CODE&gt;count = 2&lt;/CODE&gt;), start from &lt;CODE&gt;2&lt;/CODE&gt; and then &lt;CODE&gt;1&lt;/CODE&gt;.&lt;/LI&gt;&lt;LI&gt;If there are 3 or more records (&lt;CODE&gt;count &amp;gt;= 3&lt;/CODE&gt;), start from &lt;CODE&gt;3&lt;/CODE&gt;, then &lt;CODE&gt;2&lt;/CODE&gt;, and then &lt;CODE&gt;1&lt;/CODE&gt;.&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;Like below&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;Obs   group_id   record_id   seq
--------------------------------
1      1         1           3
2      1         2           2
3      1         3           1
4      1         4           2
5      1         5           1
6      2         1           2
7      2         2           1
8      3         1           2
9      3         2           1&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Thank you,&lt;/P&gt;&lt;P&gt;Raja&lt;/P&gt;</description>
      <pubDate>Sat, 01 Mar 2025 15:16:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-assign-the-decremental-seq-and-up-to-3/m-p/960615#M374624</guid>
      <dc:creator>raja777pharma</dc:creator>
      <dc:date>2025-03-01T15:16:14Z</dc:date>
    </item>
    <item>
      <title>Re: how to assign the decremental seq and up to 3</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-assign-the-decremental-seq-and-up-to-3/m-p/960622#M374628</link>
      <description>&lt;P&gt;Please provide the sample data in a usable form, as shown in these &lt;A href="https://blogs.sas.com/content/sastraining/2016/03/11/jedi-sas-tricks-data-to-data-step-macro/" target="_self"&gt;instructions and examples&lt;/A&gt;, and as &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/18408"&gt;@Ksharp&lt;/a&gt; showed in &lt;A href="https://communities.sas.com/t5/SAS-Programming/Assign-Flag-as-minimum-value-if-same-value-then-last-records/m-p/960587#M374620" target="_self"&gt;your last thread&lt;/A&gt;. We shouldn't have to do this for you. Screen captures or copy and paste from Excel are not acceptable.&lt;/P&gt;</description>
      <pubDate>Sat, 01 Mar 2025 17:21:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-assign-the-decremental-seq-and-up-to-3/m-p/960622#M374628</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2025-03-01T17:21:16Z</dc:date>
    </item>
    <item>
      <title>Re: how to assign the decremental seq and up to 3</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-assign-the-decremental-seq-and-up-to-3/m-p/960632#M374629</link>
      <description>&lt;P&gt;You need to expand on your rules about why you have a second (or apparently many&amp;nbsp; more) 2 and 1 in the Seq values for Groupid. Your rule as stated does not say anything about repeating 2 and 1 and whether you might have a 2 without a 1 such as with only 4 values of groupid:&lt;/P&gt;
&lt;PRE&gt;Obs   group_id   record_id   seq
--------------------------------
1      1         1           3
2      1         2           2
3      1         3           1
4      1         4           2&lt;/PRE&gt;
&lt;P&gt;Or if something other than 2 would be the Seq value for Obs 4. Complete rules are needed to get desired results.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also, is your data already grouped by the values of Groupid? And is your Record_id actually a sequential value as shown all the time and sorted as shown? This question goes to one way to get the "count" without using an external procedure to generate the counts and merge back to the data.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/309000"&gt;@raja777pharma&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hi Team&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Assign Sequence Based on Record Count&lt;/STRONG&gt;:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;If there's only 1 record in a group (&lt;CODE&gt;count = 1&lt;/CODE&gt;), assign &lt;CODE&gt;seq = 1&lt;/CODE&gt;.&lt;/LI&gt;
&lt;LI&gt;If there are 2 records in a group (&lt;CODE&gt;count = 2&lt;/CODE&gt;), start from &lt;CODE&gt;2&lt;/CODE&gt; and then &lt;CODE&gt;1&lt;/CODE&gt;.&lt;/LI&gt;
&lt;LI&gt;If there are 3 or more records (&lt;CODE&gt;count &amp;gt;= 3&lt;/CODE&gt;), start from &lt;CODE&gt;3&lt;/CODE&gt;, then &lt;CODE&gt;2&lt;/CODE&gt;, and then &lt;CODE&gt;1&lt;/CODE&gt;.&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;Like below&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=""&gt;Obs   group_id   record_id   seq
--------------------------------
1      1         1           3
2      1         2           2
3      1         3           1
4      1         4           2
5      1         5           1
6      2         1           2
7      2         2           1
8      3         1           2
9      3         2           1&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Thank you,&lt;/P&gt;
&lt;P&gt;Raja&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 01 Mar 2025 21:05:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-assign-the-decremental-seq-and-up-to-3/m-p/960632#M374629</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2025-03-01T21:05:00Z</dc:date>
    </item>
    <item>
      <title>Re: how to assign the decremental seq and up to 3</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-assign-the-decremental-seq-and-up-to-3/m-p/960644#M374632</link>
      <description>&lt;P&gt;Below code returns seq_want as per your sample data.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input Obs group_id record_id seq;
  datalines;
1 1 1 3
2 1 2 2
3 1 3 1
4 1 4 2
5 1 5 1
6 2 1 2
7 2 2 1
8 3 1 2
9 3 2 1
;
run;

data want(drop=max_record_id);
  do until(last.group_id);
    set have;
    by group_id;
    if last.group_id then
      max_record_id=record_id;
  end;
  
  seq_want=min(3,max_record_id);
  do until(last.group_id);
    set have;
    by group_id;
    output;
    if seq_want=1 then
      seq_want=min(3, max_record_id-record_id);
    else
      seq_want=seq_want-1;
  end;
run;

proc print data=want;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Patrick_0-1740881483794.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/105080i01C637EE08129BBB/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Patrick_0-1740881483794.png" alt="Patrick_0-1740881483794.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/309000"&gt;@raja777pharma&lt;/a&gt;&amp;nbsp;And about providing a SAS data step that creates the sample data below how I converted what you shared using Copilot. The data have step I used above is a copy/paste from the Copilot response.&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Patrick_0-1740881907909.png" style="width: 575px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/105081iA37FE290348B9B23/image-dimensions/575x368?v=v2" width="575" height="368" role="button" title="Patrick_0-1740881907909.png" alt="Patrick_0-1740881907909.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 02 Mar 2025 02:20:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-assign-the-decremental-seq-and-up-to-3/m-p/960644#M374632</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2025-03-02T02:20:14Z</dc:date>
    </item>
    <item>
      <title>Re: how to assign the decremental seq and up to 3</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-assign-the-decremental-seq-and-up-to-3/m-p/960646#M374633</link>
      <description>&lt;P&gt;You appear to want to create SUBGROUPs with a maximum size of 3.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;First let's convert your LISTING into an actual dataset.&amp;nbsp; Let's use a different name for the variable you wanted to create, say EXPECT,&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input group_id record_id expect;
cards;
1 1 3
1 2 2
1 3 1
1 4 2
1 5 1
2 1 2
2 2 1
3 1 2
3 2 1
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Now just create SUBGROUPS of maximum size of 3.&amp;nbsp; We can do it one data step by reading the data twice.&amp;nbsp; Once to discover the new subgroups and their size and then a second time to read in the data again so we can attach the new variable counting down instead of UP.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  do _n_ = 1 by 1;
    set have;
    by group_id;
    if first.group_id then subgroup=0;
    if _n_=3 or last.group_id then leave;
  end;
  subgroup+1;
  do seq=_n_ to 1 by -1 ;
    set have;
    output;
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Results:&lt;/P&gt;
&lt;PRE&gt;                   record_
Obs    group_id       id      expect    subgroup    seq

 1         1          1          3          1        3
 2         1          2          2          1        2
 3         1          3          1          1        1
 4         1          4          2          2        2
 5         1          5          1          2        1
 6         2          1          2          1        2
 7         2          2          1          1        1
 8         3          1          2          1        2
 9         3          2          1          1        1
&lt;/PRE&gt;</description>
      <pubDate>Sun, 02 Mar 2025 02:37:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-assign-the-decremental-seq-and-up-to-3/m-p/960646#M374633</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2025-03-02T02:37:26Z</dc:date>
    </item>
    <item>
      <title>Re: how to assign the decremental seq and up to 3</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-assign-the-decremental-seq-and-up-to-3/m-p/960648#M374635</link>
      <description>&lt;P&gt;Pass through each GROUP_ID twice.&amp;nbsp; The first pass to generate a total count, the second pass to generate the descending WANT_SEQ:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input Obs group_id record_id seq;
  datalines;
1 1 1 3
2 1 2 2
3 1 3 1
4 1 4 2
5 1 5 1
6 2 1 2
7 2 2 1
8 3 1 2
9 3 2 1
run;

data want (drop=_:) ;
  set have (in=firstpass)
      have (in=secondpass);
  by group_id;

  if first.group_id then call missing(_n_remaining,want_seq);
  if firstpass then _n_remaining+1;

  if secondpass;
  if want_seq in(.,1) then want_seq=min(_n_remaining,3);
  else want_seq +(-1);
  _n_remaining+(-1);
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 02 Mar 2025 04:26:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-assign-the-decremental-seq-and-up-to-3/m-p/960648#M374635</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2025-03-02T04:26:22Z</dc:date>
    </item>
  </channel>
</rss>

