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);

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!

Register now

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 1526 views
  • 5 likes
  • 3 in conversation