Hell_TRIAL21_o World
Good Mor_Trial9_ning
如何删除_TRIAL21_
和_TRIAL9_
?
我所做的是我找到了第一个_和第二个_的位置。然后我想从第一个_和第二个_压缩。但是压缩功能不可用于这样做。怎么样?
x = index(string, '_');
if (x>0) then do;
y = x+1;
z = find(string, '_', y);
end;
Hell_TRIAL21_o World
Good Mor_Trial9_ning
如何删除_TRIAL21_
和_TRIAL9_
?
我所做的是我找到了第一个_和第二个_的位置。然后我想从第一个_和第二个_压缩。但是压缩功能不可用于这样做。怎么样?
x = index(string, '_');
if (x>0) then do;
y = x+1;
z = find(string, '_', y);
end;
PERL正则表达式是识别这类字符串的好方法。 call prxchange
是将删除相关字符的功能。它需要预先prxparse
来创建搜索和替换参数。
我在这里使用modify
来修改现有的数据集,显然你可能想用set
写出一个新的数据集并首先测试结果。
data have;
input string $ 30.;
datalines;
Hell_TRIAL21_o World
Good Mor_Trial9_ning
;
run;
data have;
modify have;
regex = prxparse('s/_.*_//'); /* identify and remove anything between 2 underscores */
call prxchange(regex,-1,string);
run;
或者要创建一个新的变量和数据集,只需用prxchange
(不需要prxparse
)。
data want;
set have;
new_string = prxchange('s/_.*_//',-1,string);
run;
文本= “Hell_TRIAL21_o世界好Mor_Trial9_ning”
var= catx("",scan(text,1,"_"),"__",scan(text,3,"_"),"_", scan(text,5,"_"))
注意变量var
的长度可能不希望您的case.Remember做出相应的调整。
谢谢! :D它适合我! – Yumi