在SAS中使用压缩和扫描创建新变量

在SAS中使用压缩和扫描创建新变量,sas,compression,Sas,Compression,我是SAS新手,只想使用compress and SCAN在我的新列中保留另一列括号之间的内容: x abc(animal) efg(food) hij(plant) 我试过: DATA NEW set OLD; y = (compress(scan([x], 2, '('), ')'); RUN; PROC PRINT NEW; RUN; 但我似乎无法让它发挥作用。任何见解都会有所帮助 应该不需要compress(),除非要从提取的值中删除一些其他字符。您可能需要

我是SAS新手,只想使用compress and SCAN在我的新列中保留另一列括号之间的内容:

 x 
abc(animal)
efg(food)
hij(plant)
我试过:

DATA NEW 
    set OLD;
    y = (compress(scan([x], 2, '('), ')');
RUN;
PROC PRINT NEW; 
RUN; 

但我似乎无法让它发挥作用。任何见解都会有所帮助

应该不需要
compress()
,除非要从提取的值中删除一些其他字符。您可能需要使用
left()
xxx(yyy)
等值中删除前导空格

如果您有一些值,比如
(没有第一个单词)
,那么您可能需要使用索引1而不是2。如果是这样,您可以使用
=:
测试X是否以打开的参数开头

y=scan(x,1+(x^=:'('),'()');

如果您知道()总是在末尾,那么可以使用索引-1。但是,如果某些字符串在结束参数后有字符,那么您需要使用-2来代替。

@tom answer是完美的,另一种方法是使用prxsubstr

 data have;
 input x $20.;
 datalines;
abc(animal)
efg(food)
hij(plant)
;

 prxparse \(.+?\) is to find anything between parenthesis
 \( -- starting of parenthesis
 .+?\( ---  anything till closing parenthesis
调用prxsubstr捕获它找到模式和长度的位置。 然后执行substr以获取括号内的值

data want;
set have;
if _n_= 1 then do;
retain re;
re = prxparse('/\(.+?\)/');
end;
call prxsubstr(re, trim(x), position, length);

if position gt 0 then 
 new=substr(trim(x),position+1, length-2);
run;

该值是否总是在打开参数之前有某些内容?它总是有并且只有一组括号吗?
data want;
set have;
if _n_= 1 then do;
retain re;
re = prxparse('/\(.+?\)/');
end;
call prxsubstr(re, trim(x), position, length);

if position gt 0 then 
 new=substr(trim(x),position+1, length-2);
run;