Dear,
In my data, the following values are present. I am not getting the output I need with my code. Please help. Thanks.
value
ALLEGRA-D /01367401/
AMOXICILLIN W/CLAVULANATE POTASSIUM
ASCORBIC ACID W/BETACAROTENE/BIOTIN/08634701/
BUDESONIDE W/FORMOTEROL FUMARATE
ESOMEPRAZOLE MAGNESIUM
HYDROCODONE COMPOUND /01224801/
output needed;
ALLEGRA-D
AMOXICILLIN W/CLAVULANATE POTASSIUM
ASCORBIC ACID W/BETACAROTENE/BIOTIN
BUDESONIDE W/FORMOTEROL FUMARATE
ESOMEPRAZOLE MAGNESIUM
HYDROCODONE COMPOUND
code:
value1=compress(value,'','d');
output getting;
ALLEGRA-D//
AMOXICILLINW/CLAVULANATE POTASSIUM
ASCORBICACID W/BETACAROTENE/BIOTIN//
BUDESONIDEW/FORMOTEROLFUMARATE
ESOMEPRAZOLEMAGNESIUM
HYDROCODONECOMPOUND//
Assuming the pattern you need to remove is at the end of the string and looks like /<at least one digit>/ then the following should do the job.
data sample;
infile datalines truncover;
length have_str want_str $60;
input have_str $60.;
want_str=prxchange('s/\/\d+\/\s*$//oi',1,have_str);
datalines;
ALLEGRA-D /01367401/
AMOXICILLIN W/CLAVULANATE POTASSIUM
ASCORBIC ACID W/BETACAROTENE/BIOTIN/08634701/
BUDESONIDE W/FORMOTEROL FUMARATE
ESOMEPRAZOLE MAGNESIUM
HYDROCODONE COMPOUND /01224801/
;
run;
You're halfway there. You removed all the digits, but you still have an unwanted pair '//'. Use the TRANSTRN function to eliminate those.
data want;
set have;
value1=compress(value,'','d');
value1=transtrn(value1,'//','');
/* or value1=transtrn(compress(value,,'d'),'//',''); */
run;
Of course your program assumes the only time you have digits is when they occupy every character between a pair of slashes. You don't expect numerals anywhere else.
Here's a more specific rule. If value ends with a pair of slashes encompasing digits, remove it all. This would leave other digits in place:
data want (drop=lastchar lastword);
set have;
lastchar=substr(value,length(value),1);
if lastchar='/' then do;
lastword=scan(value,-2,'/');
if notdigit(trim(lastword))=0 then value1=transtrn(value,cats('/',lastword,'/'),'');
end;
else value1=value;
run;
Regards,
Mark
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.