BookmarkSubscribeRSS Feed
lsehlola
Calcite | Level 5

Hi All

 

I have a table with duplicate key. This key can occur any number of times as per below table. 

 

We need to look at the highest number in ATM_ACNT_S. Where ATM_ACNT_S - 4, add one to the highest number of ATM_ACNT_S and populate ATM_ACNT_S_new.

 

if ATM_ACNT_S ne 4, then the ATM_ACNT_S_NEW = ATM_ACNT_S else if ATM_ACNT_S = 4 then ATM_ACNT_S_NEW = Highest number of ATM_ACNT_S + 1

 

CIDATM_ACNT_SATM_ACNT_S_NEWCOUNTER
92906993511111
9290699354162
9290699354173
9290699354184
92906993512125
92906993513136
92906993514147
92906993515158
2 REPLIES 2
PaigeMiller
Diamond | Level 26
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;
--
Paige Miller
mkeintz
PROC Star

If your data are sorted by CID, then a data step with two passes through each CID can do what you want:

 

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;

 

BTW, if the data are grouped by CID, but not in ascending or descending order, you can still use the above approach:

 

 

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;

 

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 504 views
  • 0 likes
  • 3 in conversation