BookmarkSubscribeRSS Feed
cho16
Obsidian | Level 7

 

I have a data set with a bunch of code as example below. I want to delete the code BH1003 for all LOB’s except HAR.

Any help with SAS code is much appreciated.

 

Code                                     LOB

BH1003                                 HAR

BH1003                                 LIP

BH1003,BH1005,BH1007   LIP

BH1005                                 MA

AEHJ                                    IP

BH1003,BH1006,BH1008   HAR

BH1006,BH1003,BH1004   CCC

 

Want

Code                                     LOB

BH1003                                 HAR

BH1005,BH1007     LIP

BH1005                                 MA

AEHJ                                    IP

BH1003,BH1006,BH1008   HAR

BH1006, BH1004                 CCC

3 REPLIES 3
novinosrin
Tourmaline | Level 20

Hi @cho16   See if this helps

 


data have;
input Code       :$30.                              LOB $;
cards;
BH1003                                 HAR
BH1003                                 LIP
BH1003,BH1005,BH1007   LIP
BH1005                                 MA
AEHJ                                    IP
BH1003,BH1006,BH1008   HAR
BH1006,BH1003,BH1004   CCC
;

data want;
 set have;
 length want $100;
 if lob ne 'HAR' then
 do _n_=1 to countw(code,',');
  if scan(code,_n_,',')='BH1003' then continue;
  want=catx(',',want,scan(code,_n_,','));
 end;
 else want=code;
 if want>' ';
run;
kiranv_
Rhodochrosite | Level 12
data want;
set have;
if LOB ne 'HAR' then do;
code= tranwrd(code,'BH1003,',''  );
code= tranwrd(code,'BH1003',''  );
end;
if code = '' then delete;
run;
Patrick
Opal | Level 21

or this way.

data have;
  input Codes :$40. LOB $;
  datalines;
BH1003 HAR
BH1003 LIP
BH1003,BH1005,BH1007 LIP
BH1005 MA
AEHJ IP
BH1003,BH1006,BH1008 HAR
BH1006,BH1003,BH1004 CCC
;

data want;
  set have;
  if lob ne 'HAR' then
    do;
      codes=prxchange('s/\bBH1003\b,?//oi',-1,strip(codes));
      if missing(codes) then delete;
    end;
run;

proc print;
run;

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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
  • 3 replies
  • 1272 views
  • 2 likes
  • 4 in conversation