03-28-2016 12:02 PM
I have a variable that has different strings seperated by a pipe sign, Say as : VAR1 = STRING1|STRING2|STRING3 and so - on.
Where only one string will contain a KEYWORD --> "EXTRACT_ME".
And I would like to extract only the string that has EXTRACT_ME keyword. (an example is shown below)
I can defenitely achieve it with the help of a DO loop and serach for the KEYWORD in each string one by one and extract it.
But I'm looking for something more effieicnt that really need to put it in a do loop. Maybe Perl Expression?
Experts: Any advise? Appreciate your help.
|FIRST STRING|EXTRACT_ME[ABC]|SECOND STRING||EXTRACT_ME[ABC]|
|STRING ONE|EXTRACT_ME[ABCDEFG]|STRING THREE|STRING FOUR||EXTRACT_ME[ABCDEFG]|
03-28-2016 12:18 PM
You can do it with a combination of string functions, but PERL will probably work better.
data have; string="FIRST STRING|EXTRACT_ME[ABC]|SECOND STRING";output; string="STRING ONE|EXTRACT_ME[ABCDEFG]|STRING THREE|STRING FOUR";output; run; data want; set have; loc= index(string, "EXTRACT_ME"); if loc>0 then end= index(substr(string, loc), "|"); want=substr(string, loc, end-1); run;
03-28-2016 12:49 PM
You'll need a slight modification, since LOC > 0 should control execution of the remainder of the statements (not just one).
A similar possibility:
if loc > 0 then want = scan(substr(string, loc), 1, '|');
03-28-2016 11:05 PM
What reason force you to use PRX ? data have; string="FIRST STRING|EXTRACT_ME[ABC]|SECOND STRING";output; string="STRING ONE|EXTRACT_ME[ABCDEFG]|STRING THREE|STRING FOUR";output; run; data want; set have; length want $ 200; pid=prxparse('/EXTRACT_ME\[\w+\]/oi'); call prxsubstr(pid,string,p,l); if p then want=substr(string,p,l); drop pid p l; run;
03-29-2016 06:13 AM
Just in case you don't need the redundant "EXTRACT_ME," but only what's in the square brackets behind this keyword, you can modify Ksharp's Perl regular expression for example as follows:
This expression matches a sequence of one or more characters not equal to "]" after the text "EXTRACT_ME[" (case-insensitive).