SAS:如何删除两个特定位置之间的单词?

时间:2015-06-09 04:20:18

标签: sas

数据:

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;

2 个答案:

答案 0 :(得分:3)

文字=" Hell_TRIAL21_o World Good Mor_Trial9_ning"

var= catx("",scan(text,1,"_"),"__",scan(text,3,"_"),"_", scan(text,5,"_"))

请注意,变量var的长度可能不适合您的情况。请记住相应地进行调整。

答案 1 :(得分:2)

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;
相关问题