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.
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
;
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
;
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);
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.
Ready to level-up your skills? Choose your own adventure.