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 2025: Call for Content

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!

Submit your idea!

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
  • 7 replies
  • 1023 views
  • 0 likes
  • 3 in conversation