SAS PROC SQL 一条语句中不包含多个值
SAS PROC SQL NOT CONTAINS multiple values in one statement
在 PROC SQL 中,我需要 select 所有行,其中名为 "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 中不起作用,我收到以下错误:
ERROR: Function CONTAINS could not be located.
我不想使用 LIKE。
您可以使用 NOT IN
:
SELECT * FROM A WHERE
NAME NOT IN ('abc','cde','fbv');
我想你可以使用正则表达式。
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。
如果项目数量超过构建内部代码的合理数量,您可以创建一个 table(下面的 work.words)来存储单词并迭代它以检查出现情况:
data work.values;
input name $;
datalines;
xyabcde
xyzxyz
xycdeyz
xyzxyzxyz
fbvxyz
;
run;
data work.words;
length word ;
input word $;
datalines;
abc
cde
fbv
;
run;
data output;
set values;
/* build a has of words */
length word ;
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;
在 PROC SQL 中,我需要 select 所有行,其中名为 "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 中不起作用,我收到以下错误:
ERROR: Function CONTAINS could not be located.
我不想使用 LIKE。
您可以使用 NOT IN
:
SELECT * FROM A WHERE
NAME NOT IN ('abc','cde','fbv');
我想你可以使用正则表达式。
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。
如果项目数量超过构建内部代码的合理数量,您可以创建一个 table(下面的 work.words)来存储单词并迭代它以检查出现情况:
data work.values;
input name $;
datalines;
xyabcde
xyzxyz
xycdeyz
xyzxyzxyz
fbvxyz
;
run;
data work.words;
length word ;
input word $;
datalines;
abc
cde
fbv
;
run;
data output;
set values;
/* build a has of words */
length word ;
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;