BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
BayzidurRahman
Obsidian | Level 7

I need to split a character variable by multiple characters and the keyword "AND". When I am using the following it is considering  all the 3 letters separately as delimiters.

gen1=scan(GenericName, 1, '"AND",&/+');

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

So replace all of the AND's with & instead.

data have;
  input var $80.;
cards;
paracetamol AND nurophen + vitamin/mineral
acetaminophen and ibuprofen And naproxen 
;

data want;
  dlm=',&/+';
  set have;
  fixed=var;
  do until(loc=0);
    loc=findw(fixed,'and',dlm,'si');
    if loc then fixed=catx('&',substrn(fixed,1,loc-1),substrn(fixed,loc+3));
  end;
  do index=1 to countw(fixed,dlm);
    newvar=left(scan(fixed,index,dlm));
    output;
  end;
  drop dlm loc fixed;
run;

proc print;
run;
Obs                       var                        index    newvar

 1     paracetamol AND nurophen + vitamin/mineral      1      paracetamol
 2     paracetamol AND nurophen + vitamin/mineral      2      nurophen
 3     paracetamol AND nurophen + vitamin/mineral      3      vitamin
 4     paracetamol AND nurophen + vitamin/mineral      4      mineral
 5     acetaminophen and ibuprofen And naproxen        1      acetaminophen
 6     acetaminophen and ibuprofen And naproxen        2      ibuprofen
 7     acetaminophen and ibuprofen And naproxen        3      naproxen

View solution in original post

7 REPLIES 7
Tom
Super User Tom
Super User

@BayzidurRahman wrote:

I need to split a character variable by multiple characters and the keyword "AND". When I am using the following it is considering  all the 3 letters separately as delimiters.

gen1=scan(GenericName, 1, '"AND",&/+');


That is how the SCAN() function works.

 

So what is it you actually want to do?

Please provide example input data.  Explain the rules.  Show what results you need.

BayzidurRahman
Obsidian | Level 7

I have the data

Var

paracetamol AND nurophen + vitamin/mineral

 

want

var1                   var2               vr3           v4

paracetamol   nurophen   vitamin  mineral

ballardw
Super User

If you don't want "and" then you could replace occurences of " and " , lead and trailing spaces with a single delimiter.

 

One way, which replaces the AND with a space:

data example;
   string = "paracetamol AND nurophen + vitamin/mineral";
   newstring = tranwrd(string,' AND ',' ');
run;

Caution: tranwrd is case sensitive so would not match 'and' or 'And' or other than all upper case. You might consider upper or lower case conversion of the whole string for consistency.

You would also have to include a space in the Scan delimiter list.

Quite often string manipulation means do one thing then another. Sometimes you can nest functions but may lose track of why you did such later.

BayzidurRahman
Obsidian | Level 7

Thanks for the suggestions.
I do not want to replace AND by a space and then use space as a delimiter. Because some of my other target words will contain space and I want to keep them.

ballardw
Super User

@BayzidurRahman wrote:

Thanks for the suggestions.
I do not want to replace AND by a space and then use space as a delimiter. Because some of my other target words will contain space and I want to keep them.


so use a different character.

Tom
Super User Tom
Super User

So replace all of the AND's with & instead.

data have;
  input var $80.;
cards;
paracetamol AND nurophen + vitamin/mineral
acetaminophen and ibuprofen And naproxen 
;

data want;
  dlm=',&/+';
  set have;
  fixed=var;
  do until(loc=0);
    loc=findw(fixed,'and',dlm,'si');
    if loc then fixed=catx('&',substrn(fixed,1,loc-1),substrn(fixed,loc+3));
  end;
  do index=1 to countw(fixed,dlm);
    newvar=left(scan(fixed,index,dlm));
    output;
  end;
  drop dlm loc fixed;
run;

proc print;
run;
Obs                       var                        index    newvar

 1     paracetamol AND nurophen + vitamin/mineral      1      paracetamol
 2     paracetamol AND nurophen + vitamin/mineral      2      nurophen
 3     paracetamol AND nurophen + vitamin/mineral      3      vitamin
 4     paracetamol AND nurophen + vitamin/mineral      4      mineral
 5     acetaminophen and ibuprofen And naproxen        1      acetaminophen
 6     acetaminophen and ibuprofen And naproxen        2      ibuprofen
 7     acetaminophen and ibuprofen And naproxen        3      naproxen

BayzidurRahman
Obsidian | Level 7

Thanks to both of you.

SAS INNOVATE 2024

Innovate_SAS_Blue.png

Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.

If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website. 

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.

Get the $99 certification deal.jpg

 

 

Back in the Classroom!

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

View all other training opportunities.

Discussion stats
  • 7 replies
  • 314 views
  • 0 likes
  • 3 in conversation