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
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.