在PROC SQL中,我需要选择所有行,其中名为“NAME”的列不包含多个值“abc”,“cde”和“fbv”,无论这些值之前或之后是什么。所以我这样做了:
SELECT * FROM A WHERE
NAME NOT CONTAINS "abc"
AND
NAME NOT CONTAINS "cde"
AND
NAME NOT CONTAINS "fbv";
效果很好,但我想如果我们有一百个条件就会很头疼。所以我的问题是 - 我们可以在PROC SQL中的单个语句中完成此操作吗? 我试过用这个:
SELECT * FROM A WHERE
NOT CONTAINS(NAME, '"abc" AND "cde" AND "fbv"');
但这在PROC SQL中不起作用,我收到以下错误:
错误:无法找到功能CONTAINS。
我不想使用LIKE。
答案 0 :(得分:2)
我想你可以使用正则表达式。
data a;
input name $;
datalines;
xyabcde
xyzxyz
xycdeyz
xyzxyzxyz
fbvxyz
;;;;
run;
proc sql;
SELECT * FROM A WHERE
NAME NOT CONTAINS "abc"
AND
NAME NOT CONTAINS "cde"
AND
NAME NOT CONTAINS "fbv";
SELECT * FROM A WHERE
NOT (PRXMATCH('~ABC|CDE|FBV~i',NAME));
quit;
但是,你不能以这种方式使用CONTAINS。
答案 1 :(得分:0)
您可以使用NOT IN
:
SELECT * FROM A WHERE
NAME NOT IN ('abc','cde','fbv');
答案 2 :(得分:0)
如果项目数量高于合理数量以构建内部代码,您可以创建一个表(下面的work.words)来存储单词并迭代它以检查出现的事项:
data work.values;
input name $;
datalines;
xyabcde
xyzxyz
xycdeyz
xyzxyzxyz
fbvxyz
;
run;
data work.words;
length word $50;
input word $;
datalines;
abc
cde
fbv
;
run;
data output;
set values;
/* build a has of words */
length word $50;
if _n_ = 1 then do;
/* this runs once only */
call missing(word);
declare hash words (dataset: 'work.words');
words.defineKey('word');
words.defineData('word');
words.defineDone();
end;
/* iterate hash of words */
declare hiter iter('words');
rc = iter.first();
found = 0;
do while (rc=0);
if index(name, trim(word)) gt 0 then do; /* check if word present using INDEX function */
found= 1;
rc = 1;
end;
else rc = iter.next();
end;
if found = 0 then output; /* output only if no word found in name */
drop word rc found;
run;