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;

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 560 views
  • 2 likes
  • 4 in conversation