SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
SK_11
Obsidian | Level 7

Hi 

I am trying to find a pattern from a sentence e.g I am trying find " I am" and "live" and "with my parents" from "I am 20 years old and I live happily with my parents".

The code I tried is ;

data test;
sentence='I am 20 years old and I live happily with my parents';
pattern=prxparse('/(I am)\w*(live)\w*(with my parents)/i');
b=prxmatch(pattern,sentence);

run:

I am getting 0 value for b

Any help in this regards will be very much appreciated.

1 ACCEPTED SOLUTION

Accepted Solutions
andreas_lds
Jade | Level 19

Your aren't incorporating the blanks between the words you want to find and all other chars in the text. The following regex seems to solve the issue:

(I am)(\w*\W)*(live)(\w*\W)*(with my parents)

View solution in original post

3 REPLIES 3
andreas_lds
Jade | Level 19

Your aren't incorporating the blanks between the words you want to find and all other chars in the text. The following regex seems to solve the issue:

(I am)(\w*\W)*(live)(\w*\W)*(with my parents)
Shmuel
Garnet | Level 18

It seems that the string  (\w*\W)* is used to skip any number of words.

 

Getting result b=1 means that the sentence contains all required substrings

in the defined order.

 

Next code is searching each sub string, if it exists in the sentence:

data _null_;
  sentence='I am 20 years old and I live happily with my parents';
  prxid = prxparse('/(I am)|(live)|(my parents)/');

  length unit $20;
  start=1;
  stop=length(sentence);

  do while (1);
     call prxnext(prxid,start,stop,sentence,pos,len);
     if pos=0 then leave;
    unit = compress(substr(sentence,pos,len));
    put unit=;
  end;
run;

 

SK_11
Obsidian | Level 7
Thanks a lot Andreas for an easy solution.

sas-innovate-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 2255 views
  • 1 like
  • 3 in conversation