<?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: Looking at the next variable number and assigning a new field in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Looking-at-the-next-variable-number-and-assigning-a-new-field/m-p/730099#M227299</link>
    <description>&lt;P&gt;If your data are sorted by CID, then a data step with two passes through each CID can do what you want:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input CID	ATM_ACNT_S COUNTER;
cards;
929069935	11	1
929069935	4	2
929069935	4	3
929069935	4	4
929069935	12	5
929069935	13	6
929069935	14	7
929069935	15	8
run;
data want (drop=_:);
  set have (in=firstpass)
      have (in=secondpass);
  by cid;
  retain _s_max .;
  if first.cid then _s_max=.;
  if firstpass then _s_max=max(_s_max,atm_acnt_s);
  if secondpass;
  if atm_acnt_s^=4 then atm_acnt_s_new=atm_acnt_s;
  else do;
    _s_max+1;
    atm_acnt_s_new=_s_max;
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;BTW, if the data are grouped by CID, but not in ascending or descending order, you can still use the above approach:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want (drop=_:);
  do until (last.cid);
    set have;
    by cid notsorted;
    _s_max=max(_s_max,atm_acnt_s);
  end;

  do until (last.cid);
    set have;
    by cid notsorted;
    if atm_acnt_s^=4 then atm_acnt_s_new=atm_acnt_s;
    else do;
      _s_max+1;
      atm_acnt_s_new=_s_max;
    end;
    output;
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 30 Mar 2021 14:07:01 GMT</pubDate>
    <dc:creator>mkeintz</dc:creator>
    <dc:date>2021-03-30T14:07:01Z</dc:date>
    <item>
      <title>Looking at the next variable number and assigning a new field</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Looking-at-the-next-variable-number-and-assigning-a-new-field/m-p/730066#M227291</link>
      <description>&lt;P&gt;Hi All&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a table with duplicate key. This key can occur any number of times as per below table.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;We need to look at the highest number in&amp;nbsp;ATM_ACNT_S. Where&amp;nbsp;ATM_ACNT_S - 4, add one to the highest number of&amp;nbsp;ATM_ACNT_S and populate&amp;nbsp;ATM_ACNT_S_new.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;if&amp;nbsp;ATM_ACNT_S ne 4, then the&amp;nbsp;ATM_ACNT_S_NEW =&amp;nbsp;ATM_ACNT_S else if&amp;nbsp;ATM_ACNT_S = 4 then&amp;nbsp;ATM_ACNT_S_NEW = Highest number of&amp;nbsp;ATM_ACNT_S + 1&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;CID&lt;/TD&gt;&lt;TD&gt;ATM_ACNT_S&lt;/TD&gt;&lt;TD&gt;ATM_ACNT_S_NEW&lt;/TD&gt;&lt;TD&gt;COUNTER&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;929069935&lt;/TD&gt;&lt;TD&gt;11&lt;/TD&gt;&lt;TD&gt;11&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;929069935&lt;/TD&gt;&lt;TD&gt;4&lt;/TD&gt;&lt;TD&gt;16&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;929069935&lt;/TD&gt;&lt;TD&gt;4&lt;/TD&gt;&lt;TD&gt;17&lt;/TD&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;929069935&lt;/TD&gt;&lt;TD&gt;4&lt;/TD&gt;&lt;TD&gt;18&lt;/TD&gt;&lt;TD&gt;4&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;929069935&lt;/TD&gt;&lt;TD&gt;12&lt;/TD&gt;&lt;TD&gt;12&lt;/TD&gt;&lt;TD&gt;5&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;929069935&lt;/TD&gt;&lt;TD&gt;13&lt;/TD&gt;&lt;TD&gt;13&lt;/TD&gt;&lt;TD&gt;6&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;929069935&lt;/TD&gt;&lt;TD&gt;14&lt;/TD&gt;&lt;TD&gt;14&lt;/TD&gt;&lt;TD&gt;7&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;929069935&lt;/TD&gt;&lt;TD&gt;15&lt;/TD&gt;&lt;TD&gt;15&lt;/TD&gt;&lt;TD&gt;8&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;</description>
      <pubDate>Tue, 30 Mar 2021 12:33:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Looking-at-the-next-variable-number-and-assigning-a-new-field/m-p/730066#M227291</guid>
      <dc:creator>lsehlola</dc:creator>
      <dc:date>2021-03-30T12:33:19Z</dc:date>
    </item>
    <item>
      <title>Re: Looking at the next variable number and assigning a new field</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Looking-at-the-next-variable-number-and-assigning-a-new-field/m-p/730078#M227292</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input CID ATM_ACNT_S COUNTER;
cards;
929069935	11	1
929069935	4	2
929069935	4	3
929069935	4	4
929069935	12	5
929069935	13	6
929069935	14	7
929069935	15	8
;

proc summary nway data=have(where=(atm_acnt_s^=4));
    class cid;
    var atm_acnt_s;
    output out=_max_(drop=_:) max=/autoname;
run;
data want;
    merge have _max_;
    by cid;
    if atm_acnt_s=4 then do;
        add+1;
        atm_acnt_s_new=atm_acnt_s_max+add;
    end;
    else atm_acnt_s_new=atm_acnt_s;
    drop add;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 30 Mar 2021 13:05:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Looking-at-the-next-variable-number-and-assigning-a-new-field/m-p/730078#M227292</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2021-03-30T13:05:23Z</dc:date>
    </item>
    <item>
      <title>Re: Looking at the next variable number and assigning a new field</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Looking-at-the-next-variable-number-and-assigning-a-new-field/m-p/730099#M227299</link>
      <description>&lt;P&gt;If your data are sorted by CID, then a data step with two passes through each CID can do what you want:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input CID	ATM_ACNT_S COUNTER;
cards;
929069935	11	1
929069935	4	2
929069935	4	3
929069935	4	4
929069935	12	5
929069935	13	6
929069935	14	7
929069935	15	8
run;
data want (drop=_:);
  set have (in=firstpass)
      have (in=secondpass);
  by cid;
  retain _s_max .;
  if first.cid then _s_max=.;
  if firstpass then _s_max=max(_s_max,atm_acnt_s);
  if secondpass;
  if atm_acnt_s^=4 then atm_acnt_s_new=atm_acnt_s;
  else do;
    _s_max+1;
    atm_acnt_s_new=_s_max;
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;BTW, if the data are grouped by CID, but not in ascending or descending order, you can still use the above approach:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want (drop=_:);
  do until (last.cid);
    set have;
    by cid notsorted;
    _s_max=max(_s_max,atm_acnt_s);
  end;

  do until (last.cid);
    set have;
    by cid notsorted;
    if atm_acnt_s^=4 then atm_acnt_s_new=atm_acnt_s;
    else do;
      _s_max+1;
      atm_acnt_s_new=_s_max;
    end;
    output;
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 30 Mar 2021 14:07:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Looking-at-the-next-variable-number-and-assigning-a-new-field/m-p/730099#M227299</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2021-03-30T14:07:01Z</dc:date>
    </item>
  </channel>
</rss>

