问题描述
我正在尝试创建一个新变量,该变量指示字符串是否以某个字符结尾.
I am trying to get create a new variable which indicates if a string ends with a certain character.
以下是我尝试过的,但是当运行此代码时,变量ending_in_e 全为零.我希望像Alice"和Jane"这样的名字会被下面的代码匹配,但它们不是:
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;
推荐答案
你应该考虑到这样一个事实,在 SAS 中,字符串是 char 类型的,如果实际值比缓冲区短.
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.
任一修剪
字符串:
prxmatch("/e$/",trim(name))
或者添加一个空白模式:
Or add a whitespace pattern:
prxmatch("/e\s*$/",name)
^^^
匹配 0 个或多个空格.
to match 0 or more whitespaces.
这篇关于使用 prxmatch 匹配以某个字符结尾的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!