BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
allaboutsas
Calcite | Level 5

Is there a way to use lookbehind for a few possible letters as a whole? In the example below, I need ABC, DEF, GHI be found as a whole (ABCDE is ok), but seems like it was taken as each letter. I tried (ABC|DEF|GHI), it’s not working, one word is working. Any suggestions?

data a;

     var='ABC 1.89 DEF A YELLOW LINES'; output;

     var='ABC 1.89 YELLOW JKLM LINE'; output;

     var='9 GHI YELLOW LINE'; output;

     var='9 H BLK LINE'; output;

     var='9 BC JT GREEN LINE'; output;

run;

data b;

     set a;

     retain re1;

     if _N_=1 then do;

           re1 = prxparse('/(?<=[ABC|DEF|GHI] )(YELLOW|BLK|GREEN)(?=\s*LI)/');

     end;

     if prxmatch(re1,var) then do;

           var2 = prxposn(re1,0,var);

     end;

run;

proc print data=b; run;

1

ABC 1.89 DEF A YELLOW LINES

1

YELLOW

2

ABC 1.89 YELLOW JKLM LINE

1

3

9 GHI YELLOW LINE

1

YELLOW

4

9 H BLK LINE

1

BLK

5

9 BC JT GREEN LINE

1

1 ACCEPTED SOLUTION

Accepted Solutions
gergely_batho
SAS Employee

I think you need () instead of []

prxparse('/(?<=(ABC|DEF|GHI) )(YELLOW|BLK|GREEN)(?=\s*LI)/')

View solution in original post

5 REPLIES 5
ballardw
Super User

Please provide details on what you mean by "not working"? What was your expected output?

allaboutsas
Calcite | Level 5

For example if I have “re1 = prxparse('/(?<=ABC).*(\bYELLOW|BLK|GREEN\b)(?=\s*LI)/');” in code, so ‘YELLOW’ in “ABC 1.89 DEF A YELLOW LINESwill be captured, but I need to list more possible words, like GHI, so YELLOW in 9 GHI YELLOW LINE”’ can be captured too.

gergely_batho
SAS Employee

I think you need () instead of []

prxparse('/(?<=(ABC|DEF|GHI) )(YELLOW|BLK|GREEN)(?=\s*LI)/')

allaboutsas
Calcite | Level 5

gergely! you are right. I thought I tried that, got error earlier, I must have added an extra closed parenthesis. Thank you!

allaboutsas
Calcite | Level 5

Hi, is there a way to look around, either ahead or behind, for example, as long as ABC or GHI is in the string, before or after (\bYELLOW|BLK|GREEN\b) return the color?

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 5 replies
  • 1049 views
  • 0 likes
  • 3 in conversation