iI'm looking for one of a number of substrings in a string. if any of them are found, I want to set a variable to true and remove the substring. for example, if 'this is a test' or one or twenty or 'hello world' are in the field named Description, then set the value of FoundIt to true and remove the substring from description field. I'm assuminthe solution involves regular expressions.
I think it depends on your preference. One possibility could be something like:
data have;
informat description $80.;
infile cards truncover;
input description &;
cards;
Now is the time for this is a test I think
hello world hello world hello everyone
this is one nine twenty thirty and more
;
data want (drop=_:);
array _toremove(4) $30 ("this is a test", "one", "twenty", "hello world");
set have;
do _i=1 to dim(_toremove);
description=strip(transtrn(description,strip(_toremove(_i)),""));
end;
run;
thanks. That looks like it would work. I was wondering though how one might do it using the regular expression functions...
It is easy for PRX.
data have; informat description $80.; infile cards truncover; input description &; cards; Now is the time for this is a test I think hello world hello world hello everyone this is one nine twenty thirty and more ; run; data want ; set have; found=prxmatch('/this is a test|one|twenty|hello world/',description); description=prxchange('s/this is a test|one|twenty|hello world//o',-1,description); run;
Ksharp
ArthurT,
I don't understand what you mean totally .
Do you mean taking these substring as a word?
prx function has a basic perl metacharacter /b will take care of it .
data have;
informat description $80.;
infile cards truncover;
input description &;
cards;
Now is the time for this is a test I think
hello world hello world hello everyone
this is one nine twenty thirty and more
Now is the times for this is a test I think
hello world hello world hello everyone onetime
this is one nine twenty-one thirty and more
;
run;
data want ;
set have;
description=prxchange('s/\bthis is a test|\bone|\btwenty|\bhello world//o',-1,description);
run;
proc print;
run;
produces:
1 | Now is the time for I think |
---|---|
2 | hello everyone |
3 | this is nine thirty and more |
4 | Now is the times for I think |
5 | hello everyone time |
6 | this is nine - thirty and more |
what would one have to add to get the last line to result in:
this is nine twenty-one thirty and more
Art, the simplest way I see is to hard code 'blank' into it:
data have;
informat description $80.;
infile cards truncover;
input description &;
cards;
Now is the time for this is a test I think
hello world hello world hello everyone
this is one nine twenty thirty and more
Now is the times for this is a test I think
hello world hello world hello everyone onetime
this is one nine twenty-one thirty and more
;
run;
data want ;
set have;
description=prxchange('s/\ this is a test |\ one |\ twenty |\ hello world / /o',-1,description);
run;
proc print;
run;
Haikuo
Or maybe there will be multiple blanks.
prxchange('s/\s+this is a test\s+|\s+one\s+/ /o',-1,description);
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.