Use prxmatch to match strings ending in a certain

2020-03-26 06:09发布

问题:

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;

回答1:

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.



回答2:

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;



标签: regex sas