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

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

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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