BookmarkSubscribeRSS Feed
knveraraju91
Barite | Level 11

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

2 REPLIES 2
Patrick
Opal | Level 21

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;

mkeintz
PROC Star

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

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 1249 views
  • 3 likes
  • 3 in conversation