DATA Step, Macro, Functions and more

How to remove unwanted character from a character string

Reply
Super Contributor
Posts: 272

How to remove unwanted character from a character string

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

Respected Advisor
Posts: 3,887

Re: How to remove unwanted character from a character string

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;

Valued Guide
Posts: 797

Re: How to remove unwanted character from a character string

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

Ask a Question
Discussion stats
  • 2 replies
  • 196 views
  • 3 likes
  • 3 in conversation