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
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;
data want;
set have;
if LOB ne 'HAR' then do;
code= tranwrd(code,'BH1003,','' );
code= tranwrd(code,'BH1003','' );
end;
if code = '' then delete;
run;
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;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.