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",&/+');
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 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.
I have the data
Var
paracetamol AND nurophen + vitamin/mineral
want
var1 var2 vr3 v4
paracetamol nurophen vitamin mineral
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.
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.
@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.
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
Thanks to both of you.
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.
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.
Ready to level-up your skills? Choose your own adventure.