DATA Step, Macro, Functions and more

Parse with fewest lines of code?

Reply
New Contributor
Posts: 2

Parse with fewest lines of code?

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.

PROC Star
Posts: 7,363

Re: Parse with fewest lines of code?

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=_Smiley Happy;

  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;

New Contributor
Posts: 2

Re: Parse with fewest lines of code?

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

Super User
Posts: 9,682

Re: Parse with fewest lines of code?

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

PROC Star
Posts: 7,363

Re: Parse with fewest lines of code?

: 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.

PROC Star
Posts: 7,363

Re: Parse with fewest lines of code?

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

Super User
Posts: 9,682

Re: Parse with fewest lines of code?

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 .

PROC Star
Posts: 7,363

Re: Parse with fewest lines of code?

: 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

Respected Advisor
Posts: 3,124

Re: Parse with fewest lines of code?

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

Super User
Posts: 9,682

Re: Parse with fewest lines of code?

Or maybe there will be multiple blanks.

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

Ask a Question
Discussion stats
  • 9 replies
  • 313 views
  • 3 likes
  • 4 in conversation