BookmarkSubscribeRSS Feed
ScifiGuy
Calcite | Level 5

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.

9 REPLIES 9
art297
Opal | Level 21

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;

ScifiGuy
Calcite | Level 5

thanks.  That looks like it would work.  I was wondering though how one might do it using the regular expression functions...

Ksharp
Super User

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

art297
Opal | Level 21

: Depending upon what the OP considers a substring, your code may or may not suffice.  e.g., it will change everyone to one and remove any other identified text that happens to be part of a substring.

art297
Opal | Level 21

: Actually, my proposed code has the same problem.  Is there a way to get regular expression to only recognize words?

Ksharp
Super User

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 .

art297
Opal | Level 21

: Almost. The following:

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:

1Now is the time for I think
2hello everyone
3this is nine thirty and more
4Now is the times for I think
5hello everyone time
6this 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

Haikuo
Onyx | Level 15

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

Ksharp
Super User

Or maybe there will be multiple blanks.

prxchange('s/\s+this is a test\s+|\s+one\s+/ /o',-1,description);

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 9 replies
  • 957 views
  • 3 likes
  • 4 in conversation