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

How would I create a regular expression using prxparse that always starts with x1, ends with y2 and the middle part can be any length (1+) and any characters?

For example, I want:

x1abc_efy2

x12y2

x1a!y2

 

I don't want:

x2abcy2

x1zy3

 

Thank you.

 

1 ACCEPTED SOLUTION

Accepted Solutions
kiranv_
Rhodochrosite | Level 12

something like below. here ^ means starting position where we have x1 as requirement. .+ indicates any character after the starting position and ? stops reading  once it find any  other character mentioned in regex.  $ means at the last position and here we have y2 as per requirement

 

 

 

 DATA want;  
    IF _N_ = 1 THEN PATTERN_NUM = PRXPARSE("/^x1.+?y2$/");  
 
RETAIN PATTERN_NUM; 
    INPUT STRING $30.;    if PRXMATCH(PATTERN_NUM,trim(STRING))= 1 then output; 
	drop pattern_num;
DATALINES; 
x1abc_efy2
x12y2
x1a!y2
x2abcy2
x1zy3
;

View solution in original post

2 REPLIES 2
kiranv_
Rhodochrosite | Level 12

something like below. here ^ means starting position where we have x1 as requirement. .+ indicates any character after the starting position and ? stops reading  once it find any  other character mentioned in regex.  $ means at the last position and here we have y2 as per requirement

 

 

 

 DATA want;  
    IF _N_ = 1 THEN PATTERN_NUM = PRXPARSE("/^x1.+?y2$/");  
 
RETAIN PATTERN_NUM; 
    INPUT STRING $30.;    if PRXMATCH(PATTERN_NUM,trim(STRING))= 1 then output; 
	drop pattern_num;
DATALINES; 
x1abc_efy2
x12y2
x1a!y2
x2abcy2
x1zy3
;
Astounding
PROC Star

Looks like the parsing guys are on vacation today.  If it would help, here's a way to do it without parsing:

 

x1 = index(var, 'x1');

y2 = index(var, 'y2');

if y2 > x1 > 0 then newvar = substr(var, x1, y2 - x1 + 2);

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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.

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
  • 2 replies
  • 918 views
  • 5 likes
  • 3 in conversation