Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use prxmatch to match strings ending in a certain character

Tags:

regex

sas

Match strings ending in certain character

I am trying to get create a new variable which indicates if a string ends with a certain character.

Below is what I have tried, but when this code is run, the variable ending_in_e is all zeros. I would expect that names like "Alice" and "Jane" would be matched by the code below, but they are not:

proc sql;
select *,
case 
when prxmatch("/e$/",name)  then 1
     else 0
end as ending_in_e
from sashelp.class
;quit;
like image 843
Rasmus Larsen Avatar asked Jan 22 '26 05:01

Rasmus Larsen


2 Answers

You should account for the fact that, in SAS, strings are of char type and spaces are added up to the string end if the actual value is shorter than the buffer.

Either trim the string:

prxmatch("/e$/",trim(name))

Or add a whitespace pattern:

prxmatch("/e\s*$/",name)
            ^^^

to match 0 or more whitespaces.

like image 104
Wiktor Stribiżew Avatar answered Jan 25 '26 03:01

Wiktor Stribiżew


SAS character variables are fixed length. So you either need to trim the trailing spaces or include them in your regular expression.

Regular expressions are powerful, but they might be confusing to some. For such a simple pattern it might be clearer to use simpler functions.

proc print data=sashelp.class ;
  where char(name,length(name))='e';
run;

enter image description here

like image 22
Tom Avatar answered Jan 25 '26 01:01

Tom