How to extract text data from a variable

Solved
Regular Contributor
Posts: 208

How to extract text data from a variable

I have below text data

"If  IsEqualTo Inclusion 1 then... set datapoint value for ILIST in Inclusion Criteria to LIST #1 - The subject is a boy or girl, aged ≥7 and <12years (patients who turn 12 years during the study will be allowed to continue in the game)., and execute the ""Return True"" custom function".

In this text i need to extract "The subject is a boy or girl, aged ≥7 and <12years (patients who turn 12 years during the study will be allowed to continue in the game)"  i.e whenever it starts with "-" and ends with ".," then i need the text in between.

Any help

Accepted Solutions
Solution
‎04-04-2018 08:48 AM
Super User
Posts: 9,427

Re: How to extract text data from a variable

If your logic is acurate then that is simple:

/* Gets all text from - */
inter=substr(have,index(have,"-"));
/* gets all text up to . */
want=substr(inter,1,index(inter,"."));

Assumes both are present etc.

All Replies
Solution
‎04-04-2018 08:48 AM
Super User
Posts: 9,427

Re: How to extract text data from a variable

If your logic is acurate then that is simple:

/* Gets all text from - */
inter=substr(have,index(have,"-"));
/* gets all text up to . */
want=substr(inter,1,index(inter,"."));

Assumes both are present etc.

Occasional Contributor
Posts: 14

Re: How to extract text data from a variable

HI!

If your text is in a variable called "text", then this would work -->

want = scan(scan(text,2,'-'),1,'.') ;

Best regards,

Claes

Super Contributor
Posts: 331

Re: How to extract text data from a variable

Hello,

A solution with a regexp :

data have;
string='If  IsEqualTo Inclusion 1 then... set datapoint value for ILIST in Inclusion Criteria to LIST #1 - The subject is a boy or girl, aged ≥7 and <12years (patients who turn 12 years during the study will be allowed to continue in the game)., and execute the ""Return True"" custom function';
extract=prxchange("s/.*-(.*)\..*/\$1/",1,string);
run;
☑ This topic is solved.