DATA Step, Macro, Functions and more

Extracting sentences from text

Accepted Solution Solved
Reply
Regular Contributor
Posts: 164
Accepted Solution

Extracting sentences from text

Hi everyone

 

@Ksharp gave a great solusion to the probelm of extracting a sentence with keyword from a text:

 


data want;
 set have;
 pid=prxparse('/([^\.]|\.(?=\d))+\./o');
 start=1;stop=length(string);
 call prxnext(pid,start,stop,string,p,l);
 do while(p>0);
  found=substr(string,p,l);
  if find(found,'keyword','i') then output;
  call prxnext(pid,start,stop,string,p,l);
 end;
run;

Out of interest, anyinw can suggest modification to this code to also extract the sentence before the one that containes the keyword?

Thanks


Accepted Solutions
Solution
‎08-17-2017 06:20 AM
Regular Contributor
Posts: 164

Re: Extracting sentences from text

Managed to solve it !

data want;
 set want_temp;
 pid=prxparse('/([^\.]|\.(?=\d))+\./o');
 start=1;stop=length(string);
 call prxnext(pid,start,stop,string,p,l);
 do while(p>0);
  found=substr(string,p,l);
  found2=substr(string,p2,l2);
  if find(found,'keyword','i') then output;
  p2=p; l2=l;
  call prxnext(pid,start,stop,string,p,l);
 end;
run;

View solution in original post


All Replies
Solution
‎08-17-2017 06:20 AM
Regular Contributor
Posts: 164

Re: Extracting sentences from text

Managed to solve it !

data want;
 set want_temp;
 pid=prxparse('/([^\.]|\.(?=\d))+\./o');
 start=1;stop=length(string);
 call prxnext(pid,start,stop,string,p,l);
 do while(p>0);
  found=substr(string,p,l);
  found2=substr(string,p2,l2);
  if find(found,'keyword','i') then output;
  p2=p; l2=l;
  call prxnext(pid,start,stop,string,p,l);
 end;
run;
☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 1 reply
  • 89 views
  • 0 likes
  • 1 in conversation