BookmarkSubscribeRSS Feed
SASAlex101
Quartz | Level 8

I'm trying to use RegEx to extract/transform a string column. I want to eliminate anything enclosed by a pipe character.  the regular expression below was tested in an editor regexr.com as shown: 

SASAlex101_0-1699025138052.png

 

When I code this in SAS, I get no result in String2. 

 


data input_data;
Length string $100. desiredresult $100.;
Infile datalines delimiter='#';
  input string $ desiredResult $;
  datalines;
match all of this|not this|match|no|yes|n|y#match all of this match yes y
;
run;
data input_data;
SET WORK.input_data;
string2 = PRXCHANGE('[^| ]*(?=(?:[^|]*\|[^|]*\|)*[^|]*$)',-1,string);
;
run;

Capture.PNG

 I'm not sure what I'm doing wrong here? seems simple enough. any ideas? 

 

5 REPLIES 5
ballardw
Super User

Let's start with the subject line: PRXCHANGE to extract text outside of <> chars Regex not working [closed]

 

None of your code or example text shows and < or > so I am not sure how your subject line actually relates to your shown code, or example pictures.

It isn't even clear to me which is the desired resulting value of the variable sting2. Or the actual rule that may be involved.

 


@SASAlex101 wrote:

I'm trying to use RegEx to extract/transform a string column. I want to eliminate anything enclosed by a pipe character.  the regular expression below was tested in an editor regexr.com as shown: 

SASAlex101_0-1699025138052.png

 

When I code this in SAS, I get no result in String2. 

 


data input_data;
Length string $100. desiredresult $100.;
Infile datalines delimiter='#';
  input string $ desiredResult $;
  datalines;
match all of this|not this|match|no|yes|n|y#match all of this match yes y
;
run;
data input_data;
SET WORK.input_data;
string2 = PRXCHANGE('[^| ]*(?=(?:[^|]*\|[^|]*\|)*[^|]*$)',-1,string);
;
run;

Capture.PNG

 I'm not sure what I'm doing wrong here? seems simple enough. any ideas? 

 


 

SASAlex101
Quartz | Level 8
sorry about that. I corrected the post and content.
Oligolas
Barite | Level 11

Here you go:

want=prxchange('s/(\|[^|]*\|)/ /',-1,string);
________________________

- Cheers -

Tom
Super User Tom
Super User

Since the REGEX you tried is clearly not valid for SAS:

 

520  data input_data;
521  SET WORK.input_data;
522  string2 = PRXCHANGE('[^| ]*(?=(?:[^|]*\|[^|]*\|)*[^|]*$)',-1,string);
523  ;
524  run;

ERROR: Invalid characters "*(?=(?:[^|]*\|[^|]*\|)*[^|]*$)" after end delimiter "]" of regular expression "[^|
       ]*(?=(?:[^|]*\|[^|]*\|)*[^|]*$)".
ERROR: The regular expression passed to the function PRXCHANGE contains a syntax error.
NOTE: Argument 1 to function PRXCHANGE('[^| ]*(?=(?:'[12 of 35 characters shown],-1,'match all of'[12 of 100 characters shown]) at
      line 522 column 11 is invalid.
string=match all of this|not this|match|no|yes|n|y desiredresult=match all of this match yes y string2=  _ERROR_=1 _N_=1
NOTE: There were 1 observations read from the data set WORK.INPUT_DATA.
NOTE: The data set WORK.INPUT_DATA has 1 observations and 3 variables.
NOTE: DATA statement used (Total process time):
      real time           0.01 seconds
      cpu time            0.01 seconds

Perhaps you should EXPLAIN what the rule is from transforming

match all of this|not this|match|no|yes|n|y

into

match all of this match yes y

You seem to have replaced | with spaces.

But why did you remove "not this"?

Ksharp
Super User

data input_data;
Length string $100. desiredresult $100.;
Infile datalines delimiter='#';
  input string $ desiredResult $;
  datalines;
match all of this|not this|match|no|yes|n|y#match all of this match yes y
;
run;
data input_data;
SET WORK.input_data;
string2 = PRXCHANGE('s/\|.*?\|/ /',-1,string);
;
run;

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
  • 5 replies
  • 838 views
  • 1 like
  • 5 in conversation